By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 semantic-css.html

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