2

Here is a link to the working pen: https://codepen.io/Adam0410/pen/OBqRRN

HTML

<div id="imgWrapper">
  <img src="https://thestoryengine.co/wp-content/uploads/2017/11/The-crossroads-Section-images-02.png">
</div>

CSS

#imgWrapper {
  display: flex;
  justify-content: center;

  width: 100%;

  padding: 0 20px;
}

#imgWrapper img {
  width: 100%;
  max-width: 1660px;
  height: auto;
}

I don't want the image to exceed its original width (hence max-width) but I want it to scale down to the width of the viewport while maintaining its aspect ratio. Currently, the height does not scale with the width, why is this?

Adam Griffiths
  • 680
  • 6
  • 26
  • 60

2 Answers2

2

Why do you need display flex to imgWrapper? Change it to block and it will work as expected. In case you need flex in there add it to other wrapper.

#imgWrapper {
  display: block;
}
Mārcis P
  • 316
  • 3
  • 10
0

What about using the vm unit to your #imgWrapper making it always responsive:

#imgWrapper {
 display: flex;
 justyfy-content: center;
 vm: 100%;
 padding: 0 20px;
}
Matt
  • 492
  • 3
  • 15