Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Dynamically set css text-overflow width based on other dom element widths

I have a mat-list in an Angular component like below (food-list.component.html)

sample mat list UI

<mat-list id="site-list">
     <ng-container *cdkVirtualFor="let food of foods">
        <mat-list-item  id="food-{{food.id}}">
          <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>
          <p matLine [ngStyle]="{width: foodLabelWidth} class="overflowText"><small>{{food.label}}</small></p>
          <p matLine [ngStyle]="{width: foodLabelWidth} class="overflowText"><small>{{food.type}}</small></p>
       </mat-list-item>
    </ng-container>
</mat-list>

And in CSS file (food-list.component.scss)

p.overflowText {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

As example in a desktop view mat list item width 100px and, MatIconWidth is 5px. and in mobile view mat list item width 50px and, MatIconWidth is 5px. I want to set text overflow width according to the space that left.

for example, in food-list.component.ts I want to do something like this.

ngOnInit(): void {
   this.foodLabelWidth= matListItemWidth - MatIconWidth;
}

to make it happen I want to get DOM element width of

    <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>

Which lies inside a NgFor loop. Is there a way to achieve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So I could use flex design to make it work !! https://css-tricks.com/snippets/css/a-guide-to-flexbox/

in html file

<mat-list id="site-list">
     <ng-container *cdkVirtualFor="let food of foods">
        <mat-list-item  id="food-{{food.id}}">
          <div class="flex-row w-100">
              <div class="flex-row">
                <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>
              </div>
              <div class="food-title">
                  <p matLine class="overflowText"><small>{{food.label}}</small></p>
                  <p matLine class="overflowText"><small>{{food.type}}</small></p>
              </div>
          </div>
       </mat-list-item>
    </ng-container>
</mat-list>

scss file

::ng-deep .cdk-virtual-scroll-content-wrapper {
  max-width: 100%
}

.flex-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.overflow {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.food-title {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!