简体   繁体   English

SASS / SCSS @extend规则选择器

[英]SASS / SCSS @extend rule selectors

I'm having a little problem with the @extend rule, this is what I got (focus on the h1): 我对@extend规则有一点问题,这就是我得到的(专注于h1):

.content-header {
    // CSS properties
    h1 {
        // CSS properties
    }
}

.box-header {
    // CSS properties
    h1 {
        @extend .content-header h1; // My selector problem!
        // And his own CSS properties
    }
}

So it will be: 所以它将是:

.content-header h1, .box-header h1 {
    /* Happily sharing the same CSS properties */
}

But it seems like @extend don't like that, is any other way to write this without giving the h1 a class?? 但似乎@extend不喜欢这样,有没有其他方式来写这个而不给h1一个类?

Nested selectors cannot be extended - in fact, that's the syntax error that is reported by the parser. 嵌套选择器无法扩展 - 实际上,这是解析器报告的语法错误。 Structural comments aside (I can't think of a scenario where the above @extend relationship is warranted), this is not something that can be currently accomplished with SASS. 抛开结构性评论(我无法想到上述@extend关系得到保证的情况),这不是目前SASS可以完成的事情。

NB: this is, however supported by Stylus if you're open to it. 注意:如果您对它开放,可以使用Stylus支持。

An obvious solution whould be to define a new @mixin your-name for your .content-header h1 definitions and @include your-name within .box-header h1 . 一个明显的解决方案是为你的.content-header h1定义定义一个新的@mixin your-name ,并在.box-header h1定义@include your-name

But, there is a much better solution Referencing Parent Selectors: & : 但是,有一个更好的解决方案引用父选择器:&

h1 {
    .content-header &,
    .box-header & {
        // CSS properties
    }
    .box-header & {
        // And his own CSS properties
    }
}

It's not obvious because the logic reverse but it's better to maintain. 这并不明显,因为逻辑反转但维护更好。 You are changing the definitions of h1 for a specific selector. 您正在更改特定选择器的h1定义。

Nested @extend is not allowed. 不允许嵌套的@extend。 Try this for a work around 试试这个来解决问题

.foo {
  background-color: lime;    
}
.b{
  margin:0px;
}
.baz {
  @extend .foo;
  @extend .b;
}

Some thing I constructed for my personel use below sharing with you all, This dynamically constructs the selector, Since special characters are not allowed in naming conventions I used '--' to seperate class 我为我的个人构建的一些东西使用下面与你共享,这动态构造选择器,因为在命名约定中不允许使用特殊字符我使用' - '来分隔类

$ih-classes: ( "module--cardContainer--header",
                "a"
              );
  %module--cardContainer--header {
    color: #1e1d1c;
    background-color: #fff;
    border-bottom: 0.0714rem solid #e0dddc;
    padding: 0;
    line-height: 3.1429rem;
    font-size: 1.2857rem;
    font-family: ProximaNovaSemiBold;
}
%a{
  color:red;
}

@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}
@mixin generate-framework-code() {

              @each  $icon in $ih-classes {
               $val : str-replace($icon, '--', ' .');
                  .#{$val} {
                            @extend %#{$icon};
                         }
                  }
}

@include generate-framework-code();

Good Luck!! 祝好运!!

As already said, you can only extend a single class. 如前所述,您只能扩展一个类。 Generally, nesting with extend should be skipped. 通常,应跳过使用extend进行嵌套。 There is more about extend and its options in this text . 本文中有更多关于扩展及其选项的内容

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

相关问题 在 SASS/SCSS 中重用样式而不合并选择器 - Reusing styles in SASS/SCSS without merging selectors SASS中@ extend-only选择器(占位符)的范围 - Scope of the @extend-only selectors (placeholders) in SASS 使用SASS / SCSS扩展和覆盖现有样式 - Extend and override existing style with SASS/SCSS 如何在SASS / SCSS列表变量中的表单字段中使用属性选择器? - How to use attribute selectors for form fields in a SASS/SCSS list variable? 在Sass SCSS中将选择器列表与&组合使用在嵌套选择器中 - Using a list of selectors in combination with & in a nested selector in Sass SCSS 在 SASS/SCSS 中过度嵌套选择器在实践中有多糟糕? - How bad is it in practice to over-nest selectors in SASS/SCSS? 是否可以在 Sass/SCSS 中制作以选择器为参数的函数/mixins/速记? - Is it possible to make functions/mixins/shorthands in Sass/SCSS that take selectors as an argument? 使用@extend 时,Sass 将所有选择器应用于样式块 - Sass applies all selectors to style block when using @extend 封装/命名空间的CSS和Sass扩展,没有嵌套选择器 - Encapsulated/Namespaced CSS and Sass Extend without Nested Selectors 如何有条件地加载CSS并使用SASS / SCSS对其进行扩展 - How to conditionally load CSS and extend it using SASS/SCSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM