CSS左右垂直居中方法

效果实例图

1.溢出法
介绍:外层div相对定位,内层div绝对定位

子容器绝对定位,top:0,bottom:0,left:0,right:0,margin:auto

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <style type="text/css">
        html,body{
            margin: 0; padding: 0; 
        }
        .divParent {
            position:relative;
            width:600px;
            height:400px;
            background:#ddd;
            margin: 0 auto; 
        }
        .divChild {
            position:absolute;
            top:0;
            bottom:0;
            left:0;
            right:0;
            margin:auto;
            background:green;
            width:30%;
            height:30%;
        }
    </style>
</head>
<body>
    <div class="divParent">
        <div class="divChild">这里是居中的子元素</div>
    </div>
</body>
</html>

2.计算法
介绍:外层div相对定位,内层div相对定位

子容器相对定位 top:50%,translateY(-50%)

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <style type="text/css">
        html,body{
            margin: 0; padding: 0; 
        }
        .divParent {
            position:relative;
            width:600px;
            height:400px;
            background:#ddd;
            margin: 0 auto; 
        }
        .divChild {
            position:relative;
            top:50%;
            margin:auto;
            transform:translateY(-50%);
            -ms-transform:translateY(-50%);     /* IE 9 */
            -moz-transform:translateY(-50%);    /* Firefox */
            -webkit-transform:translateY(-50%); /* Safari 和 Chrome */
            -o-transform:translateY(-50%);  /* Opera */
            background:green;
            width:30%;
            height:30%;
        }
    </style>
</head>
<body>
    <div class="divParent">
        <div class="divChild">这里是居中的子元素</div>
    </div>
</body>
</html>

3.宽高固定值计算法
介绍:外层div相对定位,内层div绝对定位

宽高一定 top left 50% ,margin-top、margin-tleft宽高各一半

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <style type="text/css">
        html,body{
            margin: 0; padding: 0; 
        }
        .divParent {
            position:relative;
            width:600px;
            height:400px;
            background:#ddd;
            margin: 0 auto; 
        }
        .divChild {
            position: absolute;
            width: 200px;
            height: 200px;
            top: 50%;
            left: 50%;
            margin-top: -100px;
            margin-left: -100px;
        }
    </style>
</head>
<body>
    <div class="divParent">
        <div class="divChild">这里是居中的子元素</div>
    </div>
</body>
</html>

4.calc计算法
介绍:外层相对,内层绝对

top: calc(50% - (* px/2)),left: calc(50% - (* px/2));

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <style type="text/css">
        html,body{
            margin: 0; padding: 0; 
        }
        .divParent {
            position:relative;
            width:600px;
            height:400px;
            background:#ddd;
            margin: 0 auto; 
        }
        .divChild {
            position: absolute;
            width: 200px;
            height: 200px;
            top: calc(50% - (200px/2));
            left: calc(50% - (200px/2));
            background: green;
        }
    </style>
</head>
<body>
    <div class="divParent">
        <div class="divChild">这里是居中的子元素</div>
    </div>
</body>
</html>

注:以上是个人实际应用中使用的主要三种方法、网上还有各样方法提供

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,844评论 1 92
  • 1.绝对定位居中技术 我们一直用margin:auto实现水平居中,而一直认为margin:auto不能实现垂直居...
    DecadeHeart阅读 1,632评论 0 3
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,239评论 3 30
  • 1.CSS基本概念 1.1 CSS的定义 CSS(Cascading Style Sheets)层叠样式表,主要用...
    寥寥十一阅读 1,902评论 0 6
  • 他很好,我却不怎么愿意提起他。 说起来有点早熟,因为我人生的第一次恋爱发生在小学。青梅竹马、两小无猜,当时以为他是...
    柒愔阅读 2,221评论 0 3