简体   繁体   English

Sass 扩展 / CSS 覆盖一些属性

[英]Sass extend / CSS overwrite some properties

I am trying to create a small custom icon libary to use with my website following this :我正在尝试创建一个小的自定义图标库以在我的网站上使用以下内容:

It works for single icon but I don't want to write it for every icon i am going to use.它适用于单个图标,但我不想为我要使用的每个图标编写它。 So I tried to use SASS / SCSS to do something easier:所以我尝试使用 SASS / SCSS 做一些更简单的事情:

 .icon {
    height: 4.5rem;
    width: 4.5rem;
    display: inline-block;
    background-size: contain;
    background-repeat: no-repeat;
}

.google-icon {
    background: url('../icons/icon.png');
    @extend icon;
}

and it generate this CSS:它生成这个 CSS:

.icon, .google-icon {
  height: 4.5rem;
  width: 4.5rem;
  display: inline-block;
  background-size: contain;
  background-repeat: no-repeat;
}

.google-icon {
  background: url("../icons/icon.png");
}

And it doesn't work, the background-size and background-repeat values are being overwriten, i don't know by what, but they don't apply, I can see the i element i've been using to insert the icon, and in thd dev tools i can see the image that I used but because this 2 properties being overwritten it doesn't show properly.而且它不起作用,背景大小和背景重复值被覆盖,我不知道是什么,但它们不适用,我可以看到我用来插入图标的 i 元素,并且在 thd 开发工具中,我可以看到我使用的图像,但是因为这 2 个属性被覆盖,所以无法正确显示。 If I use @Mixin it works fine but from what I heard it's better to use @extend when I can.如果我使用@Mixin,它可以正常工作,但据我所知,最好尽可能使用@extend。

Few issues in your snippet:您的代码段中的几个问题:

.google-icon {
    background: url('../icons/icon.png');
    @extend icon;
}
  • Your extend should be @extend.icon , see the " . "?您的扩展应该是@extend.icon ,请参阅“ . ”?

  • You are using background: url('../icons/icon.png') , when you should use background-image: url('../icons/icon.png') .您正在使用background: url('../icons/icon.png') ,而应该使用background-image: url('../icons/icon.png')

background is a shorthand , it means that it's a way to provide a value for multiple properties. background是一个简写,它意味着它是一种为多个属性提供值的方法。 ( eg: background-image, background-size, background-position, background-color, etc... ). 例如:背景图像、背景尺寸、背景位置、背景颜色等... )。 That line is overwritting your former rules.那条线正在覆盖您以前的规则。

To avoid the use of @extend you could follow a different methods:为避免使用@extend ,您可以采用不同的方法:

CSS [attribute^=value] Selector CSS [属性^=值] 选择器

[class^=icon-] {
    height: 4.5rem;
    width: 4.5rem;
    display: inline-block;
    background-size: contain;
    background-repeat: no-repeat;
}

.icon-google { // The classname will start with icon-
    background-image: url('../icons/icon.png'); // background-image instead of background
}

By using [class^=icon-] , every html element with a class starting with icon- will be selected.通过使用[class^=icon-] ,将选择每个 html 元素,其中icon-以 icon- 开头。
This way, if all your icons classname are starting with icon- , like icon-google , you won't need any extend.这样,如果您的所有图标类名都以 icon- 开头,例如icon- icon-google ,您将不需要任何扩展。

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

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