Can I check parent element has specific class in sass?
Simply create two rules:
.child {font-size: 16px;}
.big .child {font-size: 20px;}
In SASS it would be
.child
font-size: 16px
.big &
font-size: 20px
Sass selector that checks if a class is on any ancestor
Yes it is. You can use &
like this:
.seed {
.tree & {
/** Wow! I'm seed in tree! */
color: red;
}
}
You can read more about Parent Selector in Sass documentation.
Sass - if class has this parent
According to this GitHub issue, you can use the following as of SASS v3.3.0:
.foo {
color: blue;
@at-root {
p#{&} {
background: yellow;
}
span#{&} {
background: red;
}
}
}
Which outputs:
.foo {
color: blue;
}
p.foo {
background: yellow;
}
span.foo {
background: red;
}
How to select parent using SCSS
Use parent selector &
after the class, something like this:
.username {
color: red;
.mobile & {
font-weight: bold
}
.desktop & {
font-weight: normal
}
}
Here is a demo
Note: don't need to repeat the color: red
Select parent selector in SASS/SCSS
Either you write both the selectors with the full classname (without using &
) or you could save a reference to the .message
class and use it in the nested selector
.message {
display: block;
width: 300px;
border-radius: 5px;
border: 2px solid #000;
padding: 10px;
$this: &; /* reference to the .message class */
&__title {
font-size: 18px;
line-height: 1;
color: #000;
margin-bottom: 10px;
}
&__content {
font-size: 15px;
color: #000;
}
&--success {
border-color: #3f9442;
/* use the reference stored */
#{$this}__title {
color: #3f9442;
}
#{$this}__content {
color: #3f9442;
}
}
}
Related Topics
Video Player Shows Black Border
Bootstrap Styles Missing After Deploy to Heroku
How to Fix The Height of a <Div> Element
Div Height:100%; Not Working with Display:Table-Cell;
CSS for Double Height Text and Double Width Text in Font Style
How to Apply Different CSS Styles to 2 Elements with The Same Class Name
How to Make Div Same Height as Parent (Displayed as Table-Cell)
How to Stack Two Same-Sized Canvas on Top of Each Other
Can The SASS Minifier Remove Duplicate Styles
Avoid an Image to Go Outside a Div
How to Control 'Div' Overlapping in HTML
Overflow:Hidden in Float. Does It Hide Anything
CSS: Wrong Position of "Transform: Scale();" Container Children
HTML5 Video Background, Keep Center of Video in Center
Don't Change Link Color When a Link Is Clicked
Using Rem Units for a Div in a Web Page