Log in Sign up

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![A screenshot of something](./_screenshot.png "this is a title"). 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?


a month ago, 8 replies   request   added   Edit question

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 answer

I 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

Answered 2 years ago · Edit answer

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

Answered 2 years ago · Edit answer

Very cool! Thanks for doing that!

Answered 2 years ago · Edit answer

Yes, thank you! Seems to be working great

Answered 2 years ago · Edit answer

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.
Answered a month ago · Edit answer

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

  1. document.querySelectorAll('.caption'):
    Selects all elements with the class caption.

  2. .forEach(caption => ...):
    Iterates over each caption element found in the NodeList.

  3. caption.previousElementSibling:
    Checks the sibling element immediately before the caption (expected to be the img tag).

  4. ?.tagName === 'IMG':
    Ensures the previous sibling exists and is an img tag. The optional chaining (?.) prevents errors if the previousElementSibling is null.

  5. !caption.previousElementSibling.title:
    Verifies the img tag has no title attribute. If the title is missing or empty, this condition evaluates to true.

  6. caption.style.display = 'none':
    Hides the caption by setting its CSS display property to none.

This ensures only captions with corresponding images that lack a title are hidden.

Answered a month ago · Edit answer

That's perfect, thank you! :)

Answered a month ago · Edit answer

Newsletter
Get updates on Blot’s latest features and changes. Delivered every three months.