Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»You can style alt text like any other text

    You can style alt text like any other text

    May 22, 2025

    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.

    CodePen Embed Fallback

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


    You can style alt text like any other text originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleGNOME Dropping X11 Support May Complicate Next Ubuntu LTS
    Next Article Ubuntu 25.10 Will Ship with the Linux 6.17 Kernel

    Related Posts

    News & Updates

    The best CRM software with email marketing in 2025: Expert tested and reviewed

    July 22, 2025
    News & Updates

    This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Microsoft Edge 135 is here with big new features, and even bigger changes

    Operating Systems

    Oracle Critical Security Update – 309 Vulnerabilities with 145 Remotely Exploitable Patched

    Security

    CVE-2025-53835 – XWiki Rendering Cross-Site Scripting Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Mitel OpenScape Flaw (CVE-2025-23092): High-Severity Path Traversal Allows Admin RCE

    Security

    Highlights

    Halo Studios teases major reveal in October, promises big update at World Championship

    July 1, 2025

    Halo Studios has finally broken its silence. In the latest Halo Waypoint blog, the Xbox-owned…

    CVE-2025-52805 – Leyka Path Traversal PHP Local File Inclusion Vulnerability

    July 4, 2025

    CVE-2025-44024 – Pichome XSS

    May 14, 2025

    ServiceNow Flaw CVE-2025-3648 Could Lead to Data Exposure via Misconfigured ACLs

    July 10, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.