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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 23, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 23, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 23, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 23, 2025

      SteamOS is officially not just for Steam Deck anymore — now ready for Lenovo Legion Go S and sort of ready for the ROG Ally

      May 23, 2025

      Microsoft’s latest AI model can accurately forecast the weather: “It doesn’t know the laws of physics, so it could make up something completely crazy”

      May 23, 2025

      OpenAI scientists wanted “a doomsday bunker” before AGI surpasses human intelligence and threatens humanity

      May 23, 2025

      My favorite gaming service is 40% off right now (and no, it’s not Xbox Game Pass)

      May 23, 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

      A timeline of JavaScript’s history

      May 23, 2025
      Recent

      A timeline of JavaScript’s history

      May 23, 2025

      Loading JSON Data into Snowflake From Local Directory

      May 23, 2025

      Streamline Conditional Logic with Laravel’s Fluent Conditionable Trait

      May 23, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      SteamOS is officially not just for Steam Deck anymore — now ready for Lenovo Legion Go S and sort of ready for the ROG Ally

      May 23, 2025
      Recent

      SteamOS is officially not just for Steam Deck anymore — now ready for Lenovo Legion Go S and sort of ready for the ROG Ally

      May 23, 2025

      Microsoft’s latest AI model can accurately forecast the weather: “It doesn’t know the laws of physics, so it could make up something completely crazy”

      May 23, 2025

      OpenAI scientists wanted “a doomsday bunker” before AGI surpasses human intelligence and threatens humanity

      May 23, 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 

    Hostinger
    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

    SteamOS is officially not just for Steam Deck anymore — now ready for Lenovo Legion Go S and sort of ready for the ROG Ally

    May 23, 2025
    News & Updates

    Microsoft’s latest AI model can accurately forecast the weather: “It doesn’t know the laws of physics, so it could make up something completely crazy”

    May 23, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Samsung appliances are about to get smarter and wiser – thanks to AI

    News & Updates

    CVE-2025-26389 – OZW672/OZW772 Unauthenticated Remote Code Execution (RCE) in Web Service

    Common Vulnerabilities and Exposures (CVEs)

    Windows 3.1 is now available for the Game Boy Color… kind of

    News & Updates

    Outranking.io Review: Can It Really Improve SEO?

    Development
    Hostinger

    Highlights

    Why the road from passwords to passkeys is long, bumpy, and worth it – probably

    April 25, 2025

    The passkey standard has reached a precarious moment. Let’s not blow it, OK? Source: Latest…

    Crypto Investors Alarmed as Coinstats Breach Impacts 1,590 Wallets

    June 24, 2024

    Using Relative Date Helpers in Laravel’s Query Builder

    February 13, 2025

    Styling a meter element with CSS and SVG

    January 28, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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