【问题标题】:SASS: Child classes according to parent classSASS:根据父类的子类
【发布时间】:2014-04-23 22:59:04
【问题描述】:

有没有办法可以检查父元素类并更改子元素类属性?

类似:

if parentClass {
  h1{color: red;}
} else if parentClass2 {
  h1{color: blue;}
}

希望 CSS 像:

.parentClass h1 {
  color: red;
}
.parentClass h2 {
  color: blue;
}

因此,如果父类的名称发生变化,子类的属性也会发生变化。

提前致谢:)

【问题讨论】:

    标签: css sass compass-sass


    【解决方案1】:

    在这种情况下你不能使用@if 语句,但你可以做这样的事情

    h1 {
      color: red;
    
      .parent-1 & {
        color: blue;
      }
    
      .parent-2 & {
        color: yellow;
      }
    }
    

    输出将是

    h1 {
      color: red;
    }
    
    .parent-1 h1 {
      color: blue;
    }
    
    .parent-2 h1 {
      color: yellow;
    }
    

    【讨论】:

    • 感谢您的回答,我认为这是唯一的选择。投票! :)
    • 是否可以将类名保存为变量,以便我可以为类和 CSS 属性值使用相同的名称?像 .#{$color} {color: $color}
    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2021-09-10
    • 2016-05-08
    • 1970-01-01
    • 2012-09-01
    • 2014-12-30
    • 2020-10-19
    相关资源
    最近更新 更多