How to hide image broken Icon using only CSS/HTML?

前端 未结 17 1560
不思量自难忘°
不思量自难忘° 2020-11-27 09:14

How can I hide the broken image icon? Example: \"Example\"

I have an image with error src:

相关标签:
17条回答
  • 2020-11-27 09:39

    Found a great solution at https://bitsofco.de/styling-broken-images/

    img {  
      position: relative;
    }
    
    /* style this to fit your needs */
    /* and remove [alt] to apply to all images*/
    img[alt]:after {  
      display: block;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: #fff;
      font-family: 'Helvetica';
      font-weight: 300;
      line-height: 2;  
      text-align: center;
      content: attr(alt);
    }
    <img src="error">
    <br>
    <img src="broken" alt="A broken image">
    <br>
    <img src="https://images-na.ssl-images-amazon.com/images/I/218eLEn0fuL.png" alt="A bird" style="width: 120px">

    0 讨论(0)
  • 2020-11-27 09:39

    For future googlers, in 2016 there is a browser safe pure CSS way of hiding empty images using the attribute selector:

    img[src="Error.src"] {
        display: none;
    }
    

    Edit: I'm back - for future googlers, in 2019 there is a way to style the actual alt text and alt text image in the Shadow Dom, but it only works in developer tools. So you can't use it. Sorry. It would be so nice.

    #alttext-container {
        opacity: 0;
    }
    #alttext-image {
        opacity: 0;
    }
    #alttext {
        opacity: 0;
    }
    
    0 讨论(0)
  • 2020-11-27 09:44

    Despite what people are saying here, you don't need JavaScript at all, you don't even need CSS!!

    It's actually very doable and simple with HTML only. You can even show a default image if an image doesn't load. Here's how...

    This also works on all browsers, even as far back as IE8 (out of 250,000+ visitors to sites I hosted in September 2015, ZERO people used something worse than IE8, meaning this solution works for literally everything).

    Step 1: Reference the image as an object instead of an img. When objects fail they don't show broken icons; they just do nothing. Starting with IE8, you can use Object and Img tags interchangeably. You can resize and do all the glorious stuff you can with regular images too. Don't be afraid of the object tag; it's just a tag, nothing big and bulky gets loaded and it doesn't slow down anything. You'll just be using the img tag by another name. A speed test shows they are used identically.

    Step 2: (Optional, but awesome) Stick a default image inside that object. If the image you want actually loads in the object, the default image won't show. So for example you could show a list of user avatars, and if someone doesn't have an image on the server yet, it could show the placeholder image... no javascript or CSS required at all, but you get the features of what takes most people JavaScript.

    Here is the code...

    <object data="avatar.jpg" type="image/jpg">
        <img src="default.jpg" />
    </object>
    

    ... Yes, it's that simple.

    If you want to implement default images with CSS, you can make it even simpler in your HTML like this...

    <object class="avatar" data="user21.jpg" type="image/jpg"></object>
    

    ...and just add the CSS from this answer -> https://stackoverflow.com/a/32928240/3196360

    0 讨论(0)
  • 2020-11-27 09:47

    You can follow this path as a css solution

    img {
            width:200px;
            height:200px;
            position:relative
       }
    img:after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: inherit;
            height: inherit;
            background: #ebebeb url('http://via.placeholder.com/300?text=PlaceHolder') no-repeat center;
            color: transparent;
        }
    <img src="gdfgd.jpg">

    0 讨论(0)
  • 2020-11-27 09:49

    Using CSS only is tough, but you could use CSS's background-image instead of <img> tags...

    Something like this:

    HTML

    <div id="image"></div>
    

    CSS

    #image {
        background-image: url(Error.src);
        width: //width of image;
        height: //height of image;
    
    }
    

    Here is a working fiddle.

    Note: I added the border in the CSS on the fiddle just to demonstrate where the image would be.

    0 讨论(0)
  • 2020-11-27 09:50

    There is no way for CSS/HTML to know if the image is broken link, so you are going to have to use JavaScript no matter what

    But here is a minimal method for either hiding the image, or replacing the source with a backup.

    <img src="Error.src" onerror="this.style.display='none'"/>
    

    or

    <img src="Error.src" onerror="this.src='fallback-img.jpg'"/>
    

    Update

    You can apply this logic to multiple images at once by doing something like this:

    document.addEventListener("DOMContentLoaded", function(event) {
       document.querySelectorAll('img').forEach(function(img){
      	img.onerror = function(){this.style.display='none';};
       })
    });
    <img src="error.src">
    <img src="error.src">
    <img src="error.src">
    <img src="error.src">

    Update 2

    For a CSS option see michalzuber's answer below. You can't hide the entire image, but you change how the broken icon looks.

    0 讨论(0)
提交回复
热议问题