Change background image on the fly

Hi,

I have a div in which i have a background image via a css class, the class code is:

[code].home-top-bg
{
margin:0 0 0 0;
padding:0px;
width:100%;
background-image: url(‘…/pictures/cover.jpg’);
background-size:cover;

}[/code]

It works fine as I want, I am using bootstrap in that. The issue I have is that when the site is viewed in mobile mode etc. then that bg does’nt looks good, is it possible to change the bg to something else on the fly ???

Thanks.

use a media query which assigns a different background image.

As Dave said just do something like this:

.home-top-bg
{
	margin:0 0 0 0;
	padding:0px;
	width:100%;
	background: url('../pictures/cover-small.jpg') no-repeat 0 0; /* small screens get this */
	background-size:cover;
}
@media screen and (min-width:768px){ /* large screens get this */
	 .home-top-bg {
			background: url('../pictures/cover.jpg')  no-repeat 0 0;
			background-size:cover;
	}
}
1 Like

Excellent, works like a charm! Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.