【问题标题】:Target the same child with two differents parent class用两个不同的父类定位同一个孩子
【发布时间】:2018-05-21 07:06:47
【问题描述】:

几天以来,我一直在使用 SASS 编写我的 css 文件。 我有两个不同的父母班级,但这两个父母有相同的孩子班级。所以我想用这个 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;
        }
    }
}

谢谢

【问题讨论】:

    标签: css sass compass-sass


    【解决方案1】:

    在您的代码中,如果您使用&,则意味着它们在同一个元素中,并且在单个元素中只有 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 父母,而他们有相同的孩子 .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;
        }
    }
    

    【讨论】:

    • DRY principles。那么为什么不使用好的老式 CSS #header .l-header-top, #header-g .l-header-top
    • 他正在使用 SCSS,在这种情况下,这将是其构建后的输出
    • 我没有使用SCSS,所以无法判断,但是普通的CSS和SCSS不能合并在一个文件里吗?重复自己是在要求失败。如果你想改变颜色,你需要改变两个值......你总是会忘记一个......
    • 你说得有道理,所以我更新了我的答案,这与你的观点类似,但采用 SCSS 方式,但输出相同。
    • 很好,提供多种解决方案总是很好。一种更适合你,另一种更适合其他人。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-05-08
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多