【问题标题】:Why will height not change for specific mat-form-field? (Angular 6)为什么特定 mat-form-field 的高度不会改变? (角度 6)
【发布时间】:2020-03-27 14:47:48
【问题描述】:

目标:减小特定 mat-form-field 的高度(但仍将其保持在原位,在“请登录”的右侧)。 它应该在文本值和下拉箭头周围占用最少的空间。

目前看起来是这样的:

我希望它周围框上的线条更接近单词 English 和向下箭头。 该框是通过添加以下内容创建的:appearance="outline"

问题:我能够成功更改宽度,但不能更改高度。

我的尝试

HTML

<div class="title-container">
    <mat-card-title class="text-center" i18n="@@MoVeSeLo_H1_1">
        Please Sign In
    </mat-card-title>
    <form [formGroup]="_siteForm">
        <mat-form-field class="language-field" appearance="outline">
            <mat-select (selectionChange)="doSomething($event)" [value]="languages[i]">
                <mat-option *ngFor="let language of languages" [value]="language">
                    {{language.viewValue}}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </form>
</div>

CSS

.title-container {
    position: relative;
    margin-top: 8px;
}

.language-field {
    position: absolute;
    right: 0;
    top: 0;
    margin-top: -16px;
    width: 115px;
    height:40px;
    padding-top: -1.84375em;    
}

【问题讨论】:

    标签: html css angular


    【解决方案1】:

    兄弟,我找到你了,一些简单的解决方案是:

    将此添加到您的 CSS 中

    ::ng-deep .mat-form-field-appearance-outline .mat-form-field-flex {
    height: 40px !important
    }
    
    ::ng-deep .mat-form-field-infix {
    padding-top: 1px !important;
    }

    解释

    您可以在此处随意控制 mat-form-field 的高度。

    ::ng-deep .mat-form-field-appearance-outline .mat-form-field-flex {
        height: 40px !important
        }

    通过这个波纹管,您可以控制您的 mat-form-field 占位符填充以匹配您放置的新高度。

     ::ng-deep .mat-form-field-infix {
        padding-top: 1px !important;
        }

    让我知道你的作品

    【讨论】:

    • 感谢@Thiago 的回复和解释。它似乎正在工作,但我收到警告:“::ngdeep is not a valid pseudo-element”,你知道为什么会这样吗?
    • 实际上我注意到这会影响页面上的其他所有内容,其他所有内容都变小了。甚至其他页面变得更小,没有被触及。
    • @angleUr 你只需要在你想要的页面的 .css 上使用它。否则它会真正影响所有项目,是的。页面的所有 .mat-form-field 都会改变
    • 是的,我只在那个 css 页面上使用了它,但它最终影响了其他页面。是的,我只希望它影响一个垫子表单字段,而不是其他任何一个。它产生了奇怪的影响。我会多玩一点。
    【解决方案2】:

    https://pxgeqykgapgy.angular.stackblitz.io/中的代码

    HTML

    <form class="example-container" [formGroup]="options" [style.fontSize.px]="getFontSize()">
        <mat-card-title class="text-center">
            Please Sign In
        </mat-card-title>
        <mat-form-field class="matselect" [color]="options.value.color">
            <mat-select formControlName="color">
                <mat-option value="primary">English</mat-option>
                <mat-option value="accent">Chinees</mat-option>
                <mat-option value="warn">Hindi</mat-option>
            </mat-select>
        </mat-form-field>
    
    </form>
    

    CSS

    .example-container {
      display: flex;
    }
    
    .matselect{
        width: 115px !important;
        border: 1px solid;
        border-radius: 6px;
        position: absolute;
        right: 10px;
        top: 10px;
    }
    
    ::ng-deep .matselect .mat-form-field-underline {
        display: none !important;
    }
    

    【讨论】:

    • 谢谢,我试过你的 CSS 但它没有改变高度。
    猜你喜欢
    • 2020-10-29
    • 2020-05-15
    • 2019-07-12
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2022-07-22
    • 2021-12-06
    相关资源
    最近更新 更多