How to reduce the size of MatSelect DropDown Panel

10,408

Solution 1

There is a typo in your template, it needs to be panelClass and not panelclass:

<mat-select panelClass="my-panel" placeholder="Select Course"(selectionChange)="selectCourse($event,courses)" disableOptionCentering>

And as per the documentation here, the ViewEncapsulation on your component has to be set to None for this to work. It is mentioned in the comment of the example where the panelClass is being used:

  // Encapsulation has to be disabled in order for the
  // component style to apply to the select panel.

Here is a working stackblitz that has your style applied to the options.

Solution 2

You can set the width of mat-select by adding the below css to the main styles.css file.

.mat-form-field-infix{
    width: 80px !important;
}

Change 80px as per your requirement.

Share:
10,408
Harsh Nagalla
Author by

Harsh Nagalla

Updated on June 27, 2022

Comments

  • Harsh Nagalla
    Harsh Nagalla almost 2 years

    I am trying to reduce the default size of mat-select drop-down panel, But unable to figure out how to do it

    I have tried giving width , have tried overriding panel class in angular mat-select, But the size doesn't seem to go down

    My Template File

        <div class="col-md-8">
           <mat-form-field style="float: right">
              <mat-select panelclass="my-panel" placeholder="Select Course"(selectionChange)="selectCourse($event,courses)" disableOptionCentering>
             <mat-option *ngFor="let course of courses" [value]="course.courseId">
              {{course.courseCode}}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>
    

    My Css File

     .my-panel {
       background: orange;
       height: 300px;
       display: flex;
       flex-direction: column;
       justify-content: space-between;
       min-width: 350px !important;
       }
    

    Expected Result :
    Expected Result

    Actual Result : Actual Result

    New Issue

  • Harsh Nagalla
    Harsh Nagalla over 5 years
    Your method seems to be working but some issue with the alignment i.stack.imgur.com/cZr2T.png can you plz tell me how to fix this
  • Fabian Küng
    Fabian Küng over 5 years
    Without more information on your components, templates and styles it is extremely difficult to say what the issue is. If I have to guess I would say it is related to your inline style float: right; on the mat-form-field, try to position it without using float and that might help. But this is not really part of the question anymore, as the class is being applied correctly.
  • Ollie
    Ollie over 3 years
    Is there a way to get it to stretch to the full width of its parent?