vertical center an image without knowing dimensions

后端 未结 6 777
爱一瞬间的悲伤
爱一瞬间的悲伤 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:14

    If your design doesn't allow you to make parent inline (if it does, use Purmou's solution, it's better), you can set line-height on parent equal to its height and vertical-align: middle; on your img.

    Demo: http://jsfiddle.net/ThinkingStiff/YRGBk/

    HTML:

    CSS:

    img {
        vertical-align: middle;
    }
    
    #parent {
        height:400px;
        border:1px solid black;
        line-height: 400px;
    }
    

    Output:

    enter image description here

提交回复
热议问题