Home > OS >  How can I make images same size and responsive?
How can I make images same size and responsive?

Time:12-29

I am trying to build a photo blog, but I couldn't make the photo size the same.

I tried setting up width and height but it isn't working. I want to make to images responsive.

body {
  margin: 0;
  background-color: #212529;
}

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

img {
  width: 40%;
  height: auto;
  margin: 20px;
}

nav {
  font-size: larger;
  font-weight: bolder;
  font-family: 'Gill Sans';
  margin-left: 20px;
  border-bottom: 2px solid white;
  width: 50%;
  padding: 20px;
  box-sizing: border-box;
  color: white;
}
<nav>AVOCADO</nav>

<img src="https://live.staticflickr.com/4293/36118563896_e15e35df3d_4k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/47767746632_c56a01ea72_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/52053626044_eaa960e111_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/52512684817_946eeaa976_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/49983962043_e047efb5c7_h.jpg" alt="">
<img src="https://live.staticflickr.com/65535/49462720238_f13f6236a8_h.jpg" alt="">
<img src="https://live.staticflickr.com/65535/51054945653_e6605123ed_k.jpg" alt="">
<img src="https://live.staticflickr.com/2653/3963581618_c8513a6304_b.jpg" alt="">
<img src="https://live.staticflickr.com/1537/24596554485_29e9e67f52_b.jpg" alt="">
<img src="https://live.staticflickr.com/1896/44785148891_a4f8e41d9d_k.jpg" alt="">

CodePudding user response:

You will want to use img { max-width: 100%;} for the images to be responsive.

You already have a height and width defined on img which should make them all the same size. Now just add the max-width for responsiveness.

img {
  max-width: 100%;
}

/* for demonstration */
img:not(:first-child) {
  width: 100px;
  height: 100px;
}
<img src="https://dummyimage.com/600x400/000/fff">
<img src="https://dummyimage.com/600x400/000/fff">

CodePudding user response:

Try the following CSS:

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

OR

and in image tag and add following css code :

.responsive{
             max-width: 100%;
             height: auto;
           }

Refer : W3School-Resposive

CodePudding user response:

Setting img to width: 40vh & max-height: 100px. Example:

body {
  margin: 0;
  background-color: #212529;
}

img {
  width: 40vh;
  margin: 20px;
  max-height: 100px;
}

nav {
  font-size: larger;
  font-weight: bolder;
  font-family: 'Gill Sans';
  margin-left: 20px;
  border-bottom: 2px solid white;
  width: 50%;
  padding: 20px;
  box-sizing: border-box;
  color: white;
}
<nav>AVOCADO</nav>
<img src="https://live.staticflickr.com/4293/36118563896_e15e35df3d_4k.jpg">
<img src="https://live.staticflickr.com/65535/47767746632_c56a01ea72_k.jpg">
<img src="https://live.staticflickr.com/65535/52053626044_eaa960e111_k.jpg">
<img src="https://live.staticflickr.com/65535/52512684817_946eeaa976_k.jpg">
<img src="https://live.staticflickr.com/65535/49983962043_e047efb5c7_h.jpg">
<img src="https://live.staticflickr.com/65535/49462720238_f13f6236a8_h.jpg">
<img src="https://live.staticflickr.com/65535/51054945653_e6605123ed_k.jpg">
<img src="https://live.staticflickr.com/2653/3963581618_c8513a6304_b.jpg">
<img src="https://live.staticflickr.com/1537/24596554485_29e9e67f52_b.jpg">
<img src="https://live.staticflickr.com/1896/44785148891_a4f8e41d9d_k.jpg">

CodePudding user response:

The images' ratio differ from one another, so you could use object-fit: cover to crop the images at a maximized size, like so:

body {
  margin: 0;
  background-color: #212529;
}

img {
  margin: 20px;
  object-fit: cover;
  max-width: 400px;
  max-height: 400px;
  width: 100%;
  height: 100%;
}

nav {
  font-size: larger;
  font-weight: bolder;
  font-family: 'Gill Sans';
  margin-left: 20px;
  border-bottom: 2px solid white;
  width: 50%;
  padding: 20px;
  box-sizing: border-box;
  color: white;
}


/*from here the responsive code*/

@media (max-width: 700px) {
  img {
    max-width: 200px;
    max-height: 200px;
  }
}
<nav>AVOCADO</nav>

<img src="https://live.staticflickr.com/4293/36118563896_e15e35df3d_4k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/47767746632_c56a01ea72_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/52053626044_eaa960e111_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/52512684817_946eeaa976_k.jpg" alt="">
<img src="https://live.staticflickr.com/65535/49983962043_e047efb5c7_h.jpg" alt="">
<img src="https://live.staticflickr.com/65535/49462720238_f13f6236a8_h.jpg" alt="">
<img src="https://live.staticflickr.com/65535/51054945653_e6605123ed_k.jpg" alt="">
<img src="https://live.staticflickr.com/2653/3963581618_c8513a6304_b.jpg" alt="">
<img src="https://live.staticflickr.com/1537/24596554485_29e9e67f52_b.jpg" alt="">
<img src="https://live.staticflickr.com/1896/44785148891_a4f8e41d9d_k.jpg" alt="">

  • Related