vertical center an image without knowing dimensions

后端 未结 6 689
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 03:04

I have centered a lot of stuff in my webdevelopment career, but I was wondering if there is a simple way to centering an image vertically without knowing the image\'s dimens

6条回答
  •  被撕碎了的回忆
    2021-01-19 03:22

    I found this while searching for a solution to a similar problem the solution uses CSS3 so won't work on IE8 and below.

    While this is an old question I figured an updated answer might be useful to someone:

    #parent{ width:400px; height:400px; background:red; /* just used to highlight box */ text-align:center; } #parent img{ position:relative; top: 50%; transform: translateY(-50%); }

    See this fiddle: http://jsfiddle.net/E9sTk/

提交回复
热议问题