ariel
ariel

Reputation: 3072

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

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?

Upvotes: 151

Views: 603501

Answers (14)

Md Atiqur
Md Atiqur

Reputation: 683

Change your image tag with this CSS style.

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

Upvotes: 0

Simon Arnold
Simon Arnold

Reputation: 16157

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>

Upvotes: 275

Manav Kothari
Manav Kothari

Reputation: 1149

.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

Upvotes: 1

Salman Arshad
Salman Arshad

Reputation: 272036

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.sstatic.net/B0EAo.png">
  <img src="https://i.sstatic.net/iYkNH.png">
  <img src="https://i.sstatic.net/gne9N.png">
</div>

<div class="example example-contain">
  <img src="https://i.sstatic.net/B0EAo.png">
  <img src="https://i.sstatic.net/iYkNH.png">
  <img src="https://i.sstatic.net/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.

Upvotes: 28

Manoj Selvin
Manoj Selvin

Reputation: 2383

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;

}

Upvotes: 47

Ema
Ema

Reputation: 219

Set height and width parameters in CSS file

.ImageStyle{

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

Upvotes: 3

Alessia
Alessia

Reputation: 959

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+ */
}

Upvotes: 1

Arun ks
Arun ks

Reputation: 31

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%;
}

Upvotes: 3

rafaelbiten
rafaelbiten

Reputation: 6240

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.

Upvotes: 2

bakslash
bakslash

Reputation: 11

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

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

Upvotes: 0

Sven Ya
Sven Ya

Reputation: 562

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;
}

Upvotes: 3

Andi Wilkinson
Andi Wilkinson

Reputation: 662

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

Upvotes: 38

user1934286
user1934286

Reputation: 1762

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.

Upvotes: 1

asad raza
asad raza

Reputation: 432

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

Upvotes: 0

Related Questions