css - 无法用我的 css 属性覆盖角度材质主题
问题描述
我正在使用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 body
仅应用第一个属性(其余属性被删除)
我尝试了多种变体来解决这个问题,但都没有奏效(比如使用重要或更改导入主题的顺序)
那么在材料主题和用户样式表中更改小东西的正确方法是什么?(例如应用此英语规则)
我的styles.scss如下,它只适用于覆盖Material 主题的颜色和字体不起作用:
@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;
}
更新1:
请查看这个 stackblitz 示例,其中的问题非常清楚
1-我期望带有阿拉伯语类的封闭 div应更改包含的材料表单字段的字体和颜色,但它不会(如 Sushi 的第一个字段,字体未更改)
这非常重要,因为所有元素都包含在带有 ngClass 的 div 中,其中包含英语或阿拉伯语,并且我想将该类应用于所有包含的元素,包括材料表单字段
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;
}
或者使用简写声明设置字体属性,如修改后的示例中所示:
.English{
margin: 0;
/* background: #fff; */
font: normal 16px/20px 'Pacifico', cursive;
color: green;
text-shadow: 0 0 0 #0078be;
}
编辑
要回答您的评论/编辑问题:
我在计算值中找到了字体,但它被删除而不应用,只有当我专门在 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
}
标签颜色相同。
使用 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
}
但我认为这不是很好。更好的解决方案是创建两个自定义主题,每种语言一个,并在那里设置颜色和字体。这样,您的颜色和字体定义将自动应用于嵌套元素。 本指南是关于自定义主题的好读物。
编辑 2:角度材料主题
正如我之前的编辑中提到的,创建自定义主题可能是解决您问题的更好解决方案。角度材料中的自定义主题非常强大,但是您需要先进行一些设置。之后,只需几行即可调整颜色、字体等。
我创建了一个新的 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'
);
要应用这些字体配置,您需要将它们应用到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);
为了实现您的多主题设置,您可以将主题定义包装在 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);
}
现在,.Arabic
仅当您的 html 元素是具有.Arabic
该类的元素的后代时才应用主题。此外,我添加@include mat-core($arabic-typography);
了告诉主题引擎仅$arabic-typography
将$arabic-theme
.
完成样式的最后一步是定义自定义组件主题,根据需要设置 mat-form-fields 样式。现在只需几行:
@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);
}
}
@include custom-components-theme(<theme name>);
然后通过将行添加到每个主题定义,将此自定义组件主题添加到您的自定义主题中。(custom-components-theme 是一个小辅助功能。在 stackblitz 中了解更多信息)。
就是这样。如果您需要调整更多组件,您只需为该组件创建一个自定义主题并将其添加到 custom-components-theme mixin。
推荐阅读
- php - 与谷歌的身份验证它不起作用它重定向到 404 未找到
- jquery - 单击时未播放 Vimeo 嵌入视频
- python - PyGObject 无法识别 hdy.window
- github-actions - 如何从具有包含选项的构建矩阵中统一引用矩阵键?
- pytorch - Pytorch 尺寸变化
- scripting - 如何设置 KOFAX KTM Server 全局变量值,该值将在 Batch open 中初始化,在 IndependentCurrentPage 中更新并在 BatchClose 中使用?
- android - 如何在 Redmi note 8 设备中禁用 Xamarin 表单条目控制的复制/粘贴选项?
- blockchain - Substrate node-template private network without using default keys
- azure - 为 Azure 容器注册表 (ACR) 的专用终结点创建动态专用 dns 区域记录
- c# - API 控制器是否使用与 Blazor 组件不同的服务实例?