How to Vertically center images and text in CSS

只愿长相守 提交于 2019-12-02 07:15:31

Here you go.

WORKING DEMO

The CSS Change:

#header h1 {
    color: #FFFFFF;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    width: 100%;
}

Hope this helps.

Personally I use 3 methods depending upon the use.

FIDDLE

1) translate()

2) line-height

3) vertical-align

<div id="div1">
    <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg"/>
</div>
<div id="div2">
    <h1>Hello</h1>
</div>
<div id="div3">
          <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg"/>
</div>

#div1, #div2, #div3 {
    display:block;
    margin:20px auto;
    width:400px;
    height:200px;
    background-color:#ccc;
    text-align:center;
    position:relative;
}
#div1 img {
    height:100px;
    position:absolute;
    top:50%;
    left:50%;
    -webkit-transform:translateY(-50%) translateX(-50%);
}
#div2 h1 {
    line-height:200px;
}
#div3 {
    display:table-cell;
    vertical-align:middle;
}
#div3 img {
    height:100px;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!