css - 针对具有两个不同父类的同一个 child

标签 css sass compass-sass

这几天我开始使用 SASS 编写我的 css 文件。 我有两个不同的 parent 类(class),但这两个 parent 有相同的 child 类(class)。所以我想用这段 CSS 代码构建我的 SCSS 树:

#header {
    position: relative;
    width: 100%;
}

#header-g {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 300;
    width: 100%;
}

#header .l-header-top, #header-g .l-header-top {
    height: 55px;
    line-height: 50px;
    border-top: 5px solid #474747;
    background-color: #f0f1f3;

    font-size: 16px;
}

我试过了,但我想我忘记了什么:

    #header {
    position: relative;
    width: 100%;

    &#header-g {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 300;
        width: 100%;

        .l-header-top {
            height: 55px;
            line-height: 50px;
            border-top: 5px solid #474747;
            background-color: #f0f1f3;

            font-size: 16px;
        }
    }
}

谢谢

最佳答案

在您的代码中,如果您使用 &,则意味着它们在同一元素中,并且在单个元素中只有 1 个 ID。在这种情况下,您应该使用 class

#header {
    position: relative;
    width: 100%;

    &#header-g {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 300;
        width: 100%;

但它应该是这样的。 #header#header-gestion 是你的 ID parent ,而他们有相同的 child .l-header-top

#header {
    position: relative;
    width: 100%;
    .l-header-top {
       height: 55px;
       line-height: 50px;
       border-top: 5px solid #474747;
       background-color: #f0f1f3;

       font-size: 16px;
    }
}

#header-g {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 300;
    width: 100%;
    .l-header-top {
       height: 55px;
       line-height: 50px;
       border-top: 5px solid #474747;
       background-color: #f0f1f3;

       font-size: 16px;
    }
}

或者您以这种方式使用 & ,这是基于类命名约定中的 BEM 方法论。您可以查看此链接:BEM — Block Element Modifier Methodology

#header {
    position: relative;
    width: 100%;
    &-g {
       position: fixed;
       top: 0;
       left: 0;
       z-index: 300;
       width: 100%;
    }
}


#header,
#header-g {
    .l-header-top {
       height: 55px;
       line-height: 50px;
       border-top: 5px solid #474747;
       background-color: #f0f1f3;
       font-size: 16px;
    }
}

关于css - 针对具有两个不同父类的同一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47690588/

相关文章:

javascript - 如何启用模式的背景阴影?

html - 简单的 HTML 导航按钮不显示

css - Icomoon.ttf 未加载

node.js - @import 'X' 媒体查询表达式必须以 '(' 开头对于带有 gatsby 的 sass 文件没有意义

css - Bootstrap 不会加载 rails 和 sass

css - 如何摆脱 JavaFX 2 中 Pane 分隔符内的拆分 Pane 分隔符中的三个小点?

css - 使 sibling 的图像双倍高度(难以主题)

css - 在 SASS 中扩展选择器

css - 在没有 Compass 混入的情况下将 SCSS 转换为 SCSS

css - 如何将 compass 与祝福结合起来?