In SASS a placeholder looks and acts a lot like a class selector, only it starts with a %
and it is not included in the CSS output.
Our %placeholder
selector contains some width and height declarations:
%placeholder {
width: 100%;
height: 100%;
}
body {
@extend %placeholder;
}
p {
@extend %placeholder;
}
Note that weâve used the @extend
directive, which allows one selector to inherit styles of another selector.
This outputs to CSS as follows:
body, p {
width: 100%;
height: 100%;
}
Simple and as expected!
However, the preprocessor will skip %placeholder
and it wonât be included in the final CSS file.
Why use Placeholders?
Placeholder selectors are mainly useful when writing a SASS library where each style rule is optional.
Typically, when working on your own project, itâs often better to just extend a class selector instead. But itâs good to know, as it could come in quite handy if you start working on larger-scale projects.
In the next article, weâll learn how to structure our SASS projects.
Related Posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.
While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.
Thanks for reading! đ