|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- title: Some little ways I’m using CSS :has() in the real world
- url: https://piccalil.li/blog/some-little-ways-im-using-css-has-in-the-real-world/
- hash_url: 529fce4c2b7c378f07aead94e62d3923
- archive_date: 2024-03-07
- 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
- description: I’ve created some low fidelity demos of :has() snippets that I’ve been using in real-world client projects.
- favicon: https://piccalil.li/images/favicon-32x32.png
- language: en_US
-
- <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><button></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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-0"><code class="language-css">.banner {
- background: var(--color-primary);
- color: var(--color-light);
- font-weight: var(--font-bold);
- text-align: center;
- }
-
- .banner:has(button) {
- display: flex;
- justify-content: space-between;
- gap: var(--space-s);
- text-align: revert;
- }
- </code></pre>
- </div>
- </div>
- </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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-1"><code class="language-css">label::after {
- content: "\A";
- white-space: pre;
- }
- </code></pre>
- </div>
- </div>
- </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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-2"><code class="language-css">label:has(input) {
- display: flex;
- align-items: flex-start;
- gap: var(--space-s);
- }
- </code></pre>
- </div>
- </div>
- </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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-3"><code class="language-css">section:has(:target) {
- background: var(--color-light-shade);
- border: 2px solid var(--color-primary);
- }
- </code></pre>
- </div>
- </div>
- </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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-4"><code class="language-css">.tiles:has(:hover) .tile:not(:hover) {
- opacity: 70%;
- }
- </code></pre>
- </div>
- </div>
- </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">
-
- <div class="wrapper">
- <div class="code-block__code">
- <pre id="code-block-5"><code class="language-css">.post p:has(code-pen) {
- max-width: unset;
- }
- </code></pre>
- </div>
- </div>
- </div><p>Easy peasy 🙂</p></div>
|