Developing an alt text button for images on my website
25 points by capjamesg
25 points by capjamesg
Please use details
and summary
elements instead. Semantic HTML is still a nice to have instead of div soup. (See this example on my website)
Oooh, it’s good to see an example of the details
tag for this purpose. I can’t remember the reason why I didn’t use it (although it may have been that I wanted an excuse to try the :has
selector).
In all cases of HTML, semantics++ :)
I’m not seeing the I thought I could just “View Source” to see it, but instead I had to highlight the image, then select “View Selection Source” to see the actual, generated HTML.details
and summary
elements in the source for that page. What I am seeing are some custom tags and a bunch of Javascript and JSON.
For anyone else, the HTML/CSS that powers that appears to be here.
It really is not complicated. See the documentation on MDN, it even has a live playground to test things out.
If you want to see it in the wild, my online resume is stuffed with it. It is a simple way to combine overviews with detailed descriptions.
Yes, sorry. I was thinking everyone can just look at it using developer tools, to see it rendered.
That site makes heavy use of web components rendered based on ActivityPub JavaScript objects.
Why are we not using the alt attribute? Am I missing something, or is this just an article by someone who has fully missed the point of alt text? I want to be missing something, but I don’t think I am.
If someone’s provided a useful description of an image I’d like to read it even though I can see the image itself (I’ve learned the original source movies/TV shows for a lot of memes that way). But on my phone or tablet I have no way to “hover” and trigger the normal display of the alt text.
I suspect this is why several social media sites now have an explicit item you can tap to show the text without needing to turn on assistive technology on your device.
The alt
attribute is there, but I think the point is that browsers don’t commonly display its content unless you’re using assistive technology, so a button like the one OP created is helpful for reading the alt
text without looking at the source code.
I haven’t finished the article yet, but for me, when the image is successfully loaded or assistive tech is not used, alt text is hidden from the user, but it can be still be useful for sighted users. As a sighted person, I like reading alt text on Bluesky and enjoy them a lot. And I find them useful when reading a comic strip for example, and I can’t comprehend something. (This has happened with Haus of Decline comics (who writes excellent alt text by the way) when I don’t pay a lot of attention, or when there’s a known figure but I don’t recognize them.)
Exactly! I have found a lot of well written alt text by friends on Mastodon that I find helpful to read. I wanted to provide that same experience to people who read my website. I find alt text especially helpful for games where I may not know the exact game by sight but the alt text helps tells me and paints the picture.
Original commenter: Great question! This is not a replacement for alt text. Screen readers get the alt text as normal and those who do not rely on assistive technologies can also read the alt text.
I tried to reproduce without any additional markup, but couldn’t use the img[alt]
attribute (without JS):
img
tag cannot have a child (so no img::after
)picture
doesn’t help much: content
cannot retrieve alt
of the childIf duplicating the alt
attribute is acceptable, here is how it could look like: (using `:hover’, maybe it is possible to make it clickable?)
<picture alt="Fernande with a Black Mantilla, one of my favourite paintings">
<img alt="Fernande with a Black Mantilla, one of my favourite paintings" src="https://www.guggenheim.org/wp-content/uploads/1905/01/91.3914_ph_web-1.jpg">
</picture>
img {
width: 15em;
}
picture{
position: relative;
}
picture::after{
content: attr(alt);
display: block;
position: absolute;
bottom:0;
left:0;
right:0;
padding:.75rem;
background-color: rgba(255,255,255,0.7);
opacity:0;
max-height: 5em;
overflow-y:auto;
font-size:80%;
}
picture:hover::after{
opacity:1;
}
Did you consider using the title
attribute for a hover tooltip?
The HTML reference on MDN notes the following accessibility concerns with the title
attribute:
Use of the title attribute is highly problematic for:
- People using touch-only devices
- People navigating with keyboards
- People navigating with assistive technology such as screen readers or magnifiers
- People experiencing fine motor control impairment
- People with cognitive concerns
This is due to inconsistent browser support, compounded by the additional assistive technology parsing of the browser-rendered page. If a tooltip effect is desired, it is better to use a more accessible technique that can be accessed with the above browsing methods.
Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/title
Great question! The title attribute is visually hidden unless you know to hover, and doesn’t work on mobile. Title definitely has uses but I wanted this to be a prominent feature accessible across all devices.
Adding to everything you said, title is finicky. There’s a character limit, it takes too long to appear, can auto dismiss after some time, and this is all OS-dependent with no consistency.
and doesn’t work on mobile.
It depends upon the phone. iOS will show the title
attribute if you press it, but only if the image itself isn’t a link.
I use this on xkcd, which hides an additional joke there, not sure if this use is semantic though. And using the same text for title and alt is an anti-pattern.
The example doesn’t seem to work correctly on Firefox for Android (and possibly other Firefoxes?). I see the checkbox and immediately beneath the alt text, regardless of whether I toggle the checkbox or not.
I assume this is due to the :has
selector, but I’m sure I’ve tested that on Firefox for Android before and it’s worked perfectly fine, so I’m a bit surprised, but unfortunately I can’t debug further very easily.
I’m on Firefox Android and the example in the article works for me (version 142).
That’s very odd. I’m also on version 142, and it is still showing the same behaviour of showing both the checkbox and the alt text no matter what the state of the checkbox.
I’ve tried disabling AdBlock, as well as switching to dark mode, and I’m still getting the same issue.