Let's write more semantic CSS

2024-05-07, 13:50:41

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:

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:

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:

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