An alt Decision Tree Using Only :has()

I use the CSS :has() pseudo-class to provide an interactive alt text decision tree (from the W3C WAI Tutorial) that uses no script. It is progressively enhanced, so browsers without support for :has() still get all the content.

See my post Under-Engineered Dependency Questions if you want my argument why this approach is fine under WCAG even if not ideal for a general audience. You may also recall that I argue CSS-only Widgets Are Inaccessible, but I assert that this one is accessible if maybe not as usable as it could be.

On to the form…

Does the image contain text?

If yes, the image contains text…

If no, the image contains no text…

Is the image used in a link or a button, and would it be hard or impossible to understand what the link or the button does, if the image wasn’t there?

If yes, the image is contained in a control…

If no, the image is not contained in a control…

Does the image contribute meaning to the current page or context?

If yes, the image contributes meaning…

If no, the image contributes no meaning…

Is the image purely decorative or not intended for the user?

If yes, the image is purely decorative…

If no, the image is not purely decorative…

Is the image’s use not listed above or it’s unclear what alt text to provide?

If yes, the image’s use is not listed above…

If no, because the image’s use is listed above…

Standalone Version

You can fork and mess with this on CodePen, or test with your own favorite tools using the debug mode.

See the Pen An alt Decision Tree Using Only :has() by Adrian Roselli (@aardrian) on CodePen.

Absent Support

For browsers that do not support :has(), those users will get all the content but the form fields will do nothing. I opted to leave the fields there in case that set of users found it helpful to toggle the radio buttons as a visual cue to help more easily decide which content applies to their case.

Side-by-side comparison of two web pages; the first shows all the possible content even with three questions answered while the second shows content that answers the third question alone, resulting in a much shorter page.
This image shows how the entirety of all the content is visible when the browser does not support :has() (Firefox 116) versus when a browser supports :has() (Chrome 115) and only content relevant to the user’s responses are shown.

Again, this is not something you would necessarily do for general users on a real site. This is my playing around with, and testing, :has() support.

2 Comments

Reply

Neat!

FYI, in Safari (macOS & iOS), legend is very narrow. At first I thought it had a user agent stylesheet with width: min-content but then I saw the fieldset was a CSS Grid, it goes away if that’s removed. The Inspector’s grid visualizer shows the legend as being part of the border, not the grid area. I tried a couple of width and grid-column properties on the legend but didn’t find a fix for what seems to be a Safari bug.

Curtis Wilcox; . Permalink
In response to Curtis Wilcox. Reply

Yeah, I saw that when I did a run through on my Mac before posting. Then I decided, meh, nobody would look today and I could sort it later. Oops. I appreciate you doing some legwork now so I won’t spin my wheels on those properties later.

Leave a Comment or Response

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>