2
votes

In my Angular Material Application I have a list (mat-list) with several rows (mat-list-items).

I'm trying to prevent the mat-list-items from wrapping their content.

For example, instead of this:

IT Architect
at General Electric

it shall look like this:

IT Architect at General Electric

Here is the html:

component.html:

<mat-list>
      <mat-list-item class="no-wrap">                    
          <h3 class="name"> {{firstName}} {{lastName}} </h3>                                       
      </mat-list-item> 
      <mat-divider></mat-divider>  
      <mat-list-item class="no-wrap" *ngIf="position && company"> {{position}} at {{company}} </mat-list-item>
      <div class="gap"></div>    
      <mat-list-item class="no-wrap">
          <mat-icon>email</mat-icon>
          <span class="email"> {{email}} </span>
      </mat-list-item>
  </mat-list>

I tried to achieve my goal with the following CSS:

component.css:

.no-wrap {
    word-wrap: break-word;
    white-space: pre-wrap;
}

However, this doesn't work.

How can I prevent the items of an angular material list from wrapping the text they contain?

2

2 Answers

2
votes
    <mat-list>
          <mat-list-item class="no-wrap">                    
              <h3 class="name"> <span class="nobreak"> {{firstName}} {{lastName}} </span> </h3>                                       
          </mat-list-item> 
          <mat-divider></mat-divider>  
          <mat-list-item class="no-wrap" *ngIf="position && company"> <span class="nobreak"> {{position}} at {{company}} </span> </mat-list-item>
          <div class="gap"></div>    
          <mat-list-item class="no-wrap">
              <mat-icon>email</mat-icon>
              <span class="email"> {{email}} </span>
          </mat-list-item>
    </mat-list>

css

 .no-wrap {
   overflow: hidden;
   text-overflow: ellipsis;
   white-space: nowrap;
 }
0
votes

Running Angular 11.X

Another option is to add a scrolling viewport around your tree, then set the CSS for your mat-tree-node to white-space: nowrap

<mat-card>
  <mat-card-content>
    <cdk-virtual-scroll-viewport itemSize="30" class="tree">
      <ng-container *cdkVirtualFor="let item of dataSource"></ng-container>
      <mat-tree [dataSource]="dataSource" [treeControl]="treeControl">
        <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding matTreeNodePaddingIndent="20px" class="tree-node">
          <button mat-icon-button disabled></button>
          {{node.name}}
        </mat-tree-node>
        <mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding matTreeNodePaddingIndent="20px" class="tree-node">
          <button mat-icon-button matTreeNodeToggle [attr.aria-label]="'Toggle ' + node.name">
            <mat-icon class="mat-icon-rtl-mirror">
              {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
            </mat-icon>
          </button>
          {{node.name}}
        </mat-tree-node>
      </mat-tree>
    </cdk-virtual-scroll-viewport>
  </mat-card-content>
</mat-card>

CSS

.type-icon {
  color: #757575;
  margin-right: 5px;
}

.tree-node {
  height: 30px;
  min-height: 30px;
  white-space: nowrap;
}

.tree {
  height: 600px;
}