The code below uses the picture
HTML5 element to display a different versions of an image, depending if the user uses a mobile device or a desktop.
Question: How can I make only mobile_image.png 10% the width of the viewport (window)? Why can't I add a css class or style tag to source media
?
<div class="my_container">
<a href="#back_to_top">
<picture>
<source media="(max-width:767px)" srcset="mobile_image.png">
<source media="(min-width:768px)" srcset="desktop_image.png">
<img src="fallback_image.png" alt="Back to Top">
</picture>
</a>
</div>
Using the code below - nothing!
@media only screen and (max-width : 767px) {
.my_container {
width:10%;
height:auto;
}
}
For the CSS, you will need the image class names. There are default class names, which you can use to style all images at once, or you can add your own custom class names. With your own custom class names, you can style an individual image or multiple images of your choice.
Classes (i.e. classnames) are used for styling the img element. Multiple classnames are separated by a space. JavaScript uses classes to access elements by classname. Tip: class is a global attribute that can be applied to any HTML element.
Yes you can - first capture the event using onmouseover , then set the class name using Element. className . If you like to add or remove classes - use the more convenient Element.
Summing up: if an image has meaning, in terms of your content, you should use an HTML image. If an image is purely decoration, you should use CSS background images.
You can also add classes to the "img src" line of the code:
<picture>
<source media="(min-width:768px)" srcset="tablet_image.png">
<source media="(min-width:992px)" srcset="desktop_image.png">
<img src="fallback-mobile_image.png" alt="whatever" class="img-responsive center-block etc">
</picture>
The classes are then applied to whichever image is being rendered by the screen size. Definitely helpful for when you're using a framework like Bootstrap or Foundation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With