Change the mat-autocomplete width?
Asked Answered
B

8

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>
Bobbie answered 11/3, 2018 at 7:21 Comment(0)
C
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.

Clegg answered 23/11, 2018 at 13:26 Comment(2)
Thanks! I used panelWidth="auto" and it looks quite good.Erysipeloid
Any idea how I can make the panel behave like with panelWidth="auto" but keep minimum width to fit the hosts width?Remainderman
B
23

Just add the panelWidth property to the mat-autocomplete

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

Bourgeon answered 18/2, 2020 at 12:33 Comment(0)
A
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

Amido answered 13/4, 2018 at 16:50 Comment(3)
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.Berrios
@checketts' suggestion also works for the MatAutocomplete answer (Athan's), e.g. <mat-autocomplete panelWidth="auto">Gilly
What if you have two panels and you only want to change one of them?Khelat
P
14

I solved it with (dynamic):

 [panelWidth]="'auto'"

Following is also possible:

panelWidth="auto"
Purism answered 12/2, 2021 at 11:57 Comment(0)
E
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.

Epigeous answered 25/3, 2020 at 8:53 Comment(2)
Is there a way to also have a max-width?Comedo
better to use documented auto value https://mcmap.net/q/425801/-angular-material-md-autocomplete-dropdown-39-s-widthMournful
M
5

Angular provides configuration options for autocomplete named MAT_AUTOCOMPLETE_DEFAULT_OPTIONS

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

One of the fields of that class is overlayPanelClass: string, that allows to set a custom class to style cdk-overlay-panel 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'"

Marijo answered 28/4, 2022 at 13:3 Comment(1)
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.Facilitate
S
0

In order for options to be same width as mat-autocomplete. You just need to set width 100% on panel and unset panelWidth. Example:

  <mat-form-field style="width: 100%" >

    <mat-autocomplete
      #autoRef="matAutocomplete"
    >
   </mat-autocomplete>
Sheepshead answered 11/9, 2023 at 11:16 Comment(0)
C
0

What I wanted in my whole app was to extend the dropdown panel only if the values were wider than the selector (but not shrink it if not). 'panelWidth' did not work out for me so I am using these global css rules (working for Angular 15 and later):

div.mat-mdc-select-panel,
div.mat-mdc-autocomplete-panel,
div.mat-mdc-menu-panel {
  min-width: fit-content;

  .mat-mdc-option .mdc-list-item__primary-text {
    white-space: nowrap;
  }
}
Clerestory answered 27/5 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.