Does “display:none” prevent an image from loading?

前端 未结 17 1906
再見小時候
再見小時候 2020-11-22 05:13

Every responsive website development tutorial recommends using the display:none CSS property to hide content from loading on mobile browsers so the website load

17条回答
  •  -上瘾入骨i
    2020-11-22 05:39

    The trick to using display:none with images is to assign them an id. This was there is not a lot of code needed to make it work. Here is an example using media queries and 3 stylesheets. One for phone, one for tablet, and one for desktop. I have 3 images, image of a phone, a tablet, and a desktop. On a phone screen only an image of the phone will display, a tablet will display only the tablet image, a desktop displays on the desktop computer image. Here is a code example to make it work:

    Source code:

    The phone CSS which doesn't need a media query. Its the img#phone that makes it work:

    img#phone {
        display: block;
        margin: 6em auto 0 auto;
        width: 70%;
        }
    
    img#tablet {display: none;}
    img#desktop {display: none;}
    

    The tablet css:

    @media only screen and (min-width: 641px) {
    img#phone {display: none;}
    
    img#tablet {
        display: block;
        margin: 6em auto 0 auto;
        width: 70%;
        }
    }
    

    And the desktop css:

    @media only screen and (min-width: 1141px) {
    img#tablet {display: none;}
    
    img#desktop {
        display: block;
        margin: 6em auto 0 auto;
        width: 80%;
        }
    }
    

    Good luck and let me know how it works for you.

提交回复
热议问题