Null variables in Sass

Bootstrap is a large and complex frontend library with some powerful functionality and customization built-in. Occasionally though, we get asked to make things even more customizable for folks with more CSS properties and default variables.

This is la…


This content originally appeared on @mdo and was authored by Mark Otto

Bootstrap is a large and complex frontend library with some powerful functionality and customization built-in. Occasionally though, we get asked to make things even more customizable for folks with more CSS properties and default variables.

This is largely because Bootstrap’s CSS relies on some limited global inheritance of general styles (font, color, line-height, and more), but developers and designers often want to customize things further. So we were typically faced with adding CSS we didn’t need at the outset, and that you might need. This just didn’t seem right for any of us.

Back in January, thanks to one of our maintainers Martijn, we started using null value default variables to combat that bloat while still increasing our level of customization.

Here’s a basic example:

$body-text-align: null !default;

body {
  text-align: $body-text-align; // Won’t compile
}

As you can see, the Sass compiler will ignore the entire property: value; declaration because the value is null. But, if someone customized this before compilation by overriding that default value, the CSS would compile. Here’s an example of how you’d override that default variable.

// First, override `!default` variables
$body-text-align: left;

// Second, import Bootstrap
@import bootstrap;

And here’s how it compiles to CSS.

body {
  text-align: left;
}

It’s also worth mentioning that these null values can be used in mixins, too. Check out this old Treehouse article for details.

Historically, more flexibility in our default styles meant more code for everyone, but with null value default variables, we can continue to provide more customization when warranted without knowingly adding bloat to our compiled CSS. I still owe Bootstrap a fresh pass at this to see what we can remove.

Hopefully we can continue to find more techniques for increased customization and smaller file sizes. Please share if you have them!


This content originally appeared on @mdo and was authored by Mark Otto


Print Share Comment Cite Upload Translate Updates
APA

Mark Otto | Sciencx (2019-08-30T00:00:00+00:00) Null variables in Sass. Retrieved from https://www.scien.cx/2019/08/30/null-variables-in-sass/

MLA
" » Null variables in Sass." Mark Otto | Sciencx - Friday August 30, 2019, https://www.scien.cx/2019/08/30/null-variables-in-sass/
HARVARD
Mark Otto | Sciencx Friday August 30, 2019 » Null variables in Sass., viewed ,<https://www.scien.cx/2019/08/30/null-variables-in-sass/>
VANCOUVER
Mark Otto | Sciencx - » Null variables in Sass. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/08/30/null-variables-in-sass/
CHICAGO
" » Null variables in Sass." Mark Otto | Sciencx - Accessed . https://www.scien.cx/2019/08/30/null-variables-in-sass/
IEEE
" » Null variables in Sass." Mark Otto | Sciencx [Online]. Available: https://www.scien.cx/2019/08/30/null-variables-in-sass/. [Accessed: ]
rf:citation
» Null variables in Sass | Mark Otto | Sciencx | https://www.scien.cx/2019/08/30/null-variables-in-sass/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.