Using Sass to Semantically Extend Bootstrap
Last updated
Was this helpful?
Last updated
Was this helpful?
By Brad Barrow
Bootstrap provides a quick and easy way to create a scaffold for your latest site or app whether you’re a novice or a professional developer. For this reason, more and more developers are including the framework in their personal toolbox.
There is, however, a dark side to Bootstrap, in that it makes it incredibly easy to write cluttered, non-semantic and non-reusable markup that will render correctly across all browsers.
First, let’s cover what we mean when we say that we want to make things more “semantic”. HTML documents are intended to be descriptive of their contents from an information hierarchy perspective. One should be able to read them and know what they are about, not how they will look.
With the rise in popularity of CSS and especially CSS frameworks, HTML is often written with a CSS sheet in mind rather than a reader/developer.
It’s not uncommon to see HTML markup like this in the source code of modern websites:
Bootstrap’s CSS files know exactly what to do with that. Two rows, one split into thirds and the other full width. As developers who are familiar with Bootstrap, that’s easy enough for us to figure out, but what if you’d never used the framework before? What is this snippet of HTML for? What does the information pertain to? The markup isn’t revealing anything practical.
Semantic markup is code that describes it’s content rather than its appearance. We could write the code above semantically as follows:
It’s not all that different but it immediately tells us what each element is for and how each one might relate to other elements. What we’ve lost is the knowledge of how this will be presented visually. But that’s not the purpose of HTML after all.
You might be wondering how you’re going to use Bootstrap without any of its built-in class names in your markup? Well, we can leverage the power of Sass’s @extend
functionality to solve this problem.
From Sass’s documentation:
@extend
works by inserting the extending selector anywhere in the stylesheet that the extended selector appears.
This means we can use our semantic selectors and extend Bootstrap’s selectors to get all of its goodness. Here’s how:
Through the magic of @extend
, our selectors are slotted in to the compiled stylesheets alongside the Bootstrap selectors they extend. As you’ll see by examining the compiled code, our selectors have all the exact same properties as the classes they’re extending.
For example, among other things, you should see this in the compiled CSS:
Another great thing about this method is that it creates readable and reusable components. For example, you don’t have to rebuild the author component out of rows and columns each time you need to use it. Instead, you have sensible names for each part of the component, making it easy to construct. You can also rely on Bootstrap to make sure it’s presented uniformly across your site.
While Boostrap is one of the best frameworks available, the time may come when you want to move your site to a different framework or even write your own. With the above outlined method you can do so easily because your markup is cleanly decoupled from your CSS.
Bootstrap gives us an incredibly powerful set of styles – but it’s all too easy to cobble them together into something presentable at the loss of quality markup. With Sass’s @extend
directive, you’re free to write markup that speaks clearly both to its contents and to the developers reading it.
In this article, I’ll explain how you can use Bootstrap in a more robust and semantic way. Bootstrap’s latest version shipped with of the framework so we’ll use one of Sass’s most powerful features to achieve this: .
If you haven’t yet tried the Sass version of Bootstrap, you’re in for a treat. It’s easy to get started on .
Sass isn’t the only thing at play here. If you truly want next-level semantics in your HTML and CSS, I recommend adopting a naming convention such as or to keep your selectors standardized and easy to remember.