Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the mat-autocomplete width?

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> 
like image 864
Jus10 Avatar asked Mar 11 '18 07:03

Jus10


People also ask

How do you increase the width of an autocomplete mat?

mat-autocomplete styles are inherited from cdk-overlay-pane. In order to change width use below CSS. width: auto ! important; works a little better than min-width since it won't make the overlay large if it doesn't need to.

What is Mat autocomplete in angular?

The <mat-autocomplete>, an Angular Directive, is used as a special input control with an inbuilt dropdown to show all possible matches to a custom query. This control acts as a real-time suggestion box as soon as the user types in the input area.


2 Answers

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.

like image 154
Thanos Soulis Avatar answered Sep 28 '22 05:09

Thanos Soulis


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

like image 35
Sai charan Bolisetty Avatar answered Sep 28 '22 07:09

Sai charan Bolisetty