简体   繁体   English

风格ng-bootstrap手风琴与CSS

[英]Style ng-bootstrap accordion with css

I have an Angular 2 component with an accordion, which I added using ng-bootstrap. 我有一个带手风琴的Angular 2组件,我使用ng-bootstrap添加了它。 Everything works fine functionally, however the styles that I try to load using the .card, .card-header, .card-block classes that the compiled accordion elements have, those styles do not get applied to the elements at all. 一切都在功能上运行良好,但是我尝试使用编译的折叠元素具有的.card, .card-header, .card-block.card, .card-header, .card-block样式,这些样式根本不会应用于元素。

These classes are set by ng-bootstrap directly, when transforming the accordion into div's. 当将手风琴转换为div时,这些类由ng-bootstrap直接设置。

I apply my own css via a styles.scss file that I link to the components TypeScript file. 我通过一个链接到组件TypeScript文件的styles.scss文件来应用我自己的css。 When everything is rendered, my styles appear in the <style></style> tags in the header of the html output. 渲染完所有内容后,我的样式将出现在html输出标题中的<style></style>标记中。 It looks like this. 看起来像这样。

    <style>
        [_nghost-xfh-23]   .card[_ngcontent-xfh-23] {
          border: none; }

    [_nghost-xfh-23]   .card-header[_ngcontent-xfh-23] {
      margin-top: 0.75em !important;
      border: 1px solid rgba(0, 0, 0, 0.125); }

    [_nghost-xfh-23]   .card-block[_ngcontent-xfh-23] {
      text-align: left; }
    </style>

The styles.scss looks like this: styles.scss看起来像这样:

:host .card {
  border: none;
}

:host .card-header {
  margin-top: 0.75em !important;
  border: 1px solid rgba(0, 0, 0, 0.125);
}

:host .card-block {
  text-align: left;

}

My guess is that Angular 2 is trying to apply the styles (during compilation), but creates the div's with the said classes afterwards, making it impossible to apply the styles to the elements. 我的猜测是Angular 2正在尝试应用样式(在编译期间),但之后用所述类创建div,使得无法将样式应用于元素。

Im restrained to edit the bootstrap.css directly or create an other global css file. 我克制直接编辑bootstrap.css或创建其他全局css文件。 I'm hoping there is a way to reapply the css styles after the component is loaded or some other means to style ng-bootstrap accordions. 我希望有一种方法可以在加载组件之后重新应用css样式,或者使用其他方法来设置ng-bootstrap手风琴的样式。

Hope my problem makes sense, regards Sy 希望我的问题有道理,对Sy说

As @ChristopherMoore said in his comment, it was a problem due to Shadow DOM. 正如@ChristopherMoore在评论中所说,由于Shadow DOM,这是一个问题。 Adding /deep/ fixed it. 添加/deep/修复它。 Here the updated functional code. 这里是更新的功能代码。

  /deep/ .card {
  border: none;
}

/deep/ .card-header {
  margin-top: 0.75em !important;
  border: 1px solid rgba(0, 0, 0, 0.125);
}

/deep/ .card-block {
  text-align: left

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

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