阅读829 返回首页    go 阿里云 go 技术社区[云栖]


CSS实现DIV水平 垂直居中-1

水平大家都知道,一般固定宽度给个margin:0 auto;就可以了。下面实现水平垂直都居中

HTML

<div class="parent">

</div>

css

复制代码
html,body{
    width: 100%;
    height: 100%;
}
.parent{
    width: 750px;
    height: 400px;
    background: orange;
    /*水平居中*/
    margin: 0 auto;
    position: relative;
    top: 50%;
    margin-top: -200px;/*高度的一半*/
}
复制代码

都居中了。也可以把margin-top:-200;换为CSS3新属性:transform:translateY(-50%);

最后更新:2017-11-05 12:05:17

  上一篇:go  Web前端开发工程师基本要求
  下一篇:go  CSS3中DIV水平垂直居中-2(3)