I had fun time talking about maintainable CSS architecture in the Gutenberg era in WordCamp London and in WordCamp EU.
Especially in #WCEU I had a change to talk about CSS with lot’s of people. Thank you all for your questions and challenging the thinking behind my talk ideas.
In this post I’ll go through the CSS architecture in text format. Or you can skip it and check the slides or code:
At the moment track 3 video is also in Youtube and talk start at time 2:48.
I had couple of general ideas about the topic:
There are many CSS methodologies out there.
It’s important that you pick something, whatever rocks your boat. That’s the most important part of the maintenance. Otherwise you’ll end up adding the CSS in random places in random ways. And adding more specificity every time you are in trouble, not good.
I personally mix ITCSS and utility classes in PHP based WordPress theme.
All of the different CSS methodologies have similar goals.
Harry Roberts have outlined similar CSS architecture in his guidelines. Also Jarno Rantanen has nice CSS architecture examples.
ITCSS stands for Inverted Triangle CSS. It separates CSS codebase to several sections (layers). And every sections adds more specificity to CSS in the right order.
Here is example what CSS sections could look like in WordPress theme:
<h1>
and <blockquote>
.For more details this is great article about using ITCSS: Scalable and Maintainable CSS Architecture.
All I can say is that I have successfully used ITCSS architecture in several large scale projects. It works.
When working on large team with different backgrounds, class prefixes can help understand what job that class are doing. And in what layer they belong in, for example:
Brad Frost have more details and examples in brilliant article CSS Architecture for Design Systems.
Follow CSS guidelines and let linting do the hard work enforcing those guidelines. I personally use
And then enforce them using stylelint.
BEM stands for “Block Element Modifier”. It helps with our goals to keep specificity low.
In HTML BEM classes can look like this:
<nav class=”menu menu--primary”>
<ul class=”menu__items”>
<li class=”menu__item”><a class=”menu__anchor”>Home</a></li>
<li class=”menu__item”><a class=”menu__anchor”>About</a></li>
</ul>
</nav>
In CSS we could nest BEM rules like this:
.menu {
&--primary {}
&__items {}
&__item {}
&__anchor {}
}
// Compiled CSS
.menu {}
.menu--primary {}
.menu__items {}
.menu__item {}
.menu__anchor {}
And it would compile in CSS like this:
// Compiled CSS
.menu {}
.menu--primary {}
.menu__items {}
.menu__item {}
.menu__anchor {}
It’s perfectly fine not to nest CSS rules though. Ones again Brad have good article about nest or not to nest CSS selectors?
I dequeue Core block styles from front-end and editor.
add_action(
'enqueue_block_assets',
function() {
// Overwrite Core block styles with empty styles.
wp_deregister_style( 'wp-block-library' );
wp_register_style( 'wp-block-library', '' );
// Overwrite Core theme styles with empty styles.
wp_deregister_style( 'wp-block-library-theme' );
wp_register_style( 'wp-block-library-theme', '' );
},
10
);
Then I enqueue almost the same stylesheet for the editor than in the front-end. The goal is to avoid much manual work. You can compare these stylesheets in the Github:
By default these are the styles which needs some extra work in the editor:
For editor styles there needs to be .editor-styles-wrapper class added so that editor styles doesn’t leak outside the editor. There are couple of ways to do this automatically.
Nesting in SASS could look like this:
@import "settings/variables.css";
@import "tools/mixins.css";
// Editor CSS wrapper.
.editor-styles-wrapper {
@import "elements/index.css";
@import "blocks/index.css";
}
I personally use PostCSS editor styles plugin which basically adds .editor-styles-wrapper
class automatically to editor styles. Config for this can be seen on postcss.config.js file.
I also had a quick chat about with Torque Magazine. Thanks for the invite!