142

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?

ariel
  • 2,962
  • 7
  • 27
  • 31

14 Answers14

266

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>
Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
  • 3
    awesome answer! works for libraries like Angular and Vue too when want to user repeater! – Matt Catellier Sep 25 '16 at 21:26
  • 2
    use of div instead of img could have impact on seo (not sure) – Muhammad Ali Mansoor May 21 '20 at 22:12
  • @MuhammadAliMansoor it could if you don't compensate with microtadatas. I updated my answer to achieve the same result with img tags and object-fit, which now has good support. – Simon Arnold May 21 '20 at 22:52
  • 2
    Why the heck isn't this all over the internet? It even works with % widths! – Liz Jun 23 '20 at 03:38
  • 1
    I've been struggling with this equal height issue for months. But now it is cleared. Thanks. I will apply it to all my others apps suffering the same fate. – Djesu Nov 08 '21 at 20:59
  • Perfect! I needed img tags so I could drag them into my editor! – Nick Oct 10 '22 at 03:16
  • While this works from a visual point of view, it's an SEO and performance nightmare. Best solution is to crop images to equal size – KMarto Nov 01 '22 at 05:00
46

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;

}
Manoj Selvin
  • 2,247
  • 24
  • 20
  • 4
    Wow, it was really that simple. Maybe the other answers are "correct", but this one was the easiest to implement. – Randall Arms Jul 25 '18 at 01:58
  • 5
    The only caveat to this is that there is no IE11 support. – Romuloux Aug 21 '18 at 21:41
  • The upside of this is that your images are not cropped nor skewed, but the downside is that they still don't all necessarily appear the same size. Rather than object-fit:scale-down, you can also try object-fit:cover. – Vincent Jul 09 '20 at 17:57
38

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

gillytech
  • 3,595
  • 2
  • 27
  • 44
Andi Wilkinson
  • 662
  • 1
  • 6
  • 14
27

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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
3

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;
}
Sven Ya
  • 552
  • 1
  • 5
  • 12
3

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%;
}
Arun ks
  • 31
  • 1
3

Set height and width parameters in CSS file

.ImageStyle{

    max-height: 17vw;
    min-height: 17vw;
    max-width:17vw;
    min-width: 17vw;
    
}
Ema
  • 209
  • 2
  • 4
2

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.

rafaelbiten
  • 6,074
  • 2
  • 31
  • 36
1

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.

user1934286
  • 1,732
  • 3
  • 23
  • 42
1

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+ */
}
Community
  • 1
  • 1
Alessia
  • 899
  • 10
  • 16
1
.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

Manav Kothari
  • 1,149
  • 1
  • 6
  • 7
1

Change your image tag with this CSS style.

img {
    float: left;
    width:  100px;
    height: 100px;
    object-fit: cover;
}
Md Atiqur
  • 456
  • 6
  • 10
  • This is exactly the same as in the [accepted answer](https://stackoverflow.com/a/19414955/2227743) from 2013 with 200+ upvotes (the first code block). – Eric Aya Sep 12 '21 at 10:30
0

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

asad raza
  • 432
  • 1
  • 7
  • 13
0

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

img {
  width:  100px;
  height: 100px;
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93