Ahm*_*ssy 4 css angular-material2 angular angular6 angular-material-6
我使用Angular 6.0.8同Angular Material theme版本6.4.0
每当我尝试稍微修改材质主题时,它永远不会起作用,因为我总是发现材质主题属性会覆盖我的 CSS 属性,如下所示:
currentColor .mat-input-element (material theme)
initial input, textarea, select, button (user agent stylesheet)
#1072BA .English
#1072BA .English
#1072BA bodyRun Code Online (Sandbox Code Playgroud)
仅应用第一个属性(其余的被删除)
我尝试了多种变体来解决这个问题,但没有一个奏效(比如使用重要的或改变导入主题的顺序)
那么在 Material 主题和用户样式表中更改小东西的正确方法是什么?(例如,应用此英语规则)
我的styles.scss如下,它运行良好,只有覆盖颜色和字体等材质主题不起作用:
@import '~@angular/material/prebuilt-themes/indigo-pink.css';
/* You can add global styles to this file, and also import other style files */
/* prevent clicking in the wizard nav header */
.mat-horizontal-stepper-header{
pointer-events: none !important;
}
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(./assets/material_icons/material_icons.woff2) format('woff2');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}
.mat-radio-button{
margin: 10px;
}
.mat-checkbox{
margin-bottom: 10px;
}
.Done-numbers{
color: red;
font-weight: bold;
}
.finInfo-hint{
color: gray;
}
.finInfo-success{
color: green;
}
.Arabic {
font-family: "AXtManal" !important;
margin: 0;
/* background: #fff; */
font: normal 16px/20px Verdana;
color: #1072BA;
text-shadow: 0 0 0 #1072BA;
}
.English {
font-family: "Rotis" !important;
margin: 0;
/* background: #fff; */
font: normal 16px/20px Verdana;
color: #1072BA;
text-shadow: 0 0 0 #0078be;
}
Run Code Online (Sandbox Code Playgroud)
更新 1:
请查看这个stackblitz示例,问题在里面很清楚
1-我期望带有阿拉伯语类的封闭 div会更改所包含的材料表单字段的字体和颜色,但它不会(就像 Sushi 的第一个字段一样,字体未更改)
这非常重要,因为所有元素都包含在一个带有 ngClass 的 div 中,该 ngClass 有英语或阿拉伯语,我想将该类应用于所有包含的元素,包括材料表单字段
2-如何将所有表单标签的颜色从蓝色更改为红色或绿色?
请查看这个最小示例(我必须调整字体系列才能使此示例正常工作)。
您正在覆盖一些字体样式定义。分别定义您的 font-family、-size 和 -weight,就像在以下调整后的.Arabic定义中一样:
.Arabic {
font-family: 'Shadows Into Light', cursive;
margin: 0;
/* background: #fff; */
font-size: 16px/20px;
font-weight: normal;
color: red;
text-shadow: 0 0 0 #1072BA;
}
Run Code Online (Sandbox Code Playgroud)
或者使用速记声明设置字体属性,如这个修改后的示例:
.English{
margin: 0;
/* background: #fff; */
font: normal 16px/20px 'Pacifico', cursive;
color: green;
text-shadow: 0 0 0 #0078be;
}
Run Code Online (Sandbox Code Playgroud)
回答您的评论/编辑的问题:
我在计算值中找到了字体,但它被删除而不应用,只有当我专门在 mat-form 字段上应用该类时才应用它
.mat-form-field如果你想改变它,你必须明确地应用这个类,因为在 prebuilt-themes/indigo-pink.css 中它被明确定义为
.mat-form-field{
font-size:inherit;
font-weight:400;
line-height:1.125;
font-family:Roboto, "Helvetica Neue",sans-serif
}
Run Code Online (Sandbox Code Playgroud)
标签颜色相同。
使用 CSS,您始终必须至少与现有的样式定义一样具体。
你可以这样做:
.Arabic .mat-form-field,.mat-form-field-appearance-legacy .mat-form-field-label,
.mat-input-element,.mat-form-field.mat-focused .mat-form-field-label{
font-family: 'Shadows Into Light', cursive;
margin: 0;
/* background: #fff; */
font-size: 16px/20px;
font-weight: normal;
color: red;
text-shadow: 0 0 0 #1072BA;
}
.Arabic .mat-form-field.mat-focused .mat-form-field-ripple{
background-color: red
}
Run Code Online (Sandbox Code Playgroud)
但我认为这不是很好。更好的解决方案是创建两个自定义主题,每种语言一个,并在那里设置颜色和字体。这样,您的颜色和字体定义将自动应用于嵌套元素。 本指南是关于自定义主题的好读物。
正如我在之前的编辑中提到的,创建自定义主题可能是解决您问题的更好方法。角度材料中的自定义主题非常强大,但是您需要先进行一些设置。之后,只需几行即可调整颜色、字体等。
我创建了一个新的 stackblitz,您可以在其中看到默认主题和两个自定义主题的设置以及材料表单字段的颜色和字体的调整。我已经评论了每个部分,但我也会在这里简要解释一下:
你的一般结构styles.scss应该是:
导入后,@import '~@angular/material/theming';您可以开始调整字体(如果您愿意)。为此,您只需要定义一个新的mat-typography-config例如:
$arabic-typography: mat-typography-config(
$font-family: '"Shadows Into Light", cursive'
);
$english-typography: mat-typography-config(
$font-family: '"Pacifico", cursive'
);
Run Code Online (Sandbox Code Playgroud)
要应用这些字体配置,您需要将它们转移到mat-core-mixin 中,它负责其余的工作:@include mat-core($english-typography);. 现在,如果您只想将 font-config 应用于特定的自定义主题,则必须在主题定义中添加 font-config。
主题由以下结构定义:使用 mat-light-theme-mixin 定义所需的主色和主题。要应用主题,请使用@include angular-material-theme(<theme name>);
$app-primary: mat-palette($mat-indigo);
$app-accent: mat-palette($mat-amber, A200, A100, A400);
$app-warn: mat-palette($mat-red);
$app-theme: mat-light-theme($app-primary, $app-accent, $app-warn);
@include angular-material-theme($app-theme);
Run Code Online (Sandbox Code Playgroud)
要实现多主题设置,您可以将主题定义包装在 CSS 类名称中,如下所示:
.Arabic {
$arabic-primary: mat-palette($mat-orange);
$arabic-accent: mat-palette($mat-blue, A200, A100, A400);
$arabic-warn: mat-palette($mat-red);
$arabic-theme: mat-light-theme($arabic-primary, $arabic-accent, $arabic-warn);
@include mat-core($arabic-typography);
@include angular-material-theme($arabic-theme);
}
Run Code Online (Sandbox Code Playgroud)
现在,.Arabic仅当您的 html 元素是具有.Arabic该类的元素的后代时才应用主题。此外,我还添加了@include mat-core($arabic-typography);它告诉主题引擎$arabic-typography将$arabic-theme.
完成样式的最后一步是定义自定义组件主题,该主题可根据需要对 mat-form-field 进行样式设置。这现在是几行的问题:
@mixin my-custom-component($theme) {
// retrieve variables from current theme
$primary: map-get($theme, primary);
// style element
mat-form-field, .mat-form-field-appearance-legacy .mat-form-field-label {
color: mat-color($primary);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,通过将行添加@include custom-components-theme(<theme name>);到每个主题定义,将此自定义组件主题添加到您的自定义主题中。(custom-components-theme 是一个小辅助函数。在 stackblitz 中阅读更多相关信息)。
就是这样。如果需要调整更多组件,只需为该组件创建一个自定义主题并将其添加到 custom-components-theme mixin 中即可。
| 归档时间: |
|
| 查看次数: |
9531 次 |
| 最近记录: |