title: Style with Stateful, Semantic Selectors
url: https://benmyers.dev/blog/semantic-selectors/
hash_url: 99e7d2ba7e4adc69dbf0f1b2858a5248
archive_date: 2024-01-08
og_image: https://res.cloudinary.com/bendmyers/image/upload/v1656961915/benmyers.dev/semantic-selectors.png
description: See how building with accessible semantics from the get-go can give you expressive, meaningful style hooks for free.
favicon: https://benmyers.dev/favicon-32x32.png
language: en_US

<hgroup class="heading-wrapper">
<h2 id="introduction" tabindex="-1">Introduction</h2>
<a href="#introduction" rel="bookmark" aria-label="Permalink to “Introduction”">#</a></hgroup>
<p>In web development, we frequently need to style elements to visually indicate some state they're in. We give form fields red outlines to indicate invalid values. We show disabled or inactive elements in gray. We use any number of colors, icons, borders, and more to indicate what kind of state an element is in. Behind the hood, those visual styles are often handled by toggling <abbr>CSS</abbr> classes.</p>
<p>And yet: screenreaders, for instance, don't expose colors or borders or underlines or most of our other visual styles. They have no idea what our <code>.is-invalid</code> or <code>.selected</code> classes mean. This can pose an accessibility gap, since we have a discrepancy between visually-conveyed information and information conveyed via assistive technologies. <strong>If a state is important enough to indicate visually, it's probably important enough to expose to assistive technologies.</strong></p>
<p>Let's take <q>current page</q> indicators for nav links, for instance. It's a common practice to style the current page's link in a navbar, maybe coloring it differently or giving it a different border. This usually accomplishes two things:</p>
<ul>
<li>It orients the user as to where they are in the site's architecture (which is especially useful if they've come directly to that page from some external source like Google)</li>
<li>It indicates that they probably don't need to click that particular link lest they reload the page.</li>
</ul>
<p>Often, you might end up with markup like this:</p>
<pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>nav</span><span class="token punctuation">&gt;</span></span></span><br><mark class="highlight-line highlight-line-active">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/about<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>current-page<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>About<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></mark><br><span class="highlight-line"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/talks<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Talks<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/projects<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Projects<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/contact<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Contact Me<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>nav</span><span class="token punctuation">&gt;</span></span></span></code></pre>
<p>There's a problem here: that <q>current page</q> status would be super useful to screenreader users — after all, if a screenreader user reloads the current page, they might be flooded with announcements as the screenreader begins reading the page anew — and yet, screenreaders have no clue that this extra context exists.</p>
<p>We could address this with an <a href="/blog/aria/"><abbr>ARIA</abbr> state attribute</a> — specifically, setting <a href="https://www.aditus.io/aria/aria-current/"><code>aria-current="page"</code></a> on the link. When <code>aria-current="page"</code> is provided on a link, the screenreader will announce something like <q>link, About, current page.</q> The exact announcement may differ depending on which screenreader is used. Now, our markup looks like this:</p>
<pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>nav</span><span class="token punctuation">&gt;</span></span></span><br><mark class="highlight-line highlight-line-active">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/about<span class="token punctuation">"</span></span> <span class="token attr-name">aria-current</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>page<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>current-page<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>About<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></mark><br><span class="highlight-line"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/talks<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Talks<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/projects<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Projects<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/contact<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Contact Me<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>nav</span><span class="token punctuation">&gt;</span></span></span></code></pre>
<p>We've introduced a new problem, though. Now we have to keep track of <em>two</em> things: the class and the <abbr>ARIA</abbr> attribute. In theory, everything should be fine, so long as we always remember to keep these two in sync. But what if they diverge, and we end up with a link that has the <code>.current-page</code> class but not <code>aria-current="page"</code>? It happens — sighted developers are much more likely to remember the visual indication and less so the semantic indication. Or, admittedly less likely, we remember to add <code>aria-current="page"</code> but we accidentally omit our <code>.current-page</code> class? <strong>We forget things. Bugs happen.</strong></p>
<p>We can reduce the duplication and the risk of bugs, <a href="https://kentcdodds.com/blog/make-impossible-states-impossible">making impossible states impossible</a>, by instead using the <abbr>ARIA</abbr> attribute as our selector:</p>
<pre class="language-css" tabindex="0"><code class="language-css"><span class="highlight-line"><span class="token selector">a[aria-current="page"]</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  <span class="token property">border-bottom</span><span class="token punctuation">:</span> 7px solid yellow<span class="token punctuation">;</span>  </span><br><span class="highlight-line"><span class="token punctuation">}</span></span></code></pre>
<p>Now, to get the desired <em>visual</em> indication, we <em>have</em> to provide the necessary semantics for assistive technology. We can't get one without the other. I call this <strong>styling with stateful, semantic selectors</strong>. In my experience, it makes my code much more robust and ensures I don't accidentally omit necessary accessible semantics.</p>
<p>There are many ways you can style with stateful, semantic selectors, but I'd like to show you a few more examples I love:</p>
<hgroup class="heading-wrapper">
<h2 id="expandcollapse-triggers" tabindex="-1">Expand/Collapse Triggers</h2>
<a href="#expandcollapse-triggers" rel="bookmark" aria-label="Permalink to “Expand/Collapse Triggers”">#</a></hgroup>
<p>In this pattern, you have a button which, when clicked, will show or hide some content. Frequently, this button will have some visual indication of whether that content is currently being shown or not — often, it'll have a caret that's in one orientation when the content is expanded, and it's rotated when the content is collapsed.</p>
<p>As before, we <em>could</em> toggle that caret state with a CSS class — let's call it <code>.is-expanded</code>.</p>
<div class="side-by-side outer"><p class="side-by-side inner"></p><div class="side-by-side inner"><pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>is-expanded<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">   Additional Details</span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span></code></pre>
<pre class="language-css" tabindex="0"><code class="language-css"><span class="highlight-line"><span class="token selector">button::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  </span><br><span class="highlight-line">    </span><br><span class="highlight-line">    <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">'❯'</span><span class="token punctuation">;</span></span><br><span class="highlight-line">   <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span></span><br><span class="highlight-line">    <span class="token property">transition</span><span class="token punctuation">:</span> transform 0.2s<span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span><br><span class="highlight-line"></span><br><span class="highlight-line"><span class="token selector">button.is-expanded::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">    <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">rotate</span><span class="token punctuation">(</span>90deg<span class="token punctuation">)</span><span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span></code></pre>
</div></div>
<p>If you were to try to use the above button with a screenreader, you'd have an issue. When you press the button, the caret will rotate, but your screenreader will remain silent. It has no context that the button has hidden or revealed some content, so it says nothing. As a screenreader user, you're left without any feedback, and you may start to wonder whether you even clicked the button at all.</p>
<p>Fortunately, there's an <abbr>ARIA</abbr> state attribute for this exact purpose, called <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-expanded"><code>aria-expanded</code></a>! When a screenreader navigates to a button with <code>aria-expanded="false"</code>, it'll announce the button along with something like <q>collapsed,</q> such as <q>button, Additional Details, collapsed.</q> This tells screenreader users that the button controls toggling some content, and that content is currently hidden. When the attribute is toggled to <code>aria-expanded="true"</code>, the screenreader announcement will update to include <q>expanded</q> (or something to that effect), and say something like <q>button, Additional Details, expanded.</q></p>
<p>Our code might update to something like:</p>
<div class="side-by-side outer"><p class="side-by-side inner"></p><div class="side-by-side inner"><pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span></span></span><br><span class="highlight-line">  <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>is-expanded<span class="token punctuation">"</span></span></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"> <span class="token attr-name">aria-expanded</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span></span></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">   Additional Details</span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span></span></span></code></pre>
<pre class="language-css" tabindex="0"><code class="language-css"><span class="highlight-line"><span class="token selector">button::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  </span><br><span class="highlight-line">    </span><br><span class="highlight-line">    <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">'❯'</span><span class="token punctuation">;</span></span><br><span class="highlight-line">   <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span></span><br><span class="highlight-line">    <span class="token property">transition</span><span class="token punctuation">:</span> transform 0.2s<span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span><br><span class="highlight-line"></span><br><span class="highlight-line"><span class="token selector">button.is-expanded::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">    <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">rotate</span><span class="token punctuation">(</span>90deg<span class="token punctuation">)</span><span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span></code></pre>
</div></div>
<p>Upon every click of the button, a script toggles the <code>.is-expanded</code> class and flips <code>aria-expanded</code> between <code>"true"</code> and <code>"false"</code>. However… we're once again doing two things where we could be doing just one thing, and we're risking impossible states if <code>aria-expanded</code> and the <code>.is-expanded</code> class fall out of sync with each other.</p>
<p>Instead, let's use <code>aria-expanded</code> as the source of truth for our styles:</p>
<div class="side-by-side outer"><p class="side-by-side inner"></p><div class="side-by-side inner"><pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">aria-expanded</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"> Additional Details</span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span></code></pre>
</div></div>
<pre class="language-css" tabindex="0"><code class="language-css"><span class="highlight-line"><span class="token selector">button::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  </span><br><span class="highlight-line">    </span><br><span class="highlight-line">    <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">'❯'</span><span class="token punctuation">;</span></span><br><span class="highlight-line">   <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span></span><br><span class="highlight-line">    <span class="token property">transition</span><span class="token punctuation">:</span> transform 0.2s<span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span><br><span class="highlight-line"></span><br><span class="highlight-line"><span class="token selector">button[aria-expanded="true"]::before</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">rotate</span><span class="token punctuation">(</span>90deg<span class="token punctuation">)</span><span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span></code></pre>
<hgroup class="heading-wrapper">
<h2 id="sorted-table-columns" tabindex="-1">Sorted Table Columns</h2>
<a href="#sorted-table-columns" rel="bookmark" aria-label="Permalink to “Sorted Table Columns”">#</a></hgroup>
<p>Say you've got a sortable table, and you'd like to indicate which column the table is sorted by, and in which direction. No sweat — this time, we can use the <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-sort"><code>aria-sort</code></a> attribute.</p>
<p>The <code>aria-sort</code> attribute should be applied to at most <em>one</em> column header at a time. For most sortable tables <i>(sor-tables?)</i>, you'll want to apply <code>aria-sort="ascending"</code> to the sorted column's table header when the column is sorted in ascending order, apply <code>aria-sort="descending"</code> to the sorted column's table header when the column is sorted in descending order, and remove <code>aria-sort</code> from the table header altogether when the sort is cleared. When a screenreader user navigates to a table where a column header has <code>aria-sort="ascending"</code> or <code>aria-sort="descending"</code>, their screenreader will tell them the name of the sorted column and its direction.</p>
<p>Assuming you have buttons inside each of the table headers to let the user sort, your markup might look something like this:</p>
<pre class="language-html" tabindex="0"><code class="language-html"><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>table</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>thead</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">       <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tr</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span> <span class="token attr-name">scope</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>col<span class="token punctuation">"</span></span> <span class="token attr-name">aria-sort</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ascending<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">aria-describedby</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sort-description<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">                  Title</span><br><span class="highlight-line">               <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span> <span class="token attr-name">scope</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>col<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">              <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">aria-describedby</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sort-description<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">                  Author</span><br><span class="highlight-line">              <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span> <span class="token attr-name">scope</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>col<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">              <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">aria-describedby</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sort-description<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">                  ISBN-13</span><br><span class="highlight-line">             <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>tr</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>thead</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tbody</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line">       …</span><br><span class="highlight-line">   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>tbody</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>table</span><span class="token punctuation">&gt;</span></span></span><br><span class="highlight-line"></span><br><span class="highlight-line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sort-description<span class="token punctuation">"</span></span> <span class="token attr-name">hidden</span><span class="token punctuation">&gt;</span></span>Sort by column<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span></code></pre>
<p>If we wanted to add some arrows inside the sorted column's header's button to indicate the current sort direction, the <code>[aria-sort]</code> attribute selector will see us through:</p>
<pre class="language-css" tabindex="0"><code class="language-css"><span class="highlight-line"><span class="token selector">th[aria-sort="ascending"] button::after</span> <span class="token punctuation">{</span></span><br><span class="highlight-line"> </span><br><span class="highlight-line">    </span><br><span class="highlight-line">    <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">'↑'</span><span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span><br><span class="highlight-line"></span><br><span class="highlight-line"><span class="token selector">th[aria-sort="descending"] button::after</span> <span class="token punctuation">{</span></span><br><span class="highlight-line">  </span><br><span class="highlight-line">    </span><br><span class="highlight-line">    <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">'↓'</span><span class="token punctuation">;</span></span><br><span class="highlight-line"><span class="token punctuation">}</span></span></code></pre>
<p><i>(Simplified for the sake of demonstration. Check out <a href="https://adrianroselli.com/2021/04/sortable-table-columns.html">Adrian Roselli's sortable tables article</a> for a much more thorough and robust approach)</i></p>
<hgroup class="heading-wrapper">
<h2 id="and-more" tabindex="-1">And More!</h2>
<a href="#and-more" rel="bookmark" aria-label="Permalink to “And More!”">#</a></hgroup>
<p>These examples aren't the only times stateful, semantic selectors could prove helpful. Here are just a few more I didn't get into:</p>

<p>In short, <strong>building with accessible semantics from the get-go can give you expressive, meaningful style hooks for free.</strong> Leaning on those style hooks in your <abbr>CSS</abbr> selectors lets you reduce the number of moving parts in your site or application, and it can prevent accessibility bugs from creeping in down the road.</p>
<p>As with any web development recommendation, <strong>use your best judgment</strong>. If you find yourself <em>contorting</em> an element's semantics or markup to get the appearance you want, it's a sign to take a step back and revisit. Maybe classes are your friend, or maybe you need to revisit your design and determine whether it still makes sense.</p>
<hgroup class="heading-wrapper">

<a href="#related-reads" rel="bookmark" aria-label="Permalink to “Related Reads”">#</a></hgroup>
<p>I'm definitely not the first to write about this approach. Here are a few other articles on the subject I'd recommend reading:</p>
<ul>
<li><a href="https://css-tricks.com/user-facing-state/"><q>User Facing State</q> by Scott O'Hara on CSS Tricks</a></li>
<li><a href="https://adrianroselli.com/2021/06/using-css-to-enforce-accessibility.html"><q>Using CSS to Enforce Accessibility</q> by Adrian Roselli</a></li>
<li><a href="https://www.aleksandrhovhannisyan.com/blog/represent-state-with-html-attributes-not-class-names/"><q>Represent State with HTML Attributes, Not Class Names</q> by Aleksandr Hovhannisyan</a></li>
</ul>