Test365i
365i.co.uk
Back to all guides
CSS

Badges that colour themselves with the :has() selector

The :has() selector lets an element style itself by what it contains. This guide uses it to colour topic badges by the link inside them, which is how every badge on this site gets its colour, including the ones inside Elementor 4 loops where you cannot add a class per item.

10 minutesCustom code

Works on WordPress 7.2, Elementor 4.4.2; :has() works in Chrome and Edge 105+, Safari 15.4+ and Firefox 121+

Editorial integrity: read our editorial standards.

Live demo, running the code from this guide on a bare page with no theme styles.
  1. Style a neutral badge

    Start with one plain style that every badge shares: a pill shape, a quiet border and a small monospace label. The link inside inherits the badge colour, so the whole pill changes colour together in the next step. You can see the finished badges running in the Elementor 4 element gallery.

    badges.cssCSS
    .badge {
    	display: inline-flex;
    	align-items: center;
    	min-height: 22px;
    	padding: 3px 10px;
    	border-radius: 999px;
    	border: 1px solid #66667D;
    	font: 500 12px/1rem ui-monospace, monospace;
    	color: #BBBBD1;
    }
    
    .badge a {
    	color: inherit;
    	text-decoration: none;
    }
    After step 1: every badge looks the same, and the empty fourth one still shows.
  2. Colour each badge from its link with :has()

    .badge:has(a[href*="/type/css/"]) reads as: a badge that contains a link whose address includes /type/css/. One rule per topic colours the text, border and a faint fill. color-mix() makes the border and fill from the same colour, so each badge needs only one value.

    The colours are the measured ones from the Test365i design system; each passes WCAG 2.2 AA on this site's dark panels.

    A badge containing a link to /lab/type/css/ is matched by .badge:has() and turns green; Layout turns amber and Elementor pink
    The badge checks where its link points and colours itself to match.
    badges.cssCSS
    .badge:has(a[href*="/type/css/"]) {
    	color: #4FFAB5;
    	border-color: color-mix(in oklab, #4FFAB5 40%, transparent);
    	background-color: color-mix(in oklab, #4FFAB5 10%, transparent);
    }
    
    .badge:has(a[href*="/type/layout/"]) {
    	color: #FFC54D;
    	border-color: color-mix(in oklab, #FFC54D 40%, transparent);
    	background-color: color-mix(in oklab, #FFC54D 10%, transparent);
    }
    
    .badge:has(a[href*="/type/elementor/"]) {
    	color: #F757BD;
    	border-color: color-mix(in oklab, #F757BD 40%, transparent);
    	background-color: color-mix(in oklab, #F757BD 10%, transparent);
    }
    After step 2: each badge picks its own colour from where its link points.

    MDN describes the selector plainly:

    a way of selecting a parent element or a previous sibling element

    MDN Web Docs, :has() reference

    "Selecting a parent" is the part CSS could not do for decades. Before :has(), colouring a badge by its link meant a class on the badge, added by PHP or JavaScript. Here the badge is selected because of its child, so the markup stays exactly as the loop produces it.

  3. Hide badges that come out empty

    Dynamic content is sometimes empty. A post with no topic would leave a blank pill behind, so :empty removes it. On this site the same rule tidies every loop card whose field has no value.

    badges.cssCSS
    .badge:empty {
    	display: none;
    }
    After step 3: the empty badge is gone, with nothing to remove by hand.
  4. Use it inside an Elementor 4 loop

    In Elementor 4, add a Paragraph element, set its HTML tag to span, give it the class badge, and set both its text and its link with dynamic tags: the term name, and the term link. This is the case that made me use :has(). When I built the Lab guide list, the atomic loop items carried no per-post classes, so there was no class to key a colour on. The link was the one thing that differed, and :has() can read it.

    Outside Elementor, the markup is a span around a link:

    A badge paragraph inside a Collection loop item, with its text and link set by dynamic tags
    The link is the only thing that changes from post to post.
    index.htmlHTML
    <span class="badge"><a href="/lab/type/css/">CSS</a></span>
    <span class="badge"><a href="/lab/type/layout/">Layout</a></span>
    <span class="badge"><a href="/lab/type/elementor/">Elementor</a></span>

    Jen Simmons of the WebKit team made the same point when Safari shipped it:

    After decades of dead-ends, this selector can do far more.

    Jen Simmons, WebKit blog, "Using :has() as a CSS Parent Selector and much more"

    This guide only uses the simplest form, a parent checking a child. The same idea styles a card that contains an image, a form that contains an invalid field, or a menu item whose panel is open. Once a page builder controls your markup, reading the content you have beats asking for markup you cannot get.

What the :has() selector does not do here

It matches what is in the HTML, so the link address has to contain something you can key on, such as /type/css/. If two topics share part of an address, use a longer match.

Browsers older than Chrome 105, Safari 15.4 or Firefox 121 ignore the colour rules and show the neutral badge from step 1, which still works.

Each colour is one rule, so a site with forty topics would want a different approach, such as a class added at render time.

All the :has() badge code

Each file below is complete. Copy it, or download it under the name shown.

badges.cssCSS
.badge {
	display: inline-flex;
	align-items: center;
	min-height: 22px;
	padding: 3px 10px;
	border-radius: 999px;
	border: 1px solid #66667D;
	font: 500 12px/1rem ui-monospace, monospace;
	color: #BBBBD1;
}

.badge a {
	color: inherit;
	text-decoration: none;
}

.badge:has(a[href*="/type/css/"]) {
	color: #4FFAB5;
	border-color: color-mix(in oklab, #4FFAB5 40%, transparent);
	background-color: color-mix(in oklab, #4FFAB5 10%, transparent);
}

.badge:has(a[href*="/type/layout/"]) {
	color: #FFC54D;
	border-color: color-mix(in oklab, #FFC54D 40%, transparent);
	background-color: color-mix(in oklab, #FFC54D 10%, transparent);
}

.badge:has(a[href*="/type/elementor/"]) {
	color: #F757BD;
	border-color: color-mix(in oklab, #F757BD 40%, transparent);
	background-color: color-mix(in oklab, #F757BD 10%, transparent);
}

.badge:empty {
	display: none;
}
index.htmlHTML
<span class="badge"><a href="/lab/type/css/">CSS</a></span>
<span class="badge"><a href="/lab/type/layout/">Layout</a></span>
<span class="badge"><a href="/lab/type/elementor/">Elementor</a></span>

:has() selector badge questions

Is :has() a parent selector?

It can be used as one: it selects an element based on what it contains. It can also select earlier siblings.

Which browsers support :has()?

Chrome and Edge from 105, Safari from 15.4 and Firefox from 121. Older browsers show the neutral badge.

Why not just add a class to each badge?

In an Elementor 4 loop the items carry no per-post classes, so there is no class to add. The link already differs per post, so :has() reads that instead.

Does :has() slow pages down?

Not at this scale. A few attribute checks inside small badges are cheap. Avoid very broad patterns such as body:has(...) on large pages.

Can I colour by something other than a link?

Yes. Any attribute or child works, for example an image inside a card or a checked input inside a label.

What does color-mix() do here?

It mixes the badge colour with transparent, so the border uses 40% of the colour and the fill 10%, from one value.

Can I use this in a Gutenberg or classic theme?

Yes. The CSS works anywhere; you only need the badge markup with a link inside.

Can the badges glow?

Yes. A small coloured box-shadow in the badge colour gives a soft glow. Box Shadow Generator, made by Mark McNeece of 365i, builds one you can copy.

Sources

  1. MDN Web Docs: :has()
  2. Jen Simmons, WebKit: Using :has() as a CSS Parent Selector and much more
  3. Ahmad Shadeed: CSS :has() Interactive Guide

More build guides

The latest guides, newest first.

Elementor

A clickable card grid with the Elementor 4 Collection loop

The Elementor 4 Collection loop repeats one design for every post a query finds. On this site it lists the build guides, which are a JetEngine post type, in the element gallery, the Lab and the mega menu, all from one card component. This guide builds that grid, makes each whole card clickable and hides fields that come back empty.

25 minutes Get the guide
Elementor

Style the Elementor 4 Carousel element: arrows, dots and loop

The Elementor 4 Carousel element gives you slides, arrows and dots as separate atomic parts. Out of the box on this site, the arrows sat on top of the slide titles, the active dot was black on a dark panel and Loop did nothing. This guide fixes all three, with the CSS the element gallery now runs.

20 minutes Get the guide
Elementor

The Elementor 4 Accordion element, built on native details

The Elementor 4 Accordion element is built on the browser's own details and summary elements, so it opens and closes with no script. This guide sets it up the way the element gallery on this site does, styles it with classes, and explains the FAQ schema switch and what Google now does with it.

15 minutes Get the guide