I am using basic mat-select component in my project.
<mat-form-field>
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
how can i remove the underline,I have tried this answer too still no result.
You can do this:
::ng-deep .mat-form-field-underline {
display: none;
}
StackBlitz
You can also use the appearance property on the <form-field> . Setting appearance="none" will remove the underline without any css hack. And you can also get rid of ::ng-deep which is not recommended.
So your code will be like this.
<mat-form-field appearance="none">
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
For more information on appearance, you can check here. https://material.angular.io/components/form-field/overview#form-field-appearance-variants
::ng-deep is going to be depracated.
Use css hierarchy. If you have a global css or scss file... then in the component, wrap this in a div and give it a class.
For example,
<div class="content-wrapper">
...all your html code here
</div>
Then, in your global css/scss file, you can target the .mat-form-field-underline class like so:
.content-wrapper .mat-form-field-underline {
display: none;
}
Here, notice that we don't use comma after class names. This is using hierarchy. It saying that we only want to target the native .mat-form-field-underline class that is wrapped inside the .content-wrapper
Hope this helps. Glad my senior developer had shared me this as we used ViewEncapsualtion, which bled css all over the place. This is the right solution to our use case, hope it helps yours.
This also works for other native mat classes for customization of your code/html.
If you want to hide underline in particular input only then you can do something like below.
<div class="select-block">
<mat-form-field>
<mat-select placeholder="Select Option">
<mat-option>One</mat-option>
<mat-option>Two</mat-option>
<mat-option>Three</mat-option>
</mat-select>
</mat-form-field>
</div>
Add the below-mentioned line of code in the parent class of mat-form-field.
.select-block{
/deep/ mat-form-field {
&.mat-form-field-appearance-legacy {
.mat-form-field-underline {
display: none;
}
}
}
}
Try this way to hide the underline.
Thanks.
Two options to solve the problem:
You can directly setup appearance="none" in mat-form-field tag
ex. <mat-form-field appearance="none">.
You can hide using css
ex. ::ng-deep .mat-form-field-underline { display: none; }
I had appearance="fill" on the mat-form-field. These settings worked to remove the underline for me:
scss:
.mat-form-field-appearance-fill {
.mat-form-field-underline {
display: none;
}
}
Add set encapsulation to ViewEncapsulation.None in the component:
encapsulation: ViewEncapsulation.None
Related
I'm trying to disable an Angular Material Autocomplete component. I would have expected to just be able to set disabled on the input, but that does nothing. (I also tried setting disabled on mat-form-field and mat-autocomplete.) Setting matAutocompleteDisabled on input prevented the options from showing, but still allowed typing in the field. Setting readonly on input prevented typing, but it doesn't change the UI, so seems like that will be confusing for the user. Is this a bug, or am I missing something?
Here's the closest I've come so far, using readonly (and disabled isn't working as expected)
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text"
disabled readonly
placeholder="Pick one"
aria-label="Number"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
You should use formControl to set it, something like:
this.formGroupName.controls['myControl'].disable()
Use [attr.disabled]="true". It will set the disabled to true.
You can do css trick for this purpose.
Apply some class to parent tag of input. In you case upper tag is <mat-form-field class="example-full-width"> so i add disable-block class in this. And applied below css.
.disable-block {
pointer-events: none;
opacity: .7;
}
Full code here.
HTML
<form class="example-form">
<mat-form-field class="example-full-width disable-block">
<input type="text" disabled readonly placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
CSS
.disable-block {
pointer-events: none;
opacity: .7;
}
If you want more lighter text field then you can decrease opacity to .6 or .5 or more want you want.
Hope this will solve you problem.
I am using Angular Material. I have a form with various fields and list items. I have changed the size of mat-form-field inputs using below
mat-form-field.mat-form-field {
font-size: 14px;
}
However since list are not a part of mat-form-field they are still their default 16px. How can I change the size of the <mat-list-item>?
<mat-list-item *ngFor="let file of files">{{file.name}}<mat-list-item>
Try the following CSS definition:
:host /deep/ .mat-list-item-content {
font-size: 18px;
}
I was actually doing this yesterday for my mat-list-items. h6 turned out to be an appropriate size for me.
<mat-list-item *ngFor="let file of files">
<span style="font-size:12px">
{{file.name}}
</span>
<mat-list-item>
or
<mat-list-item *ngFor="let file of files">
<h6>
{{file.name}}
</h6>
<mat-list-item>
You can add the styling directly to the markup. If you use class to reference CSS file, sometimes it won't work.
<mat-list-item style="font-size: 14px; height: 25px" >Test</mat-list-item>
For some reason using Angular 8 this parameter of never does not work for me on a mat-select.
<mat-form-field appearance="outline" floatLabel="never">
<mat-label>Lesson</mat-label>
<mat-select [(value)]="selected" (selectionChange)="onMenuChange($event.value)">
<mat-option value="T1">Lesson 1</mat-option>
<mat-option value="T2">Lesson 2</mat-option>
<mat-option value="T3">Lesson 3</mat-option>
</mat-select>
</mat-form-field>
Always and Auto work, but never won't seem to make it go away.
Managed to come up with the following scss to imitate the label not floating:
.some-custom-class-name {
&.mat-form-field-appearance-outline.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-label {
transform: none;
width: initial;
opacity: 0;
}
&.mat-form-field-appearance-outline.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-outline-gap {
border-top-color: initial;
}
/* Not required, only used to reposition the label slightly
&.mat-form-field-appearance-outline .mat-form-field-label {
top: 1.54375em;
margin-top: -0.25em;
}
*/
}
<mat-form-field appearance="outline" class="some-custom-class-name">
<mat-label>Shouldn't float</mat-label>
<input matInput>
</mat-form-field>
Just append the some-custom-class-name class to the mat-form-field and it should work.
try to use floatLabel="never" or floatLabel="always"
My solution is kinda a shortcut of solution suggest by Dane Brower.
::ng-deep &.mat-form-field-appearance-outline.mat-form-field-should-float {
.mat-form-field-label {
display: none;
}
.mat-form-field-outline-gap {
border-top-color: initial;
}
}
In comparison to his solution, this one just removes the label element from DOM (via display: none) since we don't need it anymore. We also need ::ng-deep pseudo-class here since mat-form-field-... elements are encapsulated within mat-form-field.
Note: you have to be carefull with ::ng-deep and try to prefix it with :host in order to not apply its style globally (in my case :host ::ng-deep didn't work here).
Please refer to https://github.com/angular/components/issues/18267
Just add a place holder in the mat-select.
Like this: <mat-select placeholder="FL">
TLDR:
Just remove the <mat-label> and use a placeholder on <mat-form-field> instead.
You can hide the floating label, you just have to approach the 'outline' variant differently. And there is absolutely no need to forcefully override the label via some weird css hack.
From the Angular Material docs:
Note: only the legacy appearance supports the never option. never was originally added as a way to make the floating label emulate the behavior of a standard input placeholder. However the form field now supports both floating labels and placeholders. Therefore in the non-legacy appearances the never option has been disabled in favor of just using the placeholder.
So it's actually the intended behavior for the 'outline' variant.
All you have to do is to just use the placeholder attribute on <mat-form-field> instead of adding a <mat-label>.
In your case:
<mat-form-field appearance="outline" floatLabel="never" placeholder="Lesson">
<mat-select [(value)]="selected" (selectionChange)="onMenuChange($event.value)">
<mat-option value="T1">Lesson 1</mat-option>
<mat-option value="T2">Lesson 2</mat-option>
<mat-option value="T3">Lesson 3</mat-option>
</mat-select>
</mat-form-field>
So basically people usually add a label they don't even want and need, only to look for css hacks to hide it...
In Angular Material, the default design of input directives is for the content within <label> to be displayed in the input element until the user enters some input, at which point it will float above the input element, as seen in all examples here.
Is there any way to force the labels to float above the input box at all times instead, even when no data has been entered?
I think the css class md-input-has-placeholder is what you need:
<md-input-container class="md-input-has-placeholder">
<label>Name</label>
<input type="text"/>
</md-input-container>
Plunker example here
Hope it helps.
The is an official feature for that: floatLabel="always"
The floatLabel property of can be used to change this default floating behavior. It can set to never to hide the label instead of float it when text is present in the form field control. It can be set to always to float the label even when no text is present in the form field control. It can also be set to auto to restore the default behavior.
<mat-form-field floatLabel="always">
<mat-label>Both a label and a placeholder</mat-label>
<input matInput [(ngModel)]="model.value">
</mat-form-field>
source, see the official form-field documentation
With Md-select this worked for me:
<md-input-container style="width: 200px;" md-input-has-placeholder>
<placeholder>Snack Types </placeholder>
<md-select ng-model="selectedOption">
<md-option ng-repeat="item in snacks" >
{{item.name}}
</md-option>
</md-select>
</md-input-container>
For the md-select element, occupy the following:
<md-input-container class="md-input-has-placeholder">
<label md-no-float="true" class="md-required">Snack Types</label>
<md-select ng-model="$ctrl.selection" ng-required="true" md-no-asterisk>
<md-option ng-value="option.id" ng-repeat="option in $ctrl.selection">{{ opcion.value}}</md-option>
</md-select>
</md-input-container>
I used the following CSS to change the label when data is being entered and highlight the other fields:
md-input-container:focus-within > label[class~="md-required"]{
transform: scale(1);
font-weight: bold;
}
And the following CSS so that the asterisk is always in color (you can put the color you want):
md-input-container.md-default-theme:not(.md-input-focused):not(.md-input-invalid) label.md-required:after, md-input-container:not(.md-input-focused):not(.md-input-invalid) label.md-required:after{
color: rgb(255,87,10);
}
We add Struts errors and messages using ActionSupport.addActionError(...) and addActionMessage(...) and then output the results using <actionerror class="x"/> and <actionmessage class="x"/>.
When these tags output the messages they output in the form: <ul><li><span class="x">msg</span></li></ul>
As you can see you can specify the css class (in this example 'x') to apply formatting. Problem is that we want to apply the margin-top and margin-bottom css properties and you can't use these properties (I gather) with <span> elements - only with <div> elements.
So is there anyway you can get these Struts tags to output error/message using a <div> instead of a <span>?
Thanks.
Update:
As per the answer/workaround below, I just enclosed the struts tag within a div:
<div class="error-status">
<s:actionerror cssClass="error"/>
<s:actionmessage cssClass="status" />
</div>
The error-status CSS class set the properties on the LI:
.error-status LI { MARGIN-TOP: 5px; MARGIN-BOTTOM: 5px; display: block; }
.error { COLOR: red }
.status { color: #0066CC }
You can apply margin to spans if you also apply display:block.
But the optimal solution is to apply margin to the li elements.