You can style alt text like any other text

0
318

You can style alt text like any other text | CSS-Tricks

Clever, clever that Andy Bell. He shares a technique for displaying image alt text when the image fails to load. Well, more precisely, it’s a technique to apply styles to the alt when the image doesn’t load, offering a nice UI fallback for what would otherwise be a busted-looking error.

The recipe? First, make sure you’re using alt in the HTML. Then, a little JavaScript snippet that detects when an image fails to load:

const images = document.querySelectorAll("img");

if (images) {
  images.forEach((image) => {
    image.onerror = () => {
      image.setAttribute("data-img-loading-error", "");
    };
  });
}

That slaps an attribute on the image — data-img-loading-error — that is selected in CSS:

img[data-img-loading-error] {
  --img-border-style: 0.25em solid
    color-mix(in srgb, currentColor, transparent 75%);
  --img-border-space: 1em;

  border-inline-start: var(--img-border-style);
  border-block-end: var(--img-border-style);
  padding-inline-start: var(--img-border-space);
  padding-block: var(--img-border-space);
  max-width: 42ch;
  margin-inline: auto;
}

And what you get is a lovely little presentation of the alt that looks a bit like a blockquote and is is only displayed when needed.

Andy does note, however, that Safari does not render alt text if it goes beyond a single line, which 🤷‍♂️.

Спонсоры
Спонсоры
Спонсоры
Спонсоры
Спонсоры
Поиск
Спонсоры
Virtuala FansOnly
CDN FREE
Cloud Convert
Категории
Больше
Art
8 Spiele haben Veröffentlichungstermine wegen Silksong verschoben
## Einleitung Es gibt nichts Spannenderes, als auf ein neues Spiel zu warten, insbesondere wenn...
От Melina Lina 2025-08-28 21:05:31 1 339
Другое
High-K and CVD ALD Metal Precursors Market Growth Opportunities
Market OverviewAccording To The Research Report Published By Polaris Market Research, The Global...
От MAYUR YADAV 2025-09-25 10:39:28 0 179
Другое
U Topuskom održane Sportske igre hrvatskih branitelja - Sisak.info portal
U Topuskom održane Sportske igre hrvatskih branitelja - Sisak.info portalU Topuskom su održane...
От Drago Merkaš 2025-06-16 14:22:27 0 254
Art
Forget Nintendo, Mein Arcade Atari Gamestation Go Mit 200+ Retro-Videospielen, Einschließlich Pac-Man, Ist Jetzt Zur Vorbestellung Auf Amazon Verfügbar
Retro, Videospiele, Atari, Pac-Man, My Arcade, Gaming, Vorbestellung, Nostalgie, Handheld ##...
От Isabel Amelie 2025-09-15 08:05:32 1 706
Crafts
Yankees Select Estevan Florial Yankees Select Estevan Florial
The Jake Cronenworth Jersey Yankees announced Monday that theyve selected the contract of...
От Verna Skiles 2025-10-27 03:49:08 0 166
Спонсоры
Virtuala FansOnly https://virtuala.site