【问题标题】:SCSS Referencing a Particular Instance of an ElementSCSS 引用元素的特定实例
【发布时间】:2017-05-17 15:00:49
【问题描述】:

非常对 Scss/Sass 很陌生,在我的 mod.scss 文件中的其他地方,已经编写了我经常重复使用的 Ul > li 样式。但是,在另一个 mod.scss 文件中,我需要为单个特定实例更改此代码。

是否可以从本质上创建一个类似 if 的语句:“如果 UL/LI 标记出现在 .content-section 类下,则采取行为 X、Y 和 z”?

.content-section
{
    margin: 40px 0px;

    & .siteTitleText 
    {
        margin-bottom: 50px;
    }

    & .headers
    {
        margin-bottom: 30px;
    }

    img
    {
        margin: 30px 0px;
    }

    *ul*

}

HTML:

<div class="content-section vendors">
    <p class="headers">Modules, partials, and vendor</p>
    <p class="bodyText">As you can see this divides my project into three basic types of files. Modules, partials, and vendored stylesheets.</p>
    <ul>
        <li class="bodyText">The modules directory is reserved for Sass code that doesn't cause Sass to actually output CSS. Things like mixin declarations, functions, and variables.</li>

        <li class="bodyText">The partials directory is where the meat of my CSS is constructed. A lot of folks like to break their stylesheets into header, content, sidebar, and footer components (and a few others). As I'm more of a SMACSS guy myself, I like to break things down into much finer categories (typography, buttons, textboxes, selectboxes, etc…).</li>

        <li class="bodyText"></li>
    </ul> 
</div>

【问题讨论】:

  • 你能分享一下你的 HTML 结构吗?
  • 当然,我会编辑帖子
  • “下”是指“.content-section”的后代还是页面布局的物理下?

标签: html css variables sass scss-lint


【解决方案1】:

在您的代码中使用+ 来选择结束标记下方的下一个匹配项。 如果您希望在 .content-section 中选择 child 标签,只需嵌套标签即可。

参考:w3 documentation

.content-section {
    margin: 40px 0px;

    & .siteTitleText {
        margin-bottom: 50px;
    }

    & .headers {
        margin-bottom: 30px;
    }

    img {
        margin: 30px 0px;
    }

    ul, li { // If .content-section has a child named ul or li, do this:
        margin: 100px;
    }
}

【讨论】:

  • 我对 OP 的理解是当ul.content-section 的孩子而不是兄弟姐妹时。
  • 这有点不清楚,让我们问问@TheodoreSteiner本人吧。
  • 大家好,非常抱歉,是的,我的意思是
  • 删除+应该可以工作,我在移动所以我不能编辑帖子
  • @TheodoreSteiner 更新:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 2015-12-05
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
相关资源
最近更新 更多