Make an image fit its container by setting the image height to the containers heights
Asked Answered
M

2

5

Say I have container.a and it has some height in pixels, say I have another container.b within container.a that is 80% of container.a, now say I want to fit an image that has some height in pixels into container.b, how can I make the image be the height of container.b, and then maintain the width aspect ratio using CSS?

<div class="container.a">
  <div class="container.b">
    <img class="image.a" src="my_image.png" />
  </div>
</div>

.container.a { width: 200px; height: 300px; }
.container.b { width: 80%; height: 80%; }
.image.a { ? }
Meteoric answered 16/4, 2013 at 12:51 Comment(0)
R
7

Give 100% width of image

 .image.a{width:100%;}
Raimes answered 16/4, 2013 at 12:53 Comment(0)
D
5

Use this:

.image.a { height:100%; width:auto }
Disharoon answered 16/4, 2013 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.