Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

html - SCSS parent selector not works

I have a container and three parts in it. I want to make unvisible thirth part while addind class to the parent container. But my codes not works...

Here is the HTML code:

<section id="parts" class="two-parts">
    <div id="partOne">...</div>
    <div id="partTwo">...</div>
    <div id="partThree">...</div>
</section>

Here is the SCSS code:

#partThree{
    display: block;

    .two-parts &{
        display: none;
    }
}

I want to hide #partThree div, when #parts div has .two-parts class.

Guys! I found the problem. I checked the output of Scss to Css, and here is the Css output:

.two-parts body #parts #partThree {
  display: none;
}

It should be #parts.two-parts #partThree. But why is that goes to the top of all elements?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest way to achieve what you need is by setting .two-parts and #partThree as siblings, and apply ~ css operator.

<style>
  #partThree {
    display: block;

  .two-parts ~ {
    display: none;
  }
}
<style>

<section id="parts">
    <div id="partOne">...</div>
    <div id="partTwo" class="two-parts">...</div>
    <div id="partThree">...</div>
</section>

this will only work if #partThree and .two-parts siblings and .two-parts comes before.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...