Vertically Centering content in html [duplicate]

白昼怎懂夜的黑 提交于 2019-11-29 13:11:53

If you want center block verticaly you can use this trick's

margin is a half of your size bloc.

If you use this you need size on your bloc.

html :

<div id="center"></div>

css :

#center{
    position:absolute;
    left:50%;
    top:50%;
    height:400px;
    width:600px;
    margin-left:-300px;
    margin-top:-200px;
}

DEMO : http://jsfiddle.net/ucbRs/

or you can also use this :

css :

#center{
    position:absolute;
    left:100px;
    top:100px;
    bottom:100px;
    right:100px;
}

DEMO : http://jsfiddle.net/ucbRs/1/

or you can also use a CSS3 property align-content like this :

body{
    display:flex;
    flex-flow: row wrap;
    align-content:center;
}
#center{
    margin:0 auto;
}

DEMO : http://jsfiddle.net/ucbRs/3/

Welcome to SO!

Vertically centering a div is actually very simple.

div{
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

There you have it.

Demo

You might also consider absolute centering:

div{
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}

Demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!