[html] 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?

This question is related to html image css resize

The answer is


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

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


.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


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.

_x000D_
_x000D_
.example {_x000D_
  margin: 1em 0;_x000D_
  text-align: center;_x000D_
}_x000D_
_x000D_
.example img {_x000D_
  width: 30vw;_x000D_
  height: 30vw;_x000D_
}_x000D_
_x000D_
.example-cover img {_x000D_
  object-fit: cover;_x000D_
}_x000D_
_x000D_
.example-contain img {_x000D_
  object-fit: contain;_x000D_
}
_x000D_
<div class="example example-cover">_x000D_
  <img src="https://i.stack.imgur.com/B0EAo.png">_x000D_
  <img src="https://i.stack.imgur.com/iYkNH.png">_x000D_
  <img src="https://i.stack.imgur.com/gne9N.png">_x000D_
</div>_x000D_
_x000D_
<div class="example example-contain">_x000D_
  <img src="https://i.stack.imgur.com/B0EAo.png">_x000D_
  <img src="https://i.stack.imgur.com/iYkNH.png">_x000D_
  <img src="https://i.stack.imgur.com/gne9N.png">_x000D_
</div>
_x000D_
_x000D_
_x000D_

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


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.


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

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

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.


Set height and width parameters in CSS file

_x000D_
_x000D_
.ImageStyle{_x000D_
_x000D_
    max-height: 17vw;_x000D_
    min-height: 17vw;_x000D_
    max-width:17vw;_x000D_
    min-width: 17vw;_x000D_
    _x000D_
}
_x000D_
_x000D_
_x000D_


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

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;

}

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


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

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to image

Reading images in python Numpy Resize/Rescale Image Convert np.array of type float64 to type uint8 scaling values Extract a page from a pdf as a jpeg How do I stretch an image to fit the whole background (100% height x 100% width) in Flutter? Angular 4 img src is not found How to make a movie out of images in python Load local images in React.js How to install "ifconfig" command in my ubuntu docker image? How do I display local image in markdown?

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to resize

Numpy Resize/Rescale Image AngularJS $watch window resize inside directive Input type "number" won't resize How can I make all images of different height and width the same via CSS? How to get full width in body element Rerender view on browser resize with React How to resize image automatically on browser width resize but keep same height? PHPExcel auto size column width Automatically resize images with browser size using CSS Change form size at runtime in C#