Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap different img size at lg, md sm and xs

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.

like image 398
Mateo Velenik Avatar asked Jun 06 '14 17:06

Mateo Velenik


People also ask

Which class of Bootstrap image is resized based on screen size?

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.

How can you set the device width for a mobile in Bootstrap?

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.


1 Answers

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.

like image 81
ofarukcaki Avatar answered Sep 22 '22 08:09

ofarukcaki