I need to make a image change it's size if the viewer has a smartphone or a PC. This achieves what I want but it's a bad way to do it because of loading the img 4 times and it's redundant:
<div class="col-md-4 text-center">
<img class="hidden-md hidden-sm hidden-xs" src="~/Content/Images/masinaC.png" width="250" height="250" />
<img class="hidden-lg hidden-sm hidden-xs" src="~/Content/Images/masinaC.png" width="200" height="200" />
<img class="hidden-lg hidden-md hidden-xs" src="~/Content/Images/masinaC.png" width="150" height="150" />
<img class="hidden-lg hidden-md hidden-sm" src="~/Content/Images/masinaC.png" width="100" height="100" />
</div>
It displays only one image at once, and only the size differs. How to achieve it without this ugly markup? Possibly with CSS, I put the image sizes in HTML just for simplicity.
Responsive Images So do screens. Responsive images automatically adjust to fit the size of the screen. Create responsive images by adding an . img-responsive class to the <img> tag.
With the help of media queries, Bootstrap gives different widths to the . container depending on the size of the device: Extra small devices (<768px): width: auto (or no width) Small Devices (≥768px): width: 750px.
I'm also a newbie but rather than all complex codes above, this simple code fixed my problem.
#logo img {
max-height: 100%;
max-height: 50vh; //50% of user's viewport height
}
If you want to make changes for width, then try this:
#logo img {
max-width: 100%;
max-width: 50vw; //50% of user's viewport width
}
I hope this also help for others.
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