简体   繁体   English

如何覆盖angular2-mdl的默认CSS?

[英]How do I override angular2-mdl's default CSS?

I'm making good progress with my Angular2 app, thanks again for clarifying how to use the scss files from MDL (answered here ). 我正在使用我的Angular2应用程序取得良好进展,再次感谢您澄清如何使用MDL中的scss文件( 在此处回答)。

However, I'm having trouble now overriding certain default styles set by MDL. 但是,我现在无法覆盖MDL设置的某些默认样式。 For example, I don't want the tabs in the tabs bar to use "justify-content: center", but rather "justify-content: flex-start", so the tabs are aligned left. 例如,我不希望选项卡栏中的选项卡使用“justify-content:center”,而是“justify-content:flex-start”,因此选项卡左对齐。

My initial approach was to try to override the property by supplying: 我最初的方法是尝试通过提供以下方式覆盖该属性:

.mdl-tabs__tab-bar {
    justify-content: flex-start;
}

directly in the scss file of the component containing the tabs. 直接在包含选项卡的组件的scss文件中。

But this won't work as Angular2 scopes my selectors, eg like this 但是这不会起作用,因为Angular2范围我的选择器,例如像这样

.mdl-tabs__tab-bar[_ngcontent-eph-22]

and so they never apply. 所以他们从不申请。 So what's the proper way to override MDL's default styles? 那么什么是覆盖MDL默认样式的正确方法呢? Do I have to put everything I want to override in the global styles.sass? 我是否必须在全局styles.sass中放置我想要覆盖的所有内容?

Thanks in advance for any help! 在此先感谢您的帮助!

By default angular 2 component are set with encapsulation: ViewEncapsulation.Emulated which will append unique component attribute to component style. 默认情况下,使用encapsulation: ViewEncapsulation.Emulated设置angular 2组件encapsulation: ViewEncapsulation.Emulated ,它将独特的组件属性附加到组件样式。 So that its styles can be specific to the component. 因此它的样式可以特定于组件。

We can avoid adding the unique component attribute to the styles by setting encapsulation: ViewEncapsulation.None in component meta data. 我们可以通过在组件元数据中设置encapsulation: ViewEncapsulation.None来避免将独特的组件属性添加到样式中。

import {ViewEncapsulation} from '@angular/core';

@Component({
  templateUrl: 'component.template.html',
  styleUrls: [`component.style.scss`],
  encapsulation: ViewEncapsulation.None
})

Now set the following in your scss file. 现在在scss文件中设置以下内容。

.mdl-tabs__tab-bar {
    justify-content: flex-start;
}

it will applied for all the occurrences of elements with mdl-tabs__tab-bar class. 它将应用于mdl-tabs__tab-bar类的所有元素。

As far as I know, you can override using the /deep/ selector (or >>> ). 据我所知,您可以使用/deep/ selector(或>>> )覆盖。 You have to add this css to the styles of your AppComponent . 您必须将此css添加到AppComponent的样式中。

:host /deep/ .mdl-tabs__tab-bar {
    justify-content: flex-start;
}

If you add it to the AppComponent , then where ever you add the tabs, it will have this style overridden. 如果将其添加到AppComponent ,那么在添加选项卡的位置,它将覆盖此样式。 If you only want to override it for certain components, you add this css to that component. 如果您只想为某些组件覆盖它,则将此css添加到该组件。

read more 阅读更多

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

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