美文网首页
CSS左右垂直居中方法

CSS左右垂直居中方法

作者: 亦庄攻城狮 | 来源:发表于2017-11-14 15:21 被阅读0次
效果实例图

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>

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

相关文章

  • CSS垂直居中

    #CSS垂直居中的所有方法 标签(空格分隔): css技巧 --- ##1.利用padding垂直居中(line-...

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • CSS左右垂直居中方法

    1.溢出法介绍:外层div相对定位,内层div绝对定位 子容器绝对定位,top:0,bottom:0,left:0...

  • 1-css的多种垂直居中的方法

    多种css垂直居中的方法 ==css3不定宽高水平垂直居中== 1:固定高宽的垂直居中 如上图,固定高宽的很简单,...

  • 2019-01-26

    一、总结一下CSS的几种布局以及实现方法 左右布局 左中右 水平居中 垂直居中 1. 左右布局 左右布局很多种实现...

  • CSS - 垂直水平居中方法

    前言 总括:整理 css 垂直水平居中方法,区分内联元素与块级元素 CSS垂直居中和水平居中 用css让一个容器水...

  • CSS布局与居中

    这篇文章会介绍本人已学会的CSS中常用的左右布局、左中右布局、水平居中方法、垂直居中方法和一些CSS小技巧,如有错...

  • CSS布局

    如何使用CSS做出: 左右布局/左中右布局水平居中垂直居中 左右布局/左中右布局 在此提供两种实现方法,实际操作中...

网友评论

      本文标题:CSS左右垂直居中方法

      本文链接:https://www.haomeiwen.com/subject/mohfvxtx.html