【问题标题】:Issue with CSS for mat-input and mat-form-fieldmat-input 和 mat-form-field 的 CSS 问题
【发布时间】:2019-02-05 07:11:23
【问题描述】:

我尝试做一个 stackblitz,但它不接受我的 <mat-form-field><mat-label> 标签而没有错误,我导入了所需的组件但它不起作用。

但是,如果我在此处发布代码,也许您仍然可以帮助我,我遇到了 CSS 问题,因为当我使用 <mat-form-field> 时,我不知道移动 <mat-label> 标记的目标是什么appearance="outline" 当标签在中心时,在input field 上方,我不能移动它,我希望它居中,但它在mat-form-field 的底部。

如果我调整 position:absolute 值,问题是,当点击发生时标签超出字段上方时,这些规则也会被应用,将浮动标签移动到错误的位置。

代码:

<div 
    id="test" 
    fxLayout="row" 
    fxLayoutGap="12px" 
    fxLayoutAlign="start center" 
    [class.hasFocus]="numberMatInput.value">
    <mat-form-field appearance="outline">
         <mat-label>KM</mat-label>
         <input matInput #numberMatInput [formControl]="numberInput">
    </mat-form-field>
</div>

和 CSS:

#test ::ng-deep .mat-form-field-appearance-outline {
  width: 65px;
  height: 45px;
  line-height: 1.725;
}

#test ::ng-deep .mat-form-field-appearance-outline .mat-form-field-flex {
  padding: 0px 10px 0px 10px;
}

#test ::ng-deep .mat-form-field-infix {
  padding: 3px 0 3px 0;
}

这是想要的结果:

所以基本上我需要在输入字段未被触摸时移动标签,并在它获得焦点时将其留在浮动上方的相同空间中。

对我来说,两者似乎只有一个规则,所以如果我移动未触及的标签,浮动的标签也会移动。

有人可以帮忙吗?谢谢你

【问题讨论】:

  • .mat-form-field-appearance-outline似乎不存在?
  • 它存在,它给出了 mat-form-field 的 with 和 height,这些类直接来自 chrome 开发工具,它们在我的 css 中工作

标签: css angular typescript angular-material


【解决方案1】:

尝试使用以下 css:

#test ::ng-deep .mat-form-field-hide-placeholder .mat-form-field-label-wrapper {
   text-align: center;
   top: -25px;
}

#test ::ng-deep .mat-form-field-should-float {
   text-align: center;
}

Demo

【讨论】:

  • 干得好@kboul thnks 反正
  • 谢谢@harkeshkumar。您是否也遇到过这个问题?
  • 非常感谢!我确实在您发布前 1 分钟找到了一个解决方案,但您的似乎更干净!我滥用 !important 并针对 3 个实例(内部标签、上面的标签和上面的标签,输入不为空),因为我做了更改,而是将浮动标签放在中心但在左角,你能帮我申请吗您对当前样式的解决方案? stackblitz.com/edit/angular-chtqjk-2qmksf
  • 只需将您的 css 替换为我的:stackblitz.com/edit/angular-chtqjk-gbkb4f。您还可以查看我在答案中包含的演示。
  • 我想我更喜欢它在标签的一侧!如果侧边的浮动标签推起来不是太复杂,否则这样就好了,反正非常感谢大家的帮助!
猜你喜欢
  • 2018-08-24
  • 2021-07-31
  • 2021-08-17
  • 2020-10-26
  • 2019-02-05
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
相关资源
最近更新 更多