How can I make all images of different height and width the same via CSS?

HtmlImageCssResize

Html Problem Overview


I am trying to create an image wall consisting of product photos. Unfortunately, all of them are of different height and width. How can I use css to make all images look the same size? preferably 100 x 100.

I was thinking of doing a div that has height and width of 100px and then some how filling it up. NOt sure how to do that.

How can I accomplish this?

Html Solutions


Solution 1 - Html

Updated answer (No IE11 support)

img {
    float: left;
    width:  100px;
    height: 100px;
    object-fit: cover;
}

<img src="http://i.imgur.com/tI5jq2c.jpg">
<img src="http://i.imgur.com/37w80TG.jpg">
<img src="http://i.imgur.com/B1MCOtx.jpg">

Original answer

.img {
    float: left;
    width:  100px;
    height: 100px;
    background-size: cover;
}

<div class="img" style="background-image:url('http://i.imgur.com/tI5jq2c.jpg');"></div>
<div class="img" style="background-image:url('http://i.imgur.com/37w80TG.jpg');"></div>
<div class="img" style="background-image:url('http://i.imgur.com/B1MCOtx.jpg');"></div>

Solution 2 - Html

Simplest way - This will keep the image size as it is and fill the other area with space, this way all the images will take same specified space regardless of the image size without stretching

.img{
   width:100px;
   height:100px;

/*Scale down will take the necessary specified space that is 100px x 100px without stretching the image*/
    object-fit:scale-down;

}

Solution 3 - Html

can i just throw in that if you distort your images too much, ie take them out of a ratio, they may not look right, - a tiny amount is fine, but one way to do this is put the images inside a 'container' and set the container to the 100 x 100, then set your image to overflow none, and set the smallest width to the maximum width of the container, this will crop a bit of your image though,

for example

<h4>Products</h4>
<ul class="products">
    <li class="crop">
        <img src="ipod.jpg" alt="iPod" />
    </li>
</ul>



.crop {
 height: 300px;
 width: 400px;
 overflow: hidden;
}
.crop img {
 height: auto;
 width: 400px;
}

This way the image will stay the size of its container, but will resize without breaking constraints

Solution 4 - Html

You can use the object-fit property to size the img elements:

  • cover stretches or shrinks the image proportionally to fill the container. The image is cropped horizontally -or- vertically if necessary.
  • contain stretches or shrinks the image proportionally to fit inside the container.
  • scale-down shrinks the image proportionally to fit inside the container.

.example {
  margin: 1em 0;
  text-align: center;
}

.example img {
  width: 30vw;
  height: 30vw;
}

.example-cover img {
  object-fit: cover;
}

.example-contain img {
  object-fit: contain;
}

<div class="example example-cover">
  <img src="https://i.stack.imgur.com/B0EAo.png">
  <img src="https://i.stack.imgur.com/iYkNH.png">
  <img src="https://i.stack.imgur.com/gne9N.png">
</div>

<div class="example example-contain">
  <img src="https://i.stack.imgur.com/B0EAo.png">
  <img src="https://i.stack.imgur.com/iYkNH.png">
  <img src="https://i.stack.imgur.com/gne9N.png">
</div>

In the above example: red is landscape, green is portrait and blue is square image. The checkered pattern consists of 16x16px squares.

Solution 5 - Html

For those using Bootstrap and not wanting to lose the responsivness just do not set the width of the container. The following code is based on gillytech post.

index.hmtl

<div id="image_preview" class="row">  
    <div class='crop col-xs-12 col-sm-6 col-md-6 '>
         <img class="col-xs-12 col-sm-6 col-md-6" 
          id="preview0" src='img/preview_default.jpg'/>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-6">
         more stuff
    </div>

</div> <!-- end image preview -->

style.css

/*images with the same width*/
.crop {
    height: 300px;
    /*width: 400px;*/
    overflow: hidden;
}
.crop img {
    height: auto;
    width: 100%;
}

OR style.css

/*images with the same height*/
.crop {
    height: 300px;
    /*width: 400px;*/
    overflow: hidden;
}
.crop img {
    height: 100%;
    width: auto;
}

Solution 6 - Html

You can do it this way:

 .container{
position: relative;
width: 100px;
height: 100px;
overflow: hidden;
z-index: 1;
}

img{
left: 50%;
position: absolute;
top: 50%;
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
max-width: 100%;
}

Solution 7 - Html

Set height and width parameters in CSS file

.ImageStyle{

    max-height: 17vw;
    min-height: 17vw;
    max-width:17vw;
    min-width: 17vw;
    
}

Solution 8 - Html

I was looking for a solution for this same problem, to create a list of logos.

I came up with this solution that uses a bit of flexbox, which works for us since we're not worried about old browsers.

This example assumes a 100x100px box but I'm pretty sure the size could be flexible/responsive.

.img__container {
	display: flex;
    padding: 15px 12px;
	box-sizing: border-box;
	width: 100px; height: 100px;

	img {
		margin: auto;
		max-width: 100%;
		max-height: 100%;
	}
}

ps.: you may need to add some prefixes or use autoprefixer.

Solution 9 - Html

Based on Andi Wilkinson's answer (the second one), I improved a little, make sure the center of the image is shown (like the accepted answer did):

HTML:

<div class="crop">
   <img src="img.png">
</div>

CSS:

.crop{
  height: 150px;
  width: 200px;
  overflow: hidden;
}
.crop img{
  width: 100%;
  height: auto;
  position: relative;
  top: 50%;
  -webkit-transform: translateY(-50%); /* Ch <36, Saf 5.1+, iOS < 9.2, An =<4.4.4 */
  -ms-transform: translateY(-50%); /* IE 9 */
  transform: translateY(-50%); /* IE 10, Fx 16+, Op 12.1+ */
}

Solution 10 - Html

.article-img img{ 
    height: 100%;
    width: 100%;
    position: relative;
    vertical-align: middle;
    border-style: none;
 }

You will make images size same as div and you can use bootstrap grid to manipulate div size accordingly

Solution 11 - Html

Image size is not depend on div height and width,

use img element in css

Here is css code that help you

div img{
         width: 100px;
         height:100px;
 }

if you want to set size by div

use this

div {
    width:100px;
    height:100px;
    overflow:hidden;
}

by this code your image show in original size but show first 100x100px overflow will hide

Solution 12 - Html

Without code this is difficult to help you but here's some practical advice for you:

I suspect that your "image wall" has some sort of container with an id or class to give it styles.

eg:

<body>

<div id="maincontainer">
  <div id="header"></div>

  <div id="content">
    <div id="imagewall">
      <img src"img.jpg">
<!-- code continues -->

Styling a size on all images for your image wall, while not affecting other images, like you logo, etc. is easy if your code is set up similar to the above.

#imagewall img {
  width: 100px;
  height: 100px; }

But if your images are not perfectly square they will be skewed using this method.

Solution 13 - Html

Go to your CSS file and resize all your images as follows

img {
  width:  100px;
  height: 100px;
}

Solution 14 - Html

Change your image tag with this CSS style.

img {
    float: left;
    width:  100px;
    height: 100px;
    object-fit: cover;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarielView Question on Stackoverflow
Solution 1 - HtmlSimon ArnoldView Answer on Stackoverflow
Solution 2 - HtmlManoj SelvinView Answer on Stackoverflow
Solution 3 - HtmlAndi WilkinsonView Answer on Stackoverflow
Solution 4 - HtmlSalman AView Answer on Stackoverflow
Solution 5 - HtmlSven YaView Answer on Stackoverflow
Solution 6 - HtmlArun ksView Answer on Stackoverflow
Solution 7 - HtmlEmaView Answer on Stackoverflow
Solution 8 - HtmlrafaelbitenView Answer on Stackoverflow
Solution 9 - HtmlAlessiaView Answer on Stackoverflow
Solution 10 - HtmlManav KothariView Answer on Stackoverflow
Solution 11 - Htmlasad razaView Answer on Stackoverflow
Solution 12 - Html1934286View Answer on Stackoverflow
Solution 13 - HtmlbakslashView Answer on Stackoverflow
Solution 14 - HtmlMd AtiqurView Answer on Stackoverflow