简体   繁体   中英

Loading time with mobile banner in media query

I have normal banner when loading my page on a computer. However, I'd like to have smaller version (so it loads faster) on mobile (media query). My question: How does it load? Is there any benefit when changing the banner in mobile or page loads all the files either way so it's just a waste of resources? If this doesn't improve loading time, is there any other way to do it?

.banner1 .banner-img{
    background-image:url(../Images/banner1.jpg);
    background-repeat:no-repeat;
    background-size:cover;
    background-position:center;
}
@media screen and (max-width:768px){
    .banner1 .banner-img{
        background-image:url(../Images/banner2.jpg);
    }
}

From here https://css-tricks.com/the-complete-guide-to-lazy-loading-images/ :

If the CSS rule specifying the background image does not apply to an element in the document, then the browser does not load the background image. If the CSS rule is applicable to an element in the current document, then the browser loads the image.

So your css should work and should improve loading times. You can check it using your browser's devtools. The 'network' tab shows what requests the browser has made, so you can use this to check if it is loading one image or both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM