zoukankan      html  css  js  c++  java
  • 常用垂直居中方法

    常用垂直居中方法:

    <!-- html结构 -->
    <body>
    <div class="wrap">
        <div class="box"></div>
    </div>
    </body>
    /* css样式 */
    /* (1) 模仿单行文字居中的方式 */
    .wrap {
    width: 200px;
    height: 80px;
    line-height: 80px;
    }
    
    .box {
    display: inline-block;
    vertical-align:middle;
    }
    
    /* (2) 已知宽高,通过position:absolute; */
    .wrap {
    width: 200px;
    height: 200px;
    position: relative;
    }
    
    .box {
    width: 100px;
    height: 80px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -50px 0 0 -40px;
    }
    
    /* (3) 未知宽高,通过css3属性 transfrom */
    .wrap {
    width: 200px;
    height: 200px;
    position: relative;
    }
    
    .box {
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    }
    
    /* (4) 通过flex布局 */
    <!-- html结构 -->
    <body>
    <div class="wrap flexbox flexbox-center flexbox-middle">
        <div class="box"></div>
    </div>
    </body>
    
    /* css样式 */
    
    .flexbox {
    display: -webkit-box; 
    display: -moz-box; 
    display: -ms-flexbox; 
    display: -webkit-flex;
    display: flex;
    }
    
    /* 水平居中 */
    .flexbox-center {
    -webkit-box-pack: center; 
    -moz-box-pack: center; 
    -ms-flex-pack: center; 
    -webkit-justify-content: center;
    justify-content: center;
    }
    
    /* 垂直居中 */
    .flexbox-middle {
    -webkit-box-align: center; 
    -moz-box-align: center;
    -ms-flex-align: center; 
    -webkit-align-items: center;
    align-items: center;
    }
  • 相关阅读:
    HashTable、HashSet和Dictionary的区别
    CCF_ 201312-3_最大的矩形
    CCF_ 201312-2_ISBN号码
    CCF_201312-1_出现次数最多的数
    CCF_ 201509-2_日期计算
    CCF_ 201512-3_画图
    CCF_ 201512-2_消除类游戏
    CCF_ 201409-2_画图
    CCF_201409-1_相邻数对
    CCF_ 201412-1_门禁系统
  • 原文地址:https://www.cnblogs.com/qiye2016/p/6774988.html
Copyright © 2011-2022 走看看