Sass and combined child selector

后端 未结 2 1429
余生分开走
余生分开走 2020-12-13 03:12

I\'ve just discovered Sass, and I\'ve been so excited about it.

In my website I implement a tree-like navigation menu, styled using the child combinator (E &g

2条回答
  •  生来不讨喜
    2020-12-13 03:32

    For that single rule you have, there isn't any shorter way to do it. The child combinator is the same in CSS and in Sass/SCSS and there's no alternative to it.

    However, if you had multiple rules like this:

    #foo > ul > li > ul > li > a:nth-child(3n+1) {
        color: red;
    }
    
    #foo > ul > li > ul > li > a:nth-child(3n+2) {
        color: green;
    }
    
    #foo > ul > li > ul > li > a:nth-child(3n+3) {
        color: blue;
    }
    

    You could condense them to one of the following:

    /* Sass */
    #foo > ul > li > ul > li
        > a:nth-child(3n+1)
            color: red
        > a:nth-child(3n+2)
            color: green
        > a:nth-child(3n+3)
            color: blue
    
    /* SCSS */
    #foo > ul > li > ul > li {
        > a:nth-child(3n+1) { color: red; }
        > a:nth-child(3n+2) { color: green; }
        > a:nth-child(3n+3) { color: blue; }
    }
    

提交回复
热议问题