Victor Gorban
Victor Gorban

Reputation: 1699

How to make div height to be always proportional of its width (without hacks)?

I have a Flexbox div with 12 images within. I want width of every image to be 25% of that div. That image must have height = 133,33% of width. And in case image has dimensions that not equal to my 3*4, image has to be scaled to fit my 3*4. So how can I achieve that? I think I need a tool like calc(width*4/3).

My css and html:

.image-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
  width: 100%; 
  }
  .image-grid .image-wrapper {
    width: 25%;
    height: 133.3333%; // nothing happens if I delete this line. Flexbox?
    }
  .image-grid .image {
    display: block;
    width: 100%;
    object-fit: cover; 
    }
<div class="image-grid">
    <div class="image-wrapper"><img src="assets/img/portfolio-1.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-2.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-3.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-4.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-5.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-6.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-7.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-8.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-9.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-10.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-11.jpg" alt="" class="image"></div>
    <div class="image-wrapper"><img src="assets/img/portfolio-12.jpg" alt="" class="image"></div>
</div>
My code works perfect with images that are exactly 3*4, but if an image has other dimensions, I get that: bad image

Ok, the solution is found. Just set wrapper's padding as 133.33% instead of height (and set my img's position as absolute). This is what I'd call a "trick" or "hack", but it solves my problem rather clearly. More details here: description

Upvotes: 1

Views: 138

Answers (3)

jcal
jcal

Reputation: 875

you can maintain a 3:4 aspect-ratio on the wrapper by putting padding-top: 133.33%on it. So instead of setting the height, you can set the padding.

Also you need to set position: relative to the wrapper and position: absolute to the child, so the positioning of the child ignores the padding.

there's an example in the following snippet:

.row {
  display: flex;
  flex-wrap: wrap;
}

.column {
  width: 25%;
}

.wrapper {
  padding-top: 133.33%;
  border: 1px solid red;
  position: relative;
}

.wrapper img {
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  object-fit: cover;
}
<div class="row">
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
</div>

Upvotes: 2

Kosh
Kosh

Reputation: 18434

Try this:

.image-grid {
  display: flex;
  flex-wrap: wrap;
}

.image-grid .image-wrapper {
  position: relative;
  width: 25%;
  height: 0;
  padding-bottom: 33.3333%;
}

.image-grid .image {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="image-grid">
  <div class="image-wrapper"><img src="https://picsum.photos/200/300?1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/200?2.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?3.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/300?4.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/400?5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?7.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/400?8.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/400/200?9.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?2.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?4.jpg" alt="" class="image"></div>
</div>

Upvotes: 1

Johannes
Johannes

Reputation: 67798

Here's an idea that should work if the container spans the complete width of the window:

Make the image container 25% wide and 25vw*4/3 high (there you have your calc...). Also use overflow: hidden on the container to keep the fixed ratio.

Make the images 100% high and set their width to auto. This will make them fill the container vertically. Then center them by appliying transform: translate-x(-50%), position: relative and left: 50%;.

This only works for images whose height is less than 4/3 of the width, but from what I understood, that's the situation you have?

* {
  margin: 0;
}

.image-grid {
  display: flex;
  flex-wrap: wrap;
}

.image-grid .image-wrapper {
  flex-shrink: 1;
  flex-grow: 0;
  width: 25%;
  height: calc(25vw*4/3);
  overflow: hidden;
}

.image-grid .image {
  height: 100%;
  width: auto;
  display: block;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
<div class="image-grid">
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>

  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>

</div>

Upvotes: 0

Related Questions