A place to cache linked articles (think custom and personal wayback machine)
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

8 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. title: Some little ways I’m using CSS :has() in the real world
  2. url: https://piccalil.li/blog/some-little-ways-im-using-css-has-in-the-real-world/
  3. hash_url: 529fce4c2b7c378f07aead94e62d3923
  4. archive_date: 2024-03-07
  5. og_image: https://api.urlbox.io/v1/ln9ptArKXobLRpDQ/png?width=1024&height=526&url=https%3A%2F%2Fpiccalil.li%2Fblog%2Fsome-little-ways-im-using-css-has-in-the-real-world%2F%2Fsocial-image&retina=true
  6. description: I’ve created some low fidelity demos of :has() snippets that I’ve been using in real-world client projects.
  7. favicon: https://piccalil.li/images/favicon-32x32.png
  8. language: en_US
  9. <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><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">
  10. <div class="wrapper">
  11. <div class="code-block__code">
  12. <pre id="code-block-0"><code class="language-css">.banner {
  13. background: var(--color-primary);
  14. color: var(--color-light);
  15. font-weight: var(--font-bold);
  16. text-align: center;
  17. }
  18. .banner:has(button) {
  19. display: flex;
  20. justify-content: space-between;
  21. gap: var(--space-s);
  22. text-align: revert;
  23. }
  24. </code></pre>
  25. </div>
  26. </div>
  27. </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">
  28. <div class="wrapper">
  29. <div class="code-block__code">
  30. <pre id="code-block-1"><code class="language-css">label::after {
  31. content: "\A";
  32. white-space: pre;
  33. }
  34. </code></pre>
  35. </div>
  36. </div>
  37. </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">
  38. <div class="wrapper">
  39. <div class="code-block__code">
  40. <pre id="code-block-2"><code class="language-css">label:has(input) {
  41. display: flex;
  42. align-items: flex-start;
  43. gap: var(--space-s);
  44. }
  45. </code></pre>
  46. </div>
  47. </div>
  48. </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">
  49. <div class="wrapper">
  50. <div class="code-block__code">
  51. <pre id="code-block-3"><code class="language-css">section:has(:target) {
  52. background: var(--color-light-shade);
  53. border: 2px solid var(--color-primary);
  54. }
  55. </code></pre>
  56. </div>
  57. </div>
  58. </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">
  59. <div class="wrapper">
  60. <div class="code-block__code">
  61. <pre id="code-block-4"><code class="language-css">.tiles:has(:hover) .tile:not(:hover) {
  62. opacity: 70%;
  63. }
  64. </code></pre>
  65. </div>
  66. </div>
  67. </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">
  68. <div class="wrapper">
  69. <div class="code-block__code">
  70. <pre id="code-block-5"><code class="language-css">.post p:has(code-pen) {
  71. max-width: unset;
  72. }
  73. </code></pre>
  74. </div>
  75. </div>
  76. </div><p>Easy peasy 🙂</p></div>