How to implement an immediate parent selector in SASS, SCSS?
source link: https://www.codesd.com/item/how-to-implement-an-immediate-parent-selector-in-sass-scss.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to implement an immediate parent selector in SASS, SCSS?
I have HTML layout as below:
<div class="card">
<div class="section">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
Compiled CSS should look like:
.card{
}
.card .section .item{
}
.card .section .item+.item{
}
What I'm writing in SCSS:
.card{
.section{
.item{
&+&{
}
}
}
}
Can we have something like & + &
, which is not working, I do not want to write,
.card{
.section{
.item{
}
.item+.item{
}
}
}
&
is the parent selector, you can't use two &
because then you'd be out of the scope. In that case, just go up one block.
#main {
color: black;
a {
font-weight: bold;
&:hover { color: red; } /* Look at notes below */
}
}
Compiles to
#main {
color: black; }
#main a {
font-weight: bold; }
#main a:hover { /* parent selector is replaced :D */
color: red; }
If you used two &
adjacently you would be in the parent of the parent, which wouldn't make sense since you can just write your sass or scss one code block higher in the hierarchy.
Related Articles
How difficult is it to overprint selectors in SASS / SCSS?
How to implement a gradient color selector in a code?
How to implement Insert, Update and Delete in a parent class that works for all database models using sqlite-net?
Faking the: has () & ldquo; parent selector & rdquo; using only CSS
Symfony2 - How to implement nested records and recursive functions in Entity Field or Twig Layout?
How to Implement a Default Upload to uiscrollview
How to Implement Static CGRect and CGFloat
How to Implement Line Numbering in h: dataTable
How to implement code for signal loss or site?
How to skip running a parent method to run the grandparents method?
How to get the nearest parent node given a text node in jQuery?
What is the difference between .closest () and .parents ('selector')?
How to Implement OnFragmentInteractionListener
How to implement Touch Up Inside in touchBegan, touchEnded
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK