Within a specific folder structure, I have a collection of images designated for both desktop and mobile displays:
img:
img-1.png
img-2.png
img-3.png
img-4.png
img-5.png
img-6.png
img/mobile:
img-1.png
img-2.png
img-3.png
img-4.png
img-5.png
img-6.png
To switch the desktop image img-1.png
, I currently use this code:
<span id="switcher">
<img id="houdini" src="img/img-1.jpg" alt="">
</span>
<span id="switcher2">
<img id="houdini2" src="img/img-2.jpg" alt="">
</span>
<span id="switcher3">
<img id="houdini3" src="img/img-3.jpg" alt="">
</span>
<span id="switcher4">
<img id="houdini4" src="img/img-4.jpg" alt="">
</span>
<span id="switcher5">
<img id="houdini5" src="img/img-5.jpg" alt="">
</span>
Regarding CSS:
@media only screen and (max-device-width: 489px) {
span[id=switcher] {
display:block;
background-image: url(/mobile/img-1.jpg);
background-repeat: no-repeat;
background-position: center;
}
img[id=houdini] {display: none;}
}
Is there a way to streamline writing the above CSS for all images from img-1
to img-6
? Can an ID be passed or accessed?
Can we eliminate the use of !important
in the code?
(Need compatibility with IE8)