37

It seems like the <mat-autocomplete> width is always the width of the input we're typing in. Is there no way to make it larger? I have a very small input and can't read the dropdown items. I'm also limited in form space so can't make the input larger.

<mat-form-field style="width: 100px;">
  <input matInput [matAutocomplete]="auto">
  <mat-autocomplete style="width: 500px;" #auto="matAutocomplete">
    <mat-option *ngFor="let item of items" [value]="item">
    </mat-option>
  </mat-autocomplete>
</mat-form-field>
Jus10
  • 14,519
  • 21
  • 52
  • 77

6 Answers6

43

In the document for MatAutocomplete there is a property for exactly what you need:

@Input() panelWidth: string | number

Specify the width of the autocomplete panel. Can be any CSS sizing value, otherwise it will match the width of its host.

By using this property you are not in the risk of meddling with anything else inherited from cdk-overlay-pane.

For further customization you might find this property useful as well:

@Input('class') classList: string

Takes classes set on the host mat-autocomplete element and applies them to the panel inside the overlay container to allow for easy styling.

Thanos Soulis
  • 583
  • 5
  • 9
21

mat-autocomplete styles are inherited from cdk-overlay-pane. In order to change width use below CSS.

::ng-deep .cdk-overlay-pane {
    /* Do you changes here */
      min-width:450px;
}

You can refer here for full example: https://stackblitz.com/edit/angular-vzetqy

  • 5
    `width: auto !important;` works a little better than `min-width` since it won't make the overlay large if it doesn't need to. Then you can use a `max-width` too keep it from getting to unreasonable. – checketts Aug 14 '18 at 20:04
  • 5
    @checketts' suggestion also works for the MatAutocomplete answer (Athan's), e.g. `` – Ollie Feb 04 '20 at 00:39
  • 1
    What if you have two panels and you only want to change one of them? –  Dec 14 '20 at 10:10
21

Just add the panelWidth property to the mat-autocomplete

<mat-autocomplete [panelWidth]="300">

ChadNC
  • 2,528
  • 4
  • 25
  • 39
Pramod Ukkali
  • 261
  • 2
  • 2
14

I solved it with (dynamic):

 [panelWidth]="'auto'"

Following is also possible:

panelWidth="auto"
user1586746
  • 161
  • 2
  • 7
7

Apart from what Athan Soulis says, I've found out by sheer coincidence that panelWidth also accepts value "fit", which does exactly what most of us is aiming for (make it as wide as needed). Not sure it's "any CSS sizing value", but it works.

Tom Kokoska
  • 71
  • 1
  • 2
5

Angular provides configuration options for autocomplete named MAT_AUTOCOMPLETE_DEFAULT_OPTIONS

Documentation: https://material.angular.io/components/autocomplete/api#MatAutocompleteDefaultOptions

One of the field of that class is overlayPanelClass: string, that allow to set custom class to style cdk-overlay-pane for autocomplete.

  1. Inject options in component that you need, through providers: [] in component declaration

providers: [
        {
            provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
            useValue: { overlayPanelClass: 'autocomplete-overlay' },
        },
    ]
  1. Define your class in styles.scss

.autocomplete-overlay {
    width: unset !important;
}
  1. Set fit-content width for container, in which you display options of autocomplete, I use simplebar, so in my case it .simple-bar-content class

.wrapper-for-simplebar {
    height: 100%;

    ::ng-deep.simplebar-content {
        width: fit-content;
    }
}

If you need to set fixed width use [panelWidth] = "'150px'"

dennnisimo
  • 51
  • 1
  • 3
  • I found myself coming back to this thread as I was styling cdk-overlay globally to handle this, which ended up causing a lot of problems with other things. This answer works incredibly well, and I'd suggest people try it if the other options don't work for you. – James Ives Jul 22 '22 at 12:45