A place to cache linked articles (think custom and personal wayback machine)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.html 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!doctype html><!-- This is a valid HTML5 document. -->
  2. <!-- Screen readers, SEO, extensions and so on. -->
  3. <html lang="en">
  4. <!-- Has to be within the first 1024 bytes, hence before the `title` element
  5. See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
  6. <meta charset="utf-8">
  7. <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
  8. <!-- The viewport meta is quite crowded and we are responsible for that.
  9. See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
  10. <meta name="viewport" content="width=device-width,initial-scale=1">
  11. <!-- Required to make a valid HTML5 document. -->
  12. <title>Some little ways I’m using CSS :has() in the real world (archive) — David Larlet</title>
  13. <meta name="description" content="Publication mise en cache pour en conserver une trace.">
  14. <!-- That good ol' feed, subscribe :). -->
  15. <link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
  16. <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
  17. <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
  18. <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
  19. <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
  20. <link rel="manifest" href="/static/david/icons2/site.webmanifest">
  21. <link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
  22. <link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
  23. <meta name="msapplication-TileColor" content="#f7f7f7">
  24. <meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
  25. <meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
  26. <meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
  27. <!-- Is that even respected? Retrospectively? What a shAItshow…
  28. https://neil-clarke.com/block-the-bots-that-feed-ai-models-by-scraping-your-website/ -->
  29. <meta name="robots" content="noai, noimageai">
  30. <!-- Documented, feel free to shoot an email. -->
  31. <link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
  32. <!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
  33. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  34. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  35. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  36. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  37. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  38. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  39. <script>
  40. function toggleTheme(themeName) {
  41. document.documentElement.classList.toggle(
  42. 'forced-dark',
  43. themeName === 'dark'
  44. )
  45. document.documentElement.classList.toggle(
  46. 'forced-light',
  47. themeName === 'light'
  48. )
  49. }
  50. const selectedTheme = localStorage.getItem('theme')
  51. if (selectedTheme !== 'undefined') {
  52. toggleTheme(selectedTheme)
  53. }
  54. </script>
  55. <meta name="robots" content="noindex, nofollow">
  56. <meta content="origin-when-cross-origin" name="referrer">
  57. <!-- Canonical URL for SEO purposes -->
  58. <link rel="canonical" href="https://piccalil.li/blog/some-little-ways-im-using-css-has-in-the-real-world/">
  59. <body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">
  60. <article>
  61. <header>
  62. <h1>Some little ways I’m using CSS :has() in the real world</h1>
  63. </header>
  64. <nav>
  65. <p class="center">
  66. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  67. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  68. </svg> Accueil</a> •
  69. <a href="https://piccalil.li/blog/some-little-ways-im-using-css-has-in-the-real-world/" title="Lien vers le contenu original">Source originale</a>
  70. <br>
  71. Mis en cache le 2024-03-07
  72. </p>
  73. </nav>
  74. <hr>
  75. <section class="[ post ] [ p-summary ]" aria-label="Quick summary"><p><em class="color-dark-glare">I’ve created some low fidelity demos of :has() snippets that I’ve been using in real-world client projects.</em></p></section>
  76. <div class="[ post ] [ flow ]"><p>There’s a lot of chatter around the new(ish) <code>:has()</code> pseudo-class. It’s something we’ve been crying out for, for years: being able to select parent elements!</p><p>A useful mental model for <code>:has()</code> is that you are querying the parent’s children’s state and/or presence rather than selecting the parent from the children themselves. I like that. It makes a lot of sense.</p><p>I’m not 100% convinced <code>:has()</code> is the silver bullet others might claim it is though. I personally still utilise <a href="https://cube.fyi/exception.html">CUBE exceptions</a> more regularly, but I am also in the privileged position where projects I work on in <a href="https://set.studio/">the studio</a> don’t restrict access to the markup. I see <code>:has()</code> as being more useful for little tweaks more than anything, but if you don’t have access to markup, it really <em>is</em> a silver bullet.</p><p>With all that in mind, I thought I’d produce some low fidelity examples of how I’ve been using <code>:has()</code> lately on proper client projects to give you some real world stuff to look at. Let’s dig in.</p><h2 id="heading-banner-layout-adjustments">Banner layout adjustments<a href="#heading-banner-layout-adjustments" class="heading-permalink"><span class="visually-hidden"> permalink</span><svg fill="currentColor" aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z"></path></svg></a></h2><p>In a design system we work on for a client, there’s a pretty straightforward banner. This was recently updated to be dismissible, so we had to create a new variant to the pattern.</p><p>The only difference to the default pattern though is there’s a <code>&lt;button&gt;</code> element present, so a quick <code>:has()</code> query later, we could apply a flex layout in a jiffy.</p><div class="code-block" data-element="code-block">
  77. <div class="wrapper">
  78. <div class="code-block__code">
  79. <pre id="code-block-0"><code class="language-css">.banner {
  80. background: var(--color-primary);
  81. color: var(--color-light);
  82. font-weight: var(--font-bold);
  83. text-align: center;
  84. }
  85. .banner:has(button) {
  86. display: flex;
  87. justify-content: space-between;
  88. gap: var(--space-s);
  89. text-align: revert;
  90. }
  91. </code></pre>
  92. </div>
  93. </div>
  94. </div><p><code-pen id="PogPWRe" title="Banner layout demo"></code-pen></p><h2 id="heading-flex-labels-with-input-children">Flex labels with input children<a href="#heading-flex-labels-with-input-children" class="heading-permalink"><span class="visually-hidden"> permalink</span><svg fill="currentColor" aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z"></path></svg></a></h2><p>The context for this is that I like the following pattern for labels because I like to keep them as <code>inline</code> elements, but form fields that follow them should break on to a new line.</p><div class="code-block" data-element="code-block">
  95. <div class="wrapper">
  96. <div class="code-block__code">
  97. <pre id="code-block-1"><code class="language-css">label::after {
  98. content: "\A";
  99. white-space: pre;
  100. }
  101. </code></pre>
  102. </div>
  103. </div>
  104. </div><p>For labels that contain inputs like checkboxes and radios though, it’s useful to render those as flexbox layouts. Historically, that would require a class being added (or several in <abbr title="Atomic Style Sheets">ASS</abbr> codebases), but now, I’ve updated our global styles to this instead.</p><div class="code-block" data-element="code-block">
  105. <div class="wrapper">
  106. <div class="code-block__code">
  107. <pre id="code-block-2"><code class="language-css">label:has(input) {
  108. display: flex;
  109. align-items: flex-start;
  110. gap: var(--space-s);
  111. }
  112. </code></pre>
  113. </div>
  114. </div>
  115. </div><fyi-unit><p>The reason I only look for <code>input</code> is because I never put text inputs inside labels. If you do that, you should update the selector to this instead: <code>label:has(:is(input[type="checkbox"], input[type="radio"]))</code>.</p></fyi-unit><p><code-pen id="bGJVqgX" title="A label with text input followed by a label containing a checkbox"></code-pen></p><h2 id="heading-highlight-parent-elements-when-their-children-are-targeted">Highlight parent elements when their children are targeted<a href="#heading-highlight-parent-elements-when-their-children-are-targeted" class="heading-permalink"><span class="visually-hidden"> permalink</span><svg fill="currentColor" aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z"></path></svg></a></h2><p>This one is super quick and super simple. If you’ve got an element with an <code>id</code>, you can trigger its <code>:target</code> state by appending it’s id to the URL with a <code>#</code>, like this: <code>https://example.com/#my-element</code>.</p><p>Historically, you couldn’t apply styles to an element’s parent when it’s targeted, but now you can with <code>:has()</code>.</p><div class="code-block" data-element="code-block">
  116. <div class="wrapper">
  117. <div class="code-block__code">
  118. <pre id="code-block-3"><code class="language-css">section:has(:target) {
  119. background: var(--color-light-shade);
  120. border: 2px solid var(--color-primary);
  121. }
  122. </code></pre>
  123. </div>
  124. </div>
  125. </div><p>Handy as heck.</p><p><code-pen id="eYopgwJ" title="A section is highlighted when its heading is targeted"></code-pen></p><h2 id="heading-dimmer-siblings-when-an-element-is-hovered">Dimmer siblings when an element is hovered<a href="#heading-dimmer-siblings-when-an-element-is-hovered" class="heading-permalink"><span class="visually-hidden"> permalink</span><svg fill="currentColor" aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z"></path></svg></a></h2><p>It’s a design pattern that’s been on the web forever. The idea is when you hover an element its siblings all dim, so the user’s visual focus is more targeted.</p><p>We’ve been able to do this with CSS forever too, but the selectors to achieve the effect were pretty gnarly. Quite a few approaches resulted in flickering or all items dimmed if your pointer accidentally found itself in gutters too.</p><p>It’s not the case any more with <code>:has()</code> though!</p><div class="code-block" data-element="code-block">
  126. <div class="wrapper">
  127. <div class="code-block__code">
  128. <pre id="code-block-4"><code class="language-css">.tiles:has(:hover) .tile:not(:hover) {
  129. opacity: 70%;
  130. }
  131. </code></pre>
  132. </div>
  133. </div>
  134. </div><p>The beauty of this selector is it’s <em>really</em> clear what’s going on too.</p><p><code-pen id="ZEZbeGW" title="Other tiles dim when one of them is hovered"></code-pen></p><p>Yeh, there’s nothing complicated or fancy in this article, but I just wanted to show some handy real-world ways to use <code>:has()</code>. If you really want to get into <code>:has()</code>, I strongly recommend checking out <a href="https://ishadeed.com/article/css-has-guide">Ahmad’s interactive guide</a>. It’s fantastic!</p><p>P.S. one last little trick. On this site, paragraphs in the <code>.post</code> block are limited to <code>60ch</code> as a <code>max-width</code>. That's not ideal for demos though, so…</p><div class="code-block" data-element="code-block">
  135. <div class="wrapper">
  136. <div class="code-block__code">
  137. <pre id="code-block-5"><code class="language-css">.post p:has(code-pen) {
  138. max-width: unset;
  139. }
  140. </code></pre>
  141. </div>
  142. </div>
  143. </div><p>Easy peasy 🙂</p></div>
  144. </article>
  145. <hr>
  146. <footer>
  147. <p>
  148. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  149. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  150. </svg> Accueil</a> •
  151. <a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
  152. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
  153. </svg> Suivre</a> •
  154. <a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
  155. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
  156. </svg> Pro</a> •
  157. <a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
  158. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
  159. </svg> Email</a> •
  160. <abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
  161. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
  162. </svg> Légal</abbr>
  163. </p>
  164. <template id="theme-selector">
  165. <form>
  166. <fieldset>
  167. <legend><svg class="icon icon-brightness-contrast">
  168. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
  169. </svg> Thème</legend>
  170. <label>
  171. <input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
  172. </label>
  173. <label>
  174. <input type="radio" value="dark" name="chosen-color-scheme"> Foncé
  175. </label>
  176. <label>
  177. <input type="radio" value="light" name="chosen-color-scheme"> Clair
  178. </label>
  179. </fieldset>
  180. </form>
  181. </template>
  182. </footer>
  183. <script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
  184. <script>
  185. function loadThemeForm(templateName) {
  186. const themeSelectorTemplate = document.querySelector(templateName)
  187. const form = themeSelectorTemplate.content.firstElementChild
  188. themeSelectorTemplate.replaceWith(form)
  189. form.addEventListener('change', (e) => {
  190. const chosenColorScheme = e.target.value
  191. localStorage.setItem('theme', chosenColorScheme)
  192. toggleTheme(chosenColorScheme)
  193. })
  194. const selectedTheme = localStorage.getItem('theme')
  195. if (selectedTheme && selectedTheme !== 'undefined') {
  196. form.querySelector(`[value="${selectedTheme}"]`).checked = true
  197. }
  198. }
  199. const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
  200. window.addEventListener('load', () => {
  201. let hasDarkRules = false
  202. for (const styleSheet of Array.from(document.styleSheets)) {
  203. let mediaRules = []
  204. for (const cssRule of styleSheet.cssRules) {
  205. if (cssRule.type !== CSSRule.MEDIA_RULE) {
  206. continue
  207. }
  208. // WARNING: Safari does not have/supports `conditionText`.
  209. if (cssRule.conditionText) {
  210. if (cssRule.conditionText !== prefersColorSchemeDark) {
  211. continue
  212. }
  213. } else {
  214. if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
  215. continue
  216. }
  217. }
  218. mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
  219. }
  220. // WARNING: do not try to insert a Rule to a styleSheet you are
  221. // currently iterating on, otherwise the browser will be stuck
  222. // in a infinite loop…
  223. for (const mediaRule of mediaRules) {
  224. styleSheet.insertRule(mediaRule.cssText)
  225. hasDarkRules = true
  226. }
  227. }
  228. if (hasDarkRules) {
  229. loadThemeForm('#theme-selector')
  230. }
  231. })
  232. </script>
  233. </body>
  234. </html>