简体   繁体   English

如何覆盖导入的 scss 文件中的样式

[英]How to override style in imported scss file

My Jekyll blog's theme has a scss file with the following syntax related style in minima.scss :我的哲基尔博客的主题有以下语法相关风格SCSS文件minima.scss

.highlight {
  background: #fff;
  @extend %vertical-rhythm;

  .highlighter-rouge & {
    background: #eef;
  }

  .err   { color: #a61717; background-color: #e3d2d2 } // Error

// more stuff

I want to override the nested .highlight -> .err style only, but I don't want to set the color and bg-color attributes to anything specific, just the default.我只想覆盖嵌套的.highlight -> .err样式,但我不想将 color 和 bg-color 属性设置为任何特定的东西,只是默认值。 As in, I want it as if the .err style had never been defined in the first place.就像在,我希望它好像.err风格从来没有被定义过一样。

I consume the minima.scss file in a main.scss which (which is the the actual file included in the page), like so:我消耗minima.scss在文件main.scss它(这是包含在页面中的实际文件),像这样:

@import "minima";

.highlight {
    .err { ???? }
}

This makes it easy to add styles, easy to extend styles with new attributes, and easy to override specific attributes of styles (since I guess the second mention takes priority), but how do I "delete" style or elements?这使得添加样式、使用新属性轻松扩展样式以及覆盖样式的特定属性变得容易(因为我猜第二个提到的优先),但是我如何“删除”样式或元素呢?

@import "minima";

.highlight {
  .err {
    color: unset !important;
    background-color: unset !important;
  }
}

Should do the trick.应该做的伎俩。 You can read more about "unset" and "!important" .你可以阅读更多关于"unset""!important" 的信息

将颜色设置为inherit将使其采用它的父元素颜色,在这种情况下看起来是白色

The answer that's marked correct is not the recommended way at all.标记为正确的答案根本不是推荐的方法。 In fact, it's a terrible way to override a style.事实上,这是一种覆盖样式的糟糕方法。

The correct way to do this:执行此操作的正确方法:

@import "minima";

.highlight {
  &.err {
    color: unset;
    background-color: unset;
  }
}

HTML: HTML:

<div class="highlight err"></div>

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

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