【问题标题】:Overwrite PrimeNG step component styling覆盖 PrimeNG 步骤组件样式
【发布时间】:2019-02-16 00:35:53
【问题描述】:

我正在使用

<p-steps [model]="items" [(activeIndex)]="activeIndex" [readonly]="false"></p-steps>

在我的 Angular 组件中。在我的组件的样式表中,我试图设置 p 步骤的样式,但没有运气。当我直接在浏览器的开发人员工具中更改样式时,它可以工作。我什至尝试用 Angular 的

覆盖样式
:host ::ng-deep

但它没有工作。我希望步骤垂直对齐,我不想要边框,我希望步骤编号为浅灰色,所选步骤编号为浅灰色。我想要的是以下内容:

:host ::ng-deep .ui-widget, .ui-widget * {
  float: none !important;
}

:host ::ng-deep .ui-steps {
  border: none !important;
}

:host ::ng-deep .ui-steps .ui-steps-item .ui-state-highlight .ui-steps-number {
  background-color: #757575 !important;
}

:host ::ng-deep body .ui-steps .ui-steps-item .ui-menuitem-link .ui-steps-number {
   background-color: #bdbdbd !important;
}

我也设置了

encapsulation: ViewEncapsulation.None

在我的组件中。

【问题讨论】:

  • 让我看看尝试解决您的问题。我基于 PrimeNG 创建客户组件。我很惊讶 ng-deep 没有工作。仅供参考,删除 viewEncapsulation 行。使用它不是一个好习惯
  • 你测试过我的答案吗?如果是并且有效,请点赞并将其标记为正确答案
  • 嗨,thanx,我会测试它并告诉你。但是,我通过稍微改变@fatemefazli 的答案来实现它。

标签: css angular primeng


【解决方案1】:

我的建议永远不会覆盖任何第三方库的 CSS。如果你想覆盖任何元素的 CSS 属性,首先使用你自己的类。然后添加 CSS 属性。使用CSS specificity 的规则,它将轻松覆盖 CSS 属性,而无需使用 !important 和任何其他 hack。

我做了什么来解决这个问题,我添加了自己的类 customestepper 并覆盖了 CSS 属性,如下所示:

<p-steps [model]="items" class="customstepper"></p-steps>

然后在 styles.css

.customstepper .ui-state-highlight{
    background: #343a40;;

}
.customstepper .ui-steps .ui-steps-item.ui-state-highlight .ui-menuitem-link {
    color:#fff;
}

演示: Click to see the demo Image

【讨论】:

  • Thanx @DirtyMind,但是我必须使用“封装:ViewEncapsulation.None”。否则它将无法正常工作。这是使用“!important”的更好解决方案吗?并且对于以另一种颜色突出显示所选步骤而不是其余步骤,它仍然无法正常工作。只有当我使用 !important
  • 使用自己的类是覆盖 CSS 的正确方法。我不认为使用 :host ::ng-deep 覆盖 CSS 是一个好方法。
  • @YourReflection 是的,如果没有任何效果,那么我们可以使用 !important 。
【解决方案2】:

这是解决方案。你错过了::ng-deep

::ng-deep .ui-widget, ::ng-deep.ui-widget * {
  float: none !important;
  color: red;
}
.ui-steps {
  color: red;
  border: none;
}
.ui-steps .ui-steps-item .ui-state-highlight .ui-steps-number {
  background-color: #757575;
}

.ui-steps .ui-steps-item .ui-menuitem-link .ui-steps-number {
   background-color: red;
}

https://stackblitz.com/edit/primeng-template-jr2vaa

  1. 避免encapsulation: ViewEncapsulation.None
  2. 避免使用 !important
  3. 开始使用 SCSS
  4. 不要将任何自定义代码放入 你父母的scss

【讨论】:

  • 我测试了您的解决方案,但它不起作用。它只是采用标准样式,就好像我什至没有任何带有我自己样式的 scss 文件一样。
  • @YourReflection 我添加了一张图片。这就是它应该看起来的样子?您是在 stackblitz 上测试它还是在您的环境中测试了我的解决方案?
  • 是的,它看起来像这样,并且没有运行按钮。之后我在我的组件中测试了你的样式,但没有成功。
【解决方案3】:

我是这样解决的(决定不使用垂直对齐):

body .ui-steps .ui-steps-item.ui-state-highlight .ui-steps-number {
 background-color: #757575 !important;
}
.ui-steps .ui-steps-item .ui-menuitem-link .ui-steps-number {
 background-color: #bdbdbd !important;
}
.ui-steps:before {
 border: none !important;
}

我还需要添加

encapsulation: ViewEncapsulation.None

在我的组件中。

另一种无需添加封装即可工作的解决方案是:

:host ::ng-deep .ui-steps .ui-steps-item.ui-state-highlight .ui-steps-number {
  background-color: #757575 !important;
}
::ng-deep .ui-steps .ui-steps-item .ui-menuitem-link .ui-steps-number {
  background-color: #bdbdbd !important;
}
::ng-deep .ui-steps:before {
  border: none !important;
}

不使用

!important

目前还不行。

【讨论】:

  • 很高兴你知道了。顺便说一句,您不需要 :host,您也应该尽可能避免使用 !importants。这将使您的代码更具可维护性
  • @PatricioVargas:我试过不使用 :host 和 !important。不幸的是,它没有用,所以我把它留在了:/
  • 呃,我希望我能提供更多帮助
【解决方案4】:

您可以添加 encapsulation: ViewEncapsulation.None 并在您的组件 css 中尝试:

.ui-widget, .ui-widget * {
  float: none !important;
  color: red;
}
.ui-steps {
  color: red;
  border: none !important;
}
.ui-steps .ui-steps-item .ui-state-highlight .ui-steps-number {
  background-color: #757575 !important;
}

.ui-steps .ui-steps-item .ui-menuitem-link .ui-steps-number {
   background-color: red !important;
}

或者删除encapsulation: ViewEncapsulation.None并将上面的css放到你的全局styles.css文件中。

第一种方式DEMO

第二种方式DEMO

【讨论】:

  • 使用encapsulation: ViewEncapsulation.None 也不是很好的做法,也把它放在全局css 中也不是很好的做法
  • 那你有什么建议@PatricioVargas?
猜你喜欢
  • 1970-01-01
  • 2017-03-01
  • 2019-08-05
  • 2021-11-04
  • 2019-02-22
  • 2021-02-14
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多