matt
matt

Reputation: 23

Pure CSS centering of absolutely positioned image of unknown height

I have the following code:

<div id=wrapper>
  <img id=my_img src="img.png" />
</div>

<style>
  #my_img{
    max-width: 75%;
    max-height: 80%;
    position: absolute;
    bottom: 60px;
  }
</style>

I need to center #my_img within the #wrapper, absolutely positioned 60px from the bottom of #wrapper. The catch is that the width of #my_img is not always known -- when max-height takes effect for a very wide viewport.

Any help will be most appreciated.

Upvotes: 0

Views: 129

Answers (2)

Pepi
Pepi

Reputation: 232

You can use

left : 50%; transform : translateX(-50%);

Upvotes: 0

Roy123
Roy123

Reputation: 393

margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;

Top property

Setting top/left/bottom/right to 0 would solve your problem possibly.

Found on: Source

Upvotes: 2

Related Questions