Using ::ng-deep it solve the problem, but it affect all mat-select in the same module, so how can I change mat-select style juste in a specific component?
::ng-deep .mat-select-panel{
max-height: none!important;
}
Edit:
<div class="col-md-2 kt-margin-bottom-10-mobile">
<div class="kt-form__control">
<mat-form-field >
<mat-select [(value)]="searchOption"
class="mat-form-field mat-form-field-fluid"
placeholder="Search by...">
<mat-option value="all"><span>All</span></mat-option>
<mat-option value="ref"><span>Reference</span></mat-option>
<mat-option value="name"><span>Name</span></mat-option>
<mat-option value="team"><span>Team</span></mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
You can do this then,
// Add an ID to the div in the HTML
<div class="col-md-2 kt-margin-bottom-10-mobile">
<div class="kt-form__control">
<mat-form-field id="search-option-id"> <-- Add the ID Here
<mat-select
[(value)]="searchOption"
class="mat-form-field mat-form-field-fluid"
placeholder="Search by..."
>
<mat-option value="all"><span>All</span></mat-option>
<mat-option value="ref"><span>Reference</span></mat-option>
<mat-option value="name"><span>Name</span></mat-option>
<mat-option value="team"><span>Team</span></mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
// In your CSS file, you can do this:
#search-option-id > mat-select { <-- You can do this in the CSS
::ng-deep .mat-select-panel {
max-height: none!important;
}
}
Actually, the mat-select-panel is outside your component, in the .cdk-overlay-container. So you can't change CSS only for your own component's mat-selects.
What you CAN do, is adding a class to the select-panel, and then make a true ng-deep rule
<mat-form-field>
<mat-select [(value)]="searchOption"
class="mat-form-field mat-form-field-fluid"
panelClass="auto-height" <-- adding class to panel here
placeholder="Search by...">
<mat-option value="all"><span>All</span></mat-option>
<mat-option value="ref"><span>Reference</span></mat-option>
<mat-option value="name"><span>Name</span></mat-option>
<mat-option value="team"><span>Team</span></mat-option>
</mat-select>
</mat-form-field>
and in your scss file:
::ng-deep .mat-select-panel.auto-height {
max-height: none;
}
Or even better, add it to your material-overload.scss file if you already have one. If not, add it to your styles.scss file, so you don't use ng-deep at all:
.cdk-overlay-container {
.mat-select-panel.auto-height {
max-height: none;
}
}
Related
As the headline says, I updated the version of IntelliJ and now the angular material selector ::ng-deep is showing me as deprecated.
Example:
<mat-form-field class="register-custom-select">
<mat-select formControlName="gender" required>
<mat-option class="register-custom-option" *ngFor="let gender of genders
[value]="gender.genderValue">
{{ gender.genderValue }}
</mat-option>
</mat-select>
</mat-form-field>
::ng-deep .register-custom-select .mat-form-field-underline {
background-color: #838383;
}
What do I need to change to make the selector no longer deprecated and still have the style?
For me using globale style resolve this
In your style.css yu can add
.register-custom-select .mat-form-field-underline {
background-color: #838383!important;
}
I am trying to increase the size of the mat-select options without changing the size of the mat-form-field. It currently looks like this:
But when I try to change the size of the mat-select it pushes the arrow across and into the next field:
Code:
<mat-form-field>
<mat-label>Account Name</mat-label>
<mat-select
id="account_list"
ngDefaultControl
formControlName="account"
class="menu-size"
>
<mat-option
*ngFor="let acc of accounts; let i = index"
[value]="acc.name"
id="account_option_{{ i }}"
>
{{ acc.name }}
</mat-option>
</mat-select>
</mat-form-field>
CSS:
.menu-size {
width: 350px;
}
Is there a way to increase the size of the mat options menu without effecting the mat form field?
You can accomplish what you're after using CSS Flex Box. Reading a full article can be overwhelming, so I usually use an online tool to help me so I can quickly visualize what I'm after and grab the required css styles.
Using the code below I was able to make the select menu take up the remaining empty space after I resize the amount input to 100px:
<div class="flex-container" style="display: flex;">
<mat-form-field class="flex-fill-empty-space" style="flex: 1 1 auto">
<mat-label>Account Name</mat-label>
<mat-select>
<mat-option *ngFor="let acc of [{ name: 'a' }, { name: 'b' }]">{{ acc.name }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="flex-stay-original-size" style="flex: 0 1 auto; width: 100px; margin-left: 1em;">
<mat-label>Amount</mat-label>
<input matInput>
</mat-form-field>
</div>
Example Output:
Taking the example of Juan, I need use 2 selects in the same row, and I modify a few lines to implement the modification, it works:
After:
Before: taking full width of the column
Code:
<div class="flex-container d-flex">
<mat-form-field appearance="fill" class="flex-fill-empty-space" style="flex: 1 1 auto">
<mat-label>Account Name</mat-label>
<mat-select>
<mat-option *ngFor="let year of years" [value]="year.value">
{{year.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="fill" class="flex-stay-original-size ms-4 w-auto" style="flex: 1 1 auto;">
<mat-label>Account Name</mat-label>
<mat-select>
<mat-option *ngFor="let year of years" [value]="year.value">
{{year.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
Im having an issue when I try to change the CSS of dropdown using mat-form-field of angular material in two things :
My CSS doesnt change the propertys <option> and <optgroup> .
cant change :hover CSS for <option>
the position ot the dropdown "cuts" the current selected option.
My dropdown style atm (default angular-material) :
drop-down-atm
The html :
<mat-form-field class="drop-down" appearance="fill">
<select matNativeControl [(ngModel)]="currentActiveModeValue" (change)="onModeChange($event)">
<optgroup *ngFor="let mainModes of activeModArry; let i = index" label="{{ mainModes.name }}">
<option *ngFor="let subModes of mainModes.childModes" [value]="subModes.id">
{{ subModes.name }}
</option>
</optgroup>
</select>
</mat-form-field>
The style I wish to do :
the drop-down I need
Really need some help with that, If someone had this problem before :)
I believe native dropdowns are not style-able. You can try to use the material select and add your own classes to be able to style it
<mat-form-field appearance="fill" class="my-class">
<mat-select [(ngModel)]="currentActiveModeValue" (change)="onModeChange($event)">
<mat-optgroup *ngFor="let mainModes of activeModArry; let i = index" [label]="mainModes.name">
<mat-option *ngFor="let subModes of mainModes.childModes" [value]="subModes.id">
{{ subModes.name }}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
https://material.angular.io/components/select/overview
Edit: You may need to use the ::ng-deep selector to target the shadow elements that material generates.
Something like:
.mat-form-field ::ng-deep .mat-optgroup {
// styles
}
when you work with some material angular components you need declare the styles "globlally". You can include in your file "styles.sccs"
.mat-optgroup-label {
color:black;
font-weight:600;
border-bottom:1px solid silver;
}
.mat-optgroup .mat-option:not(.mat-option-multiple) {
padding-left: 0!important;
}
.mat-option-text {
border-bottom:1px solid silver;
padding-left:32px;
}
The reason is that some Angular components create the "popup" outsite our component. You can see it in a div called cdk-overlay-container. This components has a property panelClass that allow us only change the css of only this component. That's. In your styles.scss you replace the before .css by -I used "special", you use the word you want
.special .mat-optgroup-label {
color:black;
font-weight:600;
border-bottom:1px solid silver;
}
.special .mat-optgroup .mat-option:not(.mat-option-multiple) {
padding-left: 0!important;
}
.special .mat-option-text {
border-bottom:1px solid silver;
padding-left:32px;
}
And in your select
<mat-select panelClass="special" ...>
...
</mat-select>
In your code example, for the hover to work you must use the material elements and then in the css use the ::ng-deep, also verify that it does not have encapsulation set, in the component's .ts in the #Component.
In the html it would look like this:
<mat-form-field class="drop-down" appearance="fill">
<mat-select matNativeControl [(ngModel)]="currentActiveModeValue" (change)="onModeChange($event)">
<mat-optgroup *ngFor="let mainModes of activeModArry; let i = index" label="{{ mainModes.name }}">
<mat-option *ngFor="let subModes of mainModes.childModes" [value]="subModes.id">
{{ subModes.name }}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
and the css would be like this
::ng-deep mat-option:hover {
...
}
Is it possible to have a mat divider (a horizontal line seperating two mat-options)?
I have tried:
<mat-form-field>
<mat-label>Cars</mat-label>
<select matNativeControl required>
<option value="volvo">Volvo</option>
<mat-divider></mat-divider>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</mat-form-field>
A bad workaround I have now is:
<mat-option>--------------------------</mat-option>
However this is not ideal.
I am not sure how exactly you want the divider to appear, but a pure CSS approach would be to overwrite the .mat-option class on your main styles.css, make use of the :after pseudo element, and add a value to the border-bottom CSS property.
.mat-option:after {
content: '';
width: 100%;
border-bottom: solid 2px black;
position: absolute;
left: 0;
z-index: 1;
top: calc(3em - 2px);
}
I have created a demo over here.
You can achieve this using mat-divider itself by just adding ng-container to wrap mat-option and mat-divider. Then use the last ngFor local variable to conditionally remove the mat-divider from the last mat-option.
<mat-select>
<ng-container *ngFor="let food of foods; let last = last">
<mat-option [value]="food.value">
{{food.viewValue}}
</mat-option>
<mat-divider *ngIf="!last"></mat-divider>
</ng-container>
</mat-select>
Here is a working example on StackBlitz.
Yes, It is possible to add mat-divider to mat-select/mat-option
<mat-form-field>
<mat-label> Cars </mat-label>
<mat-select>
<mat-option value="volvo"> Volvo </mat-option>
<mat-divider></mat-divider>
<mat-option value="saab"> Saab </mat-option>
<mat-divider></mat-divider>
<mat-option value="mercedes"> Mercedes </mat-option>
<mat-divider></mat-divider>
<mat-option value="audi"> Audi </mat-option>
</mat-select>
Hope it helps you
Angular material Select documentation provide you help for that, you can use the panelClass directive like the example they show:
<mat-form-field>
<mat-label>Panel color</mat-label>
<mat-select [formControl]="panelColor"
panelClass="example-panel-{{panelColor.value}}">//panelColor.value if you want to handle some class dynamically
<mat-option value="red">Red</mat-option>
<mat-option value="green">Green</mat-option>
<mat-option value="blue">Blue</mat-option>
</mat-select>
</mat-form-field>
and these are the css classes they handle
.example-panel-red.mat-select-panel {
background: rgba(255, 0, 0, 0.5);
}
.example-panel-green.mat-select-panel {
background: rgba(0, 255, 0, 0.5);
}
.example-panel-blue.mat-select-panel {
background: rgba(0, 0, 255, 0.5);
}
So, you can apply wentjun css properties in a class
I have the following HTML to display select locations and below that map will be displayed.
I have the following HTML for the above view,
<mat-form-field class="locationSelector">
<mat-select placeholder="Choose location" (ngModel)="ServiceLocations">
<mat-option *ngFor="let location of ServiceLocations" [value]="location" style="width:200px; background-color: red; font-size: 10px;">
{{ location.areaName }}
</mat-option>
</mat-select>
</mat-form-field>
<div>
<agm-map [latitude]="latitude" [longitude]="longitude" [zoom]="zoom">
</agm-map>
</div>
And corresponding CSS is ,
agm-map {
height: 300px;
position: relative;
}
.locationSelector {
width: 20%;
}
The view I am getting is,
And when I click on select options, I am getting a view as follows,
Please correct me where I am wrong.
All required material components are imported in app.module.ts
Import a theme in your global style.css file.