You probably wrote something like this at least once in your life:
<div class="card card--rounded card--primary"> <div class="card__image-container"> <img src="image.jpg" alt="A nice image" class="card__image"> <span class="card__image-caption">A nice image</span> </div> <div class="card__content"> <div class="card__header"> <div class="card__title">Hello, world!</div> </div> <p class="card__text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div class="card__footer"> <button class="btn btn--primary btn--raised btn--accent card__button card__button--primary">Click me!</button> <button class="btn btn--secondary btn--raised btn--accent card__button card__button--secondary">Click me!</button> </div> </div>
Or this:
<div class="max-w-sm rounded overflow-hidden shadow-lg"> <div> <img class="w-full" src="image.jpg" alt="A nice image"> <span class="text-gray-500 text-base">A nice image</span> </div> <div class="px-6 py-4"> <div> <div class="font-bold text-xl mb-2">Hello, world!</div> </div> <p class="text-gray-700 text-base"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div class="px-6 py-4"> <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click me!</button> <button class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded">Click me!</button> </div> </div>
The second one is an adapted example from the Tailwind CSS docs. The first one is a variant that uses BEM instead. Both of them have many problems.
HTML has got over 100 elements you could use to structure your content. These examples use only
5: div
, span
, p
, img
, and button
. This is not a problem in itself for small components,
but it can indicate one. Using div
and span
for everything means you're misusing HTML. This
is wrong: don't overlook HTML. JS or CSS may be more interesting, but the document language of
the WWW is HTML.
The first example uses classes in place of elements. This creates extra work for both the HTML and CSS author. The CSS still mirrors the HTML structure, and the HTML is much more verbose than it needs to be. The word "button" or "btn" appears 8 times for each button. Ideally, it should appear two times: once in the opening tag and once in the closing tag.
The second example intentionally has the same markup tree as the first one. However, the classes
changed a lot. Tailwind uses classes instead of CSS rules. It leads to repetition. If you don't
want to repeat, you use components. But what if you don't do components? Then use @apply
in
CSS. Yes, CSS. So you're basically writing CSS only with a different syntax and less flexibility.
A Simpler Way
Let's strip the classes and focus on the markup tree for now. The two examples are identical in this regard.
<div> <div> <img src="image.jpg" alt="A nice image"> <span>A nice image</span> </div> <div> <div> <div>Hello, world!</div> </div> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div> <button>Click me!</button> <button>Click me!</button> </div> </div>
Now you see what I said? This tree is not semantic at all. Let's find the appropriate elements for each generic one.
<article> <figure> <img src="image.jpg" alt="A nice image"> <figcaption>A nice image</figcaption> </figure> <section> <header> <h2>Hello, world!</h2> </header> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </section> <menu> <button>Click me!</button> <button>Click me!</button> </menu> </article>
In case you're not familiar with the new elements, the quick meaning is:
article - a self-contained piece of content that makes sense independently from the rest of the page
figure - a piece of content that is referenced from the main content, but can stand alone
figcaption - a caption for a
figure
's other content (optional)section - a thematic grouping of content, typically with a heading
header - header for the document or a smaller part of it, can include context, navigation or information about the content
h2 - a second-level section heading (you probably knew this one already)
menu - a list of commands available to take on a specific part of the content
Please read the MDN articles I linked if you want to know more about these elements.
Depending on the other needs of your website or application, you will probably need to add a few classes. However, unlike the other examples, classes should be used as little as possible. Let's remember some things from the examples:
The article is supposed to be a card and styled as such.
The first button is the primary action, and the second one is the secondary action.
In this site, let's say not all articles are cards, but since this one is a card, we'll classify it as such. Let's also say that the secondary buttons are more common, this means we'll add a class to the primary button and style that later.
<article class="card"> <figure> <img src="image.jpg" alt="A nice image"> <figcaption>A nice image</figcaption> </figure> <section> <header> <h2>Hello, world!</h2> </header> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </section> <menu> <button class="button-primary">Click me!</button> <button>Click me!</button> </menu> </article>
Now, let's write a basic stylesheet for this. It won't look exactly like the second example for
the sake of simplicity, but it could easily be made to look like that. We're going to use a CSS
selector you've probably only seen in resets and to set the font on the html
element, the
tag selector. We're also going to use some new CSS smarts to make the styles more maintainable.
html, button, input, select, textarea { font-family: system-ui, sans-serif; } article.card { background-color: whitesmoke; border-radius: 12px; box-shadow: 0 0 4px #00000040; display: flex; flex-direction: column; gap: 1rem; overflow: hidden; } figure { display: flex; flex-direction: column; gap: 0.25rem; } figure > img { width: 100%; height: auto; } figcaption { font-style: italic; opacity: 0.875; } article.card > section { padding-left: 1rem; padding-right: 1rem; } article.card > menu, menu.buttonbox { display: flex; gap: 1rem; justify-content: flex-end; } button, .button, /* provide alternative where it makes sense, since we may want to make something else look like a button */ input:is([type="button"], [type="submit"], [type="reset"]) { background-color: white; color: orange; border: 4px solid currentColor; padding: 0.5rem 1rem; display: inline-flex; align-items: center; gap: 0.5rem; border: none; border-radius: 4px; /* Border radii are a decoration so pixels are fine */ } :is(button, .button, input:is([type="button"], [type="submit"], [type="reset"])).button-primary { background-color: orange; color: white; }
Observations:
We provide alternatives for some tag selectors where it makes sense, in case we want to make something else look like a button. However, we don't force using both when it's already clear:
<button>
will produce a styled button, same as<a class="button">
.<button class="button">
is redundant.The
>
child selector is used to avoid leaking styles in more complex nested layouts.We use the
:is()
pseudo-class to group selectors that have the same styles. This is a new feature in CSS and it saves us from writing an enormous amount of combinations.
Conclusion
Now, writing HTML is much easier: the CSS will adapt to what you intended to describe. The CSS is also much easier to maintain: the style can be changed easily without changing the HTML. The elements are always styled automatically, and you can copy-paste a snippet of plain HTML and have it magically match the rest of your site.
A more complete framework for this could add some layout container utilities. For example, a
grid
class that makes the element a grid container and uses --width
and --gap
custom
properties to position the children. There could also be layout elements to use in place of
divs like x-hbox
and x-vbox
that are flex containers. This would indicate the default style,
and an additional class or ID would be used to make them responsive as well. Utility classes
aren't bad, but they should be used for the things that can't cause repetition - which side a
dialogue should emerge from, or whether to add padding in a generic row container.
Frameworks Using Semantic CSS
Pico CSS - does it very well, I should take some inspiration from it
Water.css - a very minimalistic CSS framework, primarily intended for publishing, but also includes interactive elements
MVP.css - a basic stylesheet for plain HTML made for any site to look acceptable
The roundabout also uses semantic CSS. Once the API is stabilised a little the CSS will be released as a framework.
You might not even need a framework.