您可以将此内容添加到您的CSS中。
::ng-deep .mat-form-field-wrapper{
margin-bottom: -1.25em;
}
注意: 当您去除空格时,无法正确放置
<mat-hint>hint</mat-hint>
或<mat-error>error</mat-error>
。错误和提示将进入表单字段。
在不使用::ng-deep
(对于Angular 8)的情况下
关闭您更改填充内容的组件的封装。
您可以通过以下方式完成此操作:
import {Component,ViewEncapsulation} from '@angular/core';
@Component({
selector: 'example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
encapsulation : ViewEncapsulation.None,
})
export class ExampleComponent {}
将您想要样式化的组件包裹在自定义类中。这样它就不会影响到其他mat-form-field
组件。
现在,让我们将其用my-form-field
类包装起来。
<mat-form-field class="my-form-field">
<input matInput placeholder="Input">
</mat-form-field>
.my-form-field .mat-form-field-wrapper {
margin-bottom: -1.25em;
}
您可以将这些CSS添加到全局样式表中,而无需关闭视图封装。但更优雅的方法是上面提到的那种方法。
对于任何在2023年阅读此文的人:Angular 15现在为表单字段添加了一个新属性:subscriptSizing。
如果您将subscriptSizing="dynamic"
添加到mat-form-field
中,它将删除空格,直到需要显示错误或提示时才会展开。这会导致布局移位,但根据您的用例,这可能比手动添加边距更好,如之前所建议(在版本14之前是必需的)。
有关参考,请参见https://material.angular.io/components/form-field/api#SubscriptSizing
您还可以通过MAT_FORM_FIELD_DEFAULT_OPTIONS注入令牌全局指定此选项的默认值;请参见https://material.angular.io/components/form-field/api#MatFormFieldDefaultOptions
// in main.ts (Angular 15+)
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
bootstrapApplication(AppComponent, {
providers: [
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: {
subscriptSizing: 'dynamic'
}
}
],
});
app.module.ts
中进行更改比在main.ts
中更加便携和安全。除非涉及到环境变更、资源、全局样式、TypeScript和Angular设置等,否则您的应用程序相关更改应全部发生在src/app/
文件夹内。在升级过程中,Angular可能会更改main.ts
的结构,导致您丢失提供者的设置。 - undefined尝试以下操作:
<mat-form-field style="margin-bottom: -1.25em">
(您可以在此处关注有关此额外底部空间的讨论:https://github.com/angular/components/issues/7975)
:host ::ng-deep .mat-form-field-wrapper{
margin: 0 !important;
padding: 0;
}
另外,我正在使用appearance="outline",对于其他外观,可能需要更改其他CSS类和属性,因为它可能具有其他元素。
我的解决方案是使用一个额外的类。
HTML:
<mat-form-field class="no-padding">
<input type="number" matInput placeholder="Speed" [ngModel]="speed"
(ngModelChange)="onSpeedChange('speed', $event)"/>
</mat-form-field>
SCSS:
.no-padding {
.mat-form-field-wrapper {
padding-bottom: 0 !important;
}
}
很不幸,!important关键字是必需的,否则它将只会使用默认值。
styles.scss
中而不是 my.component.scss
中,才能让它在 Angular 11 中正常工作。 - brt或者简单地说:
HTML
<mat-form-field class="no-bottom">
...
</mat-form-field>
css
.no-bottom {
margin-bottom: -1.25em !important;
}
我用 Angular 10 进行测试,这个可以正常工作:
:host ::ng-deep .mat-form-field-wrapper{
margin: 0 !important;
padding: 0;
}
<mat-form-field appearance="outline" class="no-wrapper">
<input matInput>
</mat-form-field>
在你的CSS中添加类名:
:host ::ng-deep .no-wrapper .mat-form-field-wrapper{
margin: 0 !important;
padding: 0;
}
<mat-form-field appearance="standard" class="compact-input">
<mat-label>Test</mat-label>
<input matInput>
</mat-form-field>
以下是相关的SCSS代码:
.compact-input {
.mat-form-field-flex {
padding-top: 0;
}
.mat-form-field-wrapper {
padding-bottom: 0;
}
.mat-form-field-underline {
bottom: 0;
}
}
尽管仅测试了 standard
外观。
别忘了在组件中添加 encapsulation: ViewEncapsulation.None
。
如果你把 SCSS 放在全局样式中,可能需要添加一些 !important
。
我可以通过在style.scss中使用以下css来移除mat-form-filed的底部空间:
Original Answer翻译成"最初的回答"
.mat-form-field-wrapper{
padding-bottom: 10px;
}
.mat-mdc-form-field-subscript-wrapper {
display: none;
}
subscriptSizing=dynamic
选项来实现相同的效果。请参阅 https://dev59.com/uFQJ5IYBdhLWcg3wg2NQ#75107066 。 - JSON Derulomat-form-field
(或其他类似的东西)。在这种情况下,您无法设置您无法直接访问的元素的属性。因此,我不会称这个解决方案为“差劲”。有时这是唯一的解决方案。 - Alexandermat-form-field
(或其他类似的东西)。在这种情况下,您无法设置您无法直接访问的元素的属性。所以我不会称这个解决方案为“差劲”。有时这是唯一的解决方案。 - undefined
.mat-form-field-wrapper
样式中没有 margin-bottom,即使它们有任何 margin-bottom 样式(默认情况下不是 !important),因为我们使用自定义类包装mat-form-field
,例如在这里是 'my-form-field'。 - Akhi Akl.mat-form-field-wrapper
没有 CSS 样式,而是它没有任何margin-bottom
样式的 CSS。 而且我真的很困惑为什么它在 Angular 10 中不起作用。我已经使用了最新的 Angular Material。请检查这个 stackblitz,它使用了最新的 Angular Material。 - Akhi Akl