简体   繁体   English

如何在 Angular 2 中完全着色 mat-card-header 背景?

[英]How to get mat-card-header background fully colored in Angular 2?

I have made a contact form in Angular 2. I would make a colored top bar我已经在 Angular 2 中制作了一个联系表格。我会制作一个彩色的顶栏

<md-card class="mdcardcontact">
  <md-card-header style="background-color: black;   width:100%">
  </md-card-header>
  <div>
    <md-card-content>
      <form [formGroup]="form" class="form">
        <div>
          <md-input-container class="full-width">
            <input mdInput type="text" formControlName="name" placeholder="Votre nom">
          </md-input-container>

        </div>
       </form>
    </md-card-content>
  </div>
</md-card>

That's what I get :这就是我得到的:

标题前

I would like something like the yellow rectangle but using md-card-header :我想要类似黄色矩形但使用 md-card-header 的东西:

标题后

Put padding:0 on the mat-card.padding:0放在垫卡上。 And to correct the padding, add margin on mat-content.并且要更正填充,请在 mat-content 上添加边距。 Use specific classes names to style he card:使用特定的类名称来设计卡片的样式:

.mat-card-header{
  background-color:red !important;
  padding:5px !important;
}

 .mat-card{
  padding:0 !important;
}

.mat-card-content{
  padding:5px !important;
}

Demo 演示


Old Answer:旧答案:


I would suggest four options.我建议四个选项。

1. Using ::ng-deep . 1. 使用::ng-deep

Use the /deep/ shadow-piercing descendant combinator to force a style down through the child component tree into all the child component views.使用 /deep/ shadow-piercing 后代组合器将样式强制向下穿过子组件树进入所有子组件视图。 The /deep/ combinator works to any depth of nested components, and it applies to both the view children and content children of the component. /deep/ 组合器适用于嵌套组件的任何深度,它适用于组件的视图子项和内容子项。 Use /deep/, >>> and ::ng-deep only with emulated view encapsulation.仅在模拟视图封装中使用 /deep/、>>> 和 ::ng-deep。 Emulated is the default and most commonly used view encapsulation. Emulated 是默认的也是最常用的视图封装。 For more information, see the Controlling view encapsulation section.有关更多信息,请参阅控制视图封装部分。 The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools.不推荐使用阴影穿透后代组合器,并且正在从主要浏览器和工具中删除支持。 As such we plan to drop support in Angular (for all 3 of /deep/, >>> and ::ng-deep).因此,我们计划放弃对 Angular 的支持(对于 /deep/、>>> 和 ::ng-deep 中的所有 3 个)。 Until then ::ng-deep should be preferred for a broader compatibility with the tools.在那之前 ::ng-deep 应该是首选,因为它与工具更广泛地兼容。

CSS: CSS:

::ng-deep .mat-card-header{
  background-color:red !important;
  padding:5px !important;
}

::ng-deep .mat-card{
  padding:0 !important;
}

::ng-deep .mat-card-content{
  padding:5px !important;
}

DEMO演示


2. Using ViewEncapsulation 2. 使用视图封装

... component CSS styles are encapsulated into the component's view and don't affect the rest of the application. ... 组件 CSS 样式被封装到组件的视图中,不会影响应用程序的其余部分。 To control how this encapsulation happens on a per component basis, you can set the view encapsulation mode in the component metadata.要控制这种封装如何在每个组件的基础上发生,您可以在组件元数据中设置视图封装模式。 Choose from the following modes: .... None means that Angular does no view encapsulation.从以下模式中选择: .... None 表示 Angular 不进行视图封装。 Angular adds the CSS to the global styles. Angular 将 CSS 添加到全局样式中。 The scoping rules, isolations, and protections discussed earlier don't apply.前面讨论的范围规则、隔离和保护措施不适用。 This is essentially the same as pasting the component's styles into the HTML.这本质上与将组件的样式粘贴到 HTML 中相同。

None value is what you will need to set material style from your component. None 值是您从组件设置材料样式所需的值。 Angular material uses mat-select-content as class name for the select list. Angular 材质使用mat-select-content作为选择列表的类名。 So can set on the component's selector:所以可以在组件的选择器上设置:

Typscript:打字稿:

  import {ViewEncapsulation } from '@angular/core';
  ....
  @Component({
        ....
        encapsulation: ViewEncapsulation.None
 })  

CSS CSS

.mat-card-header{
  background-color:red !important;
  padding:5px !important;
}

.mat-card{
  padding:0 !important;
}

.mat-card-content{
  padding:5px !important;
}

DEMO演示


3. Setting styles in style.css 3.在style.css中设置样式

style.css样式文件

.mat-card-header{
  background-color:red !important;
  padding:5px !important;
}

.mat-card{
  padding:0 !important;
}

.mat-card-content{
  padding:5px !important;
}

DEMO演示


4. Using inline style 4. 使用内联样式

HTML HTML

<mat-card style="padding:0">
    <mat-card-header style="background-color:red;padding:5px}">
        <mat-card-title>Title</mat-card-title>
        <mat-card-subtitle>Subtitle</mat-card-subtitle>
    </mat-card-header>
    <mat-card-content style="padding:5px !important;">
        Body text
    </mat-card-content>
</mat-card>

DEMO演示

I would try adding我会尝试添加

position: absolute; top: 0; 

To the header style tag where you specified the background color.到您指定背景颜色的标题样式标签。 The md-card component has a default padding value for the whole card, so you'll have to position the header absolutely to ignore that padding value. md-card 组件具有整个卡片的默认填充值,因此您必须绝对定位标题以忽略该填充值。 But it might cause other position changes to the rest of the card elements if you do it like that.但是,如果您这样做,可能会导致其他卡片元素的其他位置发生变化。 If you were going to use that style a lot, I would just make your own version.如果你打算经常使用这种风格,我会制作你自己的版本。 Here's the source, https://github.com/angular/material2/tree/master/src/lib/card这是源, https://github.com/angular/material2/tree/master/src/lib/card

The /deep/ combinator is slated for deprecation in Angular , so its best to do without it. /deep/ 组合器将在 Angular 中弃用,因此最好不要使用它。

Unfortunately, Angular Material is highly specified, which makes it very difficult to override without using /deep/不幸的是,Angular Material 是高度指定的,这使得在不使用 /deep/ 的情况下很难覆盖

The best way I have found is to use an ID, which allows you a higher specificity compared to the default Angular Material styles.我发现的最好方法是使用 ID,与默认的 Angular Material 样式相比,它允许您具有更高的特异性。

<div id="my-card">
  <mat-card>
    <mat-card-title> Title
    </mat-card-title>
    <mat-card-content> Content </mat-card-content>
  </mat-card>
</div>

Then, the 'my-card' id can be used to target this card in the global.scss file.然后,'my-card' id 可用于在 global.scss 文件中定位此卡。 It can't be targeted from the component.scss without breaking your view encapsulation, which is probably something you don't want to do.在不破坏您的视图封装的情况下无法从 component.scss 中定位它,这可能是您不想做的事情。

The nice thing, is that everthing within that with the id of 'my-card' can now be easily targeted, including material animations, which are hard to target any other way.好消息是,id 为“my-card”的所有内容现在都可以轻松定位,包括很难以任何其他方式定位的材质动画。

If you have menus, buttons, etc. they can all be styled using .scss and without using important!如果您有菜单、按钮等,它们都可以使用 .scss 进行样式设置,而无需使用important!

global.scss global.scss

#chart-card {
 .mat-card-header{
   background-color:red;
   padding:5px;
 }
 .mat-card{
   padding:0;
 }
 .mat-card-content{
   padding:5px;
 }
}

I hope the Angular Material team pulls back their specificity in the future because currently there's no easy way to override their defaults.我希望 Angular Material 团队在未来收回他们的特殊性,因为目前没有简单的方法来覆盖他们的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM