semantic-css.html
HTML document, Unicode text, UTF-8 text
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4<meta charset="UTF-8"> 5<title> 6Let's write more semantic CSS 7</title> 8<link rel="stylesheet" href="/static/style.css"> 9<meta name="viewport" content="width=device-width, initial-scale=1.0"> 10</head> 11<body> 12<header> 13<nav> 14<ul> 15<li><a href="/">Home</a></li> 16<li><a href="/projects">Projects</a></li> 17<li><a href="/index">Index</a></li> 18<li><a href="/about">About</a></li> 19<li><a href="https://roundabout-host.com/roundabout">Roundabout-host</a></li> 20</ul> 21<ul> 22<li><a href="mailto:root@roundabout-host.com" id="mail-link">root@roundabout-host.com</a></li> 23</ul> 24</nav> 25</header> 26<main> 27 28<h1>Let's write more semantic CSS</h1> 29<div id="article-date">2024-05-18</div> 30<p class="tags"> 31 32<a href="/index/web.html" class="tag">web</a> 33 34<a href="/index/css.html" class="tag">css</a> 35 36<a href="/index/html.html" class="tag">html</a> 37 38</p> 39 40<article class="content-area"> 41<p>You probably wrote something like this at least once in your life: 42</p><pre data-language="html"><div class="card card--rounded card--primary"> 43<div class="card__image-container"> 44<img src="image.jpg" alt="A nice image" class="card__image"> 45<span class="card__image-caption">A nice image</span> 46</div> 47<div class="card__content"> 48<div class="card__header"> 49<div class="card__title">Hello, world!</div> 50</div> 51<p class="card__text"> 52Lorem ipsum dolor sit amet, consectetur adipiscing elit. 53</p> 54</div> 55<div class="card__footer"> 56<button class="btn btn--primary btn--raised btn--accent card__button card__button--primary">Click me!</button> 57<button class="btn btn--secondary btn--raised btn--accent card__button card__button--secondary">Click me!</button> 58</div> 59</div> 60</pre><p>Or this: 61</p><pre data-language="html"><div class="max-w-sm rounded overflow-hidden shadow-lg"> 62<div> 63<img class="w-full" src="image.jpg" alt="A nice image"> 64<span class="text-gray-500 text-base">A nice image</span> 65</div> 66<div class="px-6 py-4"> 67<div> 68<div class="font-bold text-xl mb-2">Hello, world!</div> 69</div> 70<p class="text-gray-700 text-base"> 71Lorem ipsum dolor sit amet, consectetur adipiscing elit. 72</p> 73</div> 74<div class="px-6 py-4"> 75<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click me!</button> 76<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> 77</div> 78</div> 79</pre><p>The second one is an adapted example from the <strong class="emphasis-2">Tailwind</strong> CSS docs. The first one is a variant that 80uses <strong class="emphasis-2">BEM</strong> instead. Both of them have <em class="emphasis-1">many</em> problems. 81</p><p>HTML has got over 100 elements you could use to structure your content. These examples use only 825: <code>div</code>, <code>span</code>, <code>p</code>, <code>img</code>, and <code>button</code>. This is not a problem in itself for small components, 83but it can indicate one. Using <code>div</code> and <code>span</code> for everything means you're misusing HTML. This 84is wrong: don't overlook HTML. JS or CSS may be more interesting, but the document language of 85the WWW is HTML. 86</p><p>The first example uses classes in place of elements. This creates extra work for both the HTML 87and CSS author. The CSS still mirrors the HTML structure, and the HTML is much more verbose than 88it needs to be. The word "button" or "btn" appears 8 times for each button. Ideally, it should 89appear two times: once in the opening tag and once in the closing tag. 90</p><p>The second example intentionally has the same markup tree as the first one. However, the classes 91changed a lot. Tailwind uses classes instead of CSS rules. It leads to repetition. If you don't 92want to repeat, you use components. But what if you don't do components? Then use <code>@apply</code> in 93CSS. Yes, CSS. So you're basically writing CSS only with a different syntax and less flexibility. 94</p><h2>A Simpler Way</h2><p>Let's strip the classes and focus on the markup tree for now. The two examples are identical in 95this regard. 96</p><pre data-language="html"><div> 97<div> 98<img src="image.jpg" alt="A nice image"> 99<span>A nice image</span> 100</div> 101<div> 102<div> 103<div>Hello, world!</div> 104</div> 105<p> 106Lorem ipsum dolor sit amet, consectetur adipiscing elit. 107</p> 108</div> 109<div> 110<button>Click me!</button> 111<button>Click me!</button> 112</div> 113</div> 114</pre><p>Now you see what I said? This tree is not semantic at all. Let's find the appropriate elements for 115each generic one. 116</p><pre data-language="html"><article> 117<figure> 118<img src="image.jpg" alt="A nice image"> 119<figcaption>A nice image</figcaption> 120</figure> 121<section> 122<header> 123<h2>Hello, world!</h2> 124</header> 125<p> 126Lorem ipsum dolor sit amet, consectetur adipiscing elit. 127</p> 128</section> 129<menu> 130<button>Click me!</button> 131<button>Click me!</button> 132</menu> 133</article> 134</pre><p>In case you're not familiar with the new elements, the quick meaning is: 135</p><ul><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article">article</a> - a self-contained 136piece of content that makes sense independently from the rest of the page 137</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure">figure</a> - a piece of content 138that is referenced from the main content, but can stand alone 139</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption">figcaption</a> - a caption 140for a <code>figure</code>'s other content (optional) 141</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section">section</a> - a thematic grouping 142of content, typically with a heading 143</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header">header</a> - header for the 144document or a smaller part of it, can include context, navigation or information about the 145content 146</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h2">h2</a> - a second-level section 147heading (you probably knew this one already) 148</p></li><li><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu">menu</a> - a list of commands 149available to take on a specific part of the content 150</p></li></ul><p>Please read the MDN articles I linked if you want to know more about these elements. 151</p><p>Depending on the other needs of your website or application, you will probably need to add a few 152classes. However, unlike the other examples, classes should be used as little as possible. Let's 153remember some things from the examples: 154</p><ul><li><p>The article is supposed to be a card and styled as such. 155</p></li><li><p>The first button is the primary action, and the second one is the secondary action. 156</p></li></ul><p>In this site, let's say not all articles are cards, but since this one <em class="emphasis-1">is</em> a card, we'll 157classify it as such. Let's also say that the secondary buttons are more common, this means we'll 158add a class to the primary button and style that later. 159</p><pre data-language="html"><article class="card"> 160<figure> 161<img src="image.jpg" alt="A nice image"> 162<figcaption>A nice image</figcaption> 163</figure> 164<section> 165<header> 166<h2>Hello, world!</h2> 167</header> 168<p> 169Lorem ipsum dolor sit amet, consectetur adipiscing elit. 170</p> 171</section> 172<menu> 173<button class="button-primary">Click me!</button> 174<button>Click me!</button> 175</menu> 176</article> 177</pre><p>Now, let's write a basic stylesheet for this. It won't look exactly like the second example for 178the sake of simplicity, but it could easily be made to look like that. We're going to use a CSS 179selector you've probably only seen in resets and to set the font on the <code>html</code> element, the 180tag selector. We're also going to use some new CSS smarts to make the styles more maintainable. 181</p><pre data-language="css">html, button, input, select, textarea { 182font-family: system-ui, sans-serif; 183} 184article.card { 185background-color: whitesmoke; 186border-radius: 12px; 187box-shadow: 0 0 4px #00000040; 188display: flex; 189flex-direction: column; 190gap: 1rem; 191overflow: hidden; 192} 193figure { 194display: flex; 195flex-direction: column; 196gap: 0.25rem; 197} 198figure > img { 199width: 100%; 200height: auto; 201} 202figcaption { 203font-style: italic; 204opacity: 0.875; 205} 206article.card > section { 207padding-left: 1rem; 208padding-right: 1rem; 209} 210article.card > menu, menu.buttonbox { 211display: flex; 212gap: 1rem; 213justify-content: flex-end; 214} 215button, .button, /* provide alternative where it makes sense, since we may want to make something else look like a button */ 216input:is([type="button"], [type="submit"], [type="reset"]) { 217background-color: white; 218color: orange; 219border: 4px solid currentColor; 220padding: 0.5rem 1rem; 221display: inline-flex; 222align-items: center; 223gap: 0.5rem; 224border: none; 225border-radius: 4px; /* Border radii are a decoration so pixels are fine */ 226} 227:is(button, .button, input:is([type="button"], [type="submit"], [type="reset"])).button-primary { 228background-color: orange; 229color: white; 230} 231</pre><p>Observations: 232</p><ul><li><p>We provide alternatives for some tag selectors where it makes sense, in case we want to make 233something else look like a button. However, we don't force using both when it's already clear: 234<code><button></code> will produce a styled button, same as <code><a class="button"></code>. <code><button class="button"></code> 235is redundant. 236</p></li><li><p>The <code>></code> child selector is used to avoid leaking styles in more complex nested layouts. 237</p></li><li><p>We use the <code>:is()</code> pseudo-class to group selectors that have the same styles. This is a new 238feature in CSS and it saves us from writing an enormous amount of combinations. 239</p></li></ul><h2>Conclusion</h2><p>Now, writing HTML is much easier: the CSS will adapt to what you intended to describe. The CSS 240is also much easier to maintain: the style can be changed easily without changing the HTML. The 241elements are always styled automatically, and you can copy-paste a snippet of plain HTML 242and have it magically match the rest of your site. 243</p><p>A more complete framework for this could add some layout container utilities. For example, a 244<code>grid</code> class that makes the element a grid container and uses <code>--width</code> and <code>--gap</code> custom 245properties to position the children. There could also be layout <em class="emphasis-1">elements</em> to use in place of 246divs like <code>x-hbox</code> and <code>x-vbox</code> that are flex containers. This would indicate the default style, 247and an additional class or ID would be used to make them responsive as well. Utility classes 248aren't bad, but they should be used for the things that can't cause repetition - which side a 249dialogue should emerge from, or whether to add padding in a generic row container. 250</p><h2>Frameworks Using Semantic CSS</h2><ul><li><p><a href="https://picocss.com/">Pico CSS</a> - does it very well, I should take some inspiration from it 251</p></li><li><p><a href="https://watercss.kognise.dev/">Water.css</a> - a very minimalistic CSS framework, primarily intended 252for publishing, but also includes interactive elements 253</p></li><li><p><a href="https://andybrewer.github.io/mvp/">MVP.css</a> - a basic stylesheet for plain HTML made for any 254site to look acceptable 255</p></li><li><p>The roundabout also uses semantic CSS. Once the API is stabilised a little the CSS will be 256released as a framework. 257</p></li><li><p>You might not even need a framework. 258</p></li></ul> 259</article> 260 261</main> 262<footer> 263<p>Page generated on Sunday, 2 February 2025 at 21:30:11</p> 264<p xmlns:cc="http://creativecommons.org/ns#" >This work is marked with <a href="https://creativecommons.org/publicdomain/zero/1.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC0 1.0 Universal</a> (🄍). No rights reserved.</p> 265<p>Hosted at <a href="https://roundabout-host.com/roundabout">Roundabout-host</a> using the static site service, and generated with <a href="/projects/ampoule.html">Ampoule</a>.</p> 266<a href="#">Back to top</a> 267</footer> 268</body> 269</html>