I'm internalising my site's CSS
| 4 min read
I redid my website recently. I wanted quick and simple, so I used Bootstrap as the framework to throw it together.
Bootstrap is great, I love it. I've used it on countless projects and I know what I'm getting and how to make it work. But for a site like this, it is overkill. Think less sledgehammer to crack a walnut, and more rolling over it with a tank. There's JavaScript I don't need, and dozens of classes for beautiful elements that I'm just not going to use.
So I did what any self respecting web dev nerd would do. Ignored all of the countless great lightweight CSS frameworks out there (sorry Pico, Pure, and Simple), and built my own stylesheets from scratch.
Let's nerd out
This posed an interesting* option. The go-to move for most devs is to create a stylesheet separate from the code of the website, and then reference it at the head of each page.
<link rel="stylesheet" href="assets/style.css">
But there are those who argue that a better way is to put the actual CSS into the head itself. (There are still others that argue you should put the styling inline of each element itself...)
For a lot of sites that just isn't practical. But one of the benefits of using a static site generator like Jekyll is that I can have the best of both worlds. If I create a separate file for my stylesheet, I can include the call to it in the template, and when the site it built it will be included in each and every page.
{% raw %}
<style>{% include style.css %}</style>
{% endraw %}
Easy. The CSS now sits at the top of every page, meaning one fewer server call on each page load.
Some of you might have already spotted an option here to over-engineer the situation. Which, obviously, I took.
Only certain CSS classes are needed on each page. Through the beauty of static site generation, I can break my stylesheet down into individual sheets, and then be very granular about which stylesheets are included into each page. I've decided, for example, that I'm not going to put images within the text on pages of this site, but I will have them in blog posts. So there's no need to have the image class on the homepage. I can pull that out of the main stylesheet, and only call for it in the layout templates of pages that I know will display images.
{% raw %}
<!-- styles needed for default page templates -->
<style>{% include style.min.css %}</style>
<!-- styles needed for blog posts -->
<style>{% include style.min.css %}{% include post.min.css %}</style>
{% endraw %}
(You might also notice that I've taken the opportunity to minify the stylesheets.)
I can be as granular as I like, but obviously there's a trade-off here. If I was super anal about it, I could have a separate file for each class, and only include those classes that are used individually in each page. That would be a lot though. (Although, it has made me think... It can't be too hard to reverse that process - have the compiler read through each page and determin what classes are being used, and then only include the style files for the relevant classes... A project for another time, perhaps.)
As a middle ground, I've gone with grouping classes by broad function. For instance, I have one for all pages that has generic things like spacings, fonts, and how to handle responsiveness. I also have a separate one for each of the things that are different - like typography and image styling for blog posts.
But why?
There's a lot of chat about what's more efficient. Internal CSS means fewer calls to the server (good), but larger over all page sizes (less good). External CSS can be cached (good), but you need to load the whole stylesheet for every page (less good).
To be honest, for a simple site like mine (predominately text based, few images, simple styling), the trade-offs and benefits are so miniscule that it probably wasn't worth the effort. In fact, if I'm looking at page load speeds, the fact that I'm calling in a FontAwesome API on its own more than removes any benefit. (I do love those icons though - and I'm not brave enough to go completely text-only.)
The real reason I've done it? Because it's my site, and I felt like it.
*"interesting" is, as always, subjective.
Want to chat about anything in this post? I'd love to hear from you!
Feel free to drop me an email
Or find me on Mastodon