zoukankan      html  css  js  c++  java
  • CSS3实现垂直居中

    
    

    CSS实现垂直居中的常用方法

      在前端开发过程中,盒子居中是常常用到的。其中 ,居中又可以分为水平居中和垂直居中。水平居中是比较容易的,直接设置元素的margin: 0 auto就可以实现;

    但是垂直居中相对来说是比较复杂一些的。下面我们一起来讨论一下实现垂直居中的方法。

    HTML代码:
    <div class="parent">
        <div class="child"></div>
    </div>  

    方法一、

    .parent {
        800px;
        height:500px;
        border:2px solid #000;
        position:relative;
    }
    .child {
        200px;
        height:200px;
        margin: auto;
        position: absolute;
        top: 0; left: 0; bottom: 0; right: 0;
        background-color: red;
    } 

    方法二、

    .parent {
        800px;
        height:500px;
        border:2px solid #000;
        display:table-cell;
        vertical-align:middle;
        text-align: center;
    }
    .child {
        200px;
        height:200px;
        display:inline-block;
        background-color: red;
    }
    方法三、
    .parent {
        800px;
        height:500px;
        border:2px solid #000;
        display:flex;
        justify-content:center;
        align-items:center;
    }
    .child {
        200px;
        height:200px;
        background-color: red;
    }
    方法四、
    .parent {
        800px;
        height:500px;
        border:2px solid #000;
        position:relative;
    }
    .child {
        300px;
        height:200px;
        margin:auto;
        position:absolute;/*设定水平和垂直偏移父元素的50%,再根据实际长度将子元素上左挪回一半大小*/
        left:50%;
        top:50%;
        margin-left: -150px;
        margin-top:-100px;
        background-color: red;
    }
    
    
    

      

     


    
    
    
    
    
    
  • 相关阅读:
    【训练题】最优比率生成树 P1696
    2019/9/15 校内模拟赛 考试报告
    b 解题报告
    HDU4714 Tree2cycle 解题报告
    2019/9/2 校内练习赛 考试报告
    2019/8/31 校内模拟赛 考试报告
    2019/8/29 校内模拟赛 考试报告
    康托展开
    洛谷P3807卢卡斯定理
    矩阵
  • 原文地址:https://www.cnblogs.com/caiyuqin/p/7099655.html
Copyright © 2011-2022 走看看