css - 如何在 css( sass ) 中重用通用的 css 规则

标签 css sass css-selectors

<分区>


想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它.

关闭 6 年前

我有一个如下所示的 CSS 样式表

.icon1 {
    &:after {
        content:"";
        float: right;
        width: 8px;
        height: 8px;
        border-radius: 100%;                  
        background-color: #4ac102;
        display: block;
        position: relative;
    }
}

.icon2 {
    &:after {
        content:"";
        float: right;
        width: 8px;
        height: 8px;
        border-radius: 100%;                
        background-color: #f1342f;
        display: block;       
        position: relative;
    }
}

.icon3 {
    &:after {
        content:"";
        float: right;
        width: 8px;
        height: 8px;
        border-radius: 100%;                
        background-color: #616a83;
        display: block;       
        position: relative;
    }
}

如您所见,除了背景颜色外,其他所有属性都是相同的。我可以重构此代码以使其变干吗?

最佳答案

您可以使用 Extend/Inheritance为此:

  .icons {
    content:"";
    float: right;
    width: 8px;
    height: 8px;
    border-radius: 100%;      
    display: block;       
    position: relative;
  }

  .icon1 {
    &:after {
      @extend .icons;             
      background-color: #4ac102;
    }
  }
  .icon2 {
    &:after {
      @extend .icons;             
      background-color: #f1342f;
    }
  }

  .icon3 {
    &:after {
      @extend .icons;             
      background-color: #616a83;
    }
  }

关于css - 如何在 css( sass ) 中重用通用的 css 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38463252/

上一篇:css - 侧滚动不适用于 android

下一篇:html - 使用 CSS 的锐利边框

相关文章:

javascript - 如何使 div 遵循 flex 的轨迹?

compass-sass - 仅当存在时如何在 compass 中导入scss文件?

vue.js - "": expected 1 selector or at-rule, 之后的无效 CSS 是 "{"

java - JAVA(Selenium)中的for循环通过cssSelector查找Element

指向当前页面的链接的 CSS 样式?

html - 如何在保持 CSS 边框的同时换行文本

css - 创建响应式屏幕时如何在 CSS 中删除/隐藏类广告

html - 强制列内容为 `text-align: right`

html - 无法将多个伪类添加到 :not() pseudo-class

javascript - 带有 HTML5 表单验证问题的 SASS 隔离