Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive Image with fixed height

Tags:

html

css

i would like to have responsive image with a fixed height.

Here is my html :

<div class="col-md-6">
   <div class="img">
       <img src="http://image.noelshack.com/fichiers/2016/16/1461065658-b-v-s.jpg">
   </div>
</div>
<div class="col-md-6">
   <div class="img">
       <img src="http://image.noelshack.com/fichiers/2016/16/1461065665-kfp3.jpg">
   </div>
</div>

Here is my css :

.col-md-6 {
   width: 50%;
   float: left;
 }

.img {
   overflow: hidden;
   position: relative;
   height: 290px;
}

.img img {
   max-width: 100%;
   min-width: 100%;
   min-height: 100%;
}

Here is my jsfiddle : https://jsfiddle.net/23o81xrb/

As u can see, .img has an height of 290px and that should stay like that, but when we reduce the window, images aren't adapted. I would like to have like a "zoom" on my image so they can keep 290px height and be adapted in width.

Sorry for my "bad" english, hope u understood.

Any help will be appreciated, thanks.

like image 248
Thomas Gueguen Avatar asked Apr 19 '16 11:04

Thomas Gueguen


People also ask

How do you make a position absolute image responsive?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

How do I make an image fit my screen size in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How auto adjust the Div height according to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.


1 Answers

If you use img tag, use can use object-fit property in CSS3 as below :

.col-md-6 {
  width: 50%;
  float: left;
}

.img {
  overflow: hidden;
  position: relative;
  height: 290px;
}

.img img {
  max-width: 100%;
  min-height: 100%;
  object-fit: cover;
}

That should solve your problem.

like image 141
Biraj Bora Avatar answered Sep 20 '22 12:09

Biraj Bora