Would it be possible to use image title instead of alt text for the caption?
It appears you can set the title of an image like so
. I'm wondering if there could be an option to allow using the title instead of alt text for the image caption. Could be a nice convenience over putting in HTML for a caption.
Or is there already a good way to set a separate caption that I'm missing?
I’d +1 this feature. It’d be great to be able to use alt text for its intended accessibility virtue and still easily set a separate caption.
Answered 2 years ago · Edit answerI like this too – I propose modifying the image caption feature such that the title
is chosen as first priority, then the alt
text as fallback. This feels more correct than ignoring the title
and just using the alt text
I've deployed a change to make this happen – the title
text is chosen as first priority over the alt
text when sourcing the image caption
On my site, I want captions for some images, but not for others. Is there a way to configure Blot to do the following:
- If there is both an alt and a title, use the alt for accessibility and the title as a caption.
- If there is an alt and no title, use the alt for accessibility and do not create a caption.
It's not possible to configure Blot itself this way but if you add the following JavaScript to your template's script.js file, you should get the desired result:
document.querySelectorAll('.caption').forEach(caption => caption.previousElementSibling?.tagName === 'IMG' && !caption.previousElementSibling.title ? caption.style.display = 'none' : null);
Explanation
document.querySelectorAll('.caption')
:
Selects all elements with the classcaption
..forEach(caption => ...)
:
Iterates over eachcaption
element found in the NodeList.caption.previousElementSibling
:
Checks the sibling element immediately before thecaption
(expected to be theimg
tag).?.tagName === 'IMG'
:
Ensures the previous sibling exists and is animg
tag. The optional chaining (?.
) prevents errors if thepreviousElementSibling
isnull
.!caption.previousElementSibling.title
:
Verifies theimg
tag has notitle
attribute. If thetitle
is missing or empty, this condition evaluates totrue
.caption.style.display = 'none'
:
Hides thecaption
by setting its CSSdisplay
property tonone
.
This ensures only captions with corresponding images that lack a title
are hidden.
Newsletter
Get updates on Blot’s latest features and changes. Delivered every three months.