How can I change the a background image, depending on the size of the screen?

此生再无相见时 提交于 2019-12-08 10:14:09

问题


I am using Zurb foundation to handle my styling, I know there is a data interchange class for changing images based on screen size but not sure if you can use it to change the background image of a div?

background-image:image-url('img_home_first_bar.jpg'); 

回答1:


You can do this by using media queries as said in the comments by Rahul Desai.

Example:

HTML:

<div class="backgroundchange"></div>

CSS:

.backgroundchange {
    width: 100px;
    height: 100px;
    background: grey;
}
@media screen and (min-width: 1080px) {
    .backgroundchange {
        background-image: url("backing1.png");
        background: blue;
    }
}
@media screen and (min-width: 487px) and (max-width: 1079px) {
    .backgroundchange {
        background-image: url("backing2.png");
        background: green;
    }
}
@media screen and (max-width: 487px) {
    .backgroundchange {
        background-image: url("backing3.png");
        background: red;
    }
}

So using these media queries you can set the CSS to change depending on what the window size is.

Note: Make the result window bigger and smaller to see the effect.

DEMO HERE




回答2:


Try this, as your problem is similar to the problem discussed here.

Background Image change with screen size.



来源:https://stackoverflow.com/questions/21425423/how-can-i-change-the-a-background-image-depending-on-the-size-of-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!