简体   繁体   English

如何在CSS / SASS中设置嵌套ID元素的样式

[英]How to style nested ID-elements in CSS/SASS

What is the correct way to style a nested element with an ID? 使用ID为嵌套元素设置样式的正确方法是什么?

example.html example.html的

<div id="content">
    <ul id="list">
        <li>Just a minimal example</li>
    </ul>
</div>

style.sass style.sass

#content
    background: white;

    #list
        list-style-type: none;
        padding: 10px 15px;

With this it is easier to read the SASS file, as it is obvious, that #list is a child of #content . 有了它,更容易阅读SASS文件,因为很明显, #list#content的孩子。

But I think the browser has to check for two things, isn't it? 但我认为浏览器必须检查两件事,不是吗? First find the #content and then find the #list element. 首先找到#content ,然后找到#list元素。

So should I use? 我应该使用吗?

#content
    background: white;

#list
    list-style-type: none;
    padding: 10px 15px;

I recommend to not nest id selectors as you gain performance. 我建议不要在获得性能时嵌套id选择器。 Of course this will be of no matter on small CSS files. 当然这对于小CSS文件来说无关紧要。

A well used way to "show" the relation between to classes in the CSS output, is to indent child classes like this, but also often the CSS is minified and not human friendly, so it all kind of also depends on who's gonna read it. 一个很好用的方法来“显示”CSS输出中的类之间的关系,就是缩进像这样的子类,但是CSS也经常被缩小而不是人类友好,所以这一切也取决于谁会读它。

#content {
  background: white;
}
  #list {
    list-style-type: none;
    padding: 10px 15px;
  }

Both are fine. 两者都很好。 Spacing have no effects in css/sass/less/scss code. 间距对css / sass / less / scss代码没有影响。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM