Say I have two unordered lists in one stylesheet. Both should be using the same styling, but both are nested in different parent elements:
#foo{
position:absolute;
...
ul{
list-style-type:none;
li{
color:red;
...
}
}
}
#bar{
position:relative;
...
ul{
list-style-type:none;
li{
color:red;
...
}
}
}
Is there a way to create something similar to a Rails' partial, where a single block of code can be reused / rendered inside different parent elements?
You want to use @extend . btn; - @extend allows you to inherit all the properties of a selector without having to define it as a mixin.
Nesting is combining of different logic structures. Using SASS, we can combine multiple CSS rules within one another. If you are using multiple selectors, then you can use one selector inside another to create compound selectors.
SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.
Once Sass is installed, you can compile your Sass to CSS using the sass command. You'll need to tell Sass which file to build from, and where to output CSS to. For example, running sass input.scss output.css from your terminal would take a single Sass file, input.scss , and compile that file to output.css .
Solutions:
1-Mixins Link
@mixin ul-style ()
{
ul{
list-style-type:none;
li{
color:red;
}
}
}
#foo{
position:absolute;
@include ul-style();
}
#bar{
position:relative;
@include ul-style();
}
2-Inheritance Link
.ul-style
{
ul
{
list-style-type:none;
li{
color:red;
} }
}
#foo{
position:absolute;
@extend .ul-style;
}
#bar{
position:relative;
@extend .ul-style;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With