zoukankan      html  css  js  c++  java
  • 水平垂直居中常见方式总结

    水平垂直居中常见方式总结

    html结构为:

    <div class="parent">
        <div class="child"></div>
    </div>

    (1)父元素相对定位,子元素关键在于设置为绝对定位,margin:auto

    .parent{
        width:400px;
        height:400px;
        background:#afa;
        position:relative;
    }
    .child{
        position:absolute;
        left:0;
        bottom:0;
        right:0;
        top:0;
        margin:auto;
        background:yellow;
        width:100px;
        height:100px;
    }

    (2)父元素相对定位,子元素绝对定位,且设置transform:translate(-50%,-50%)

    .parent{
        width:400px;
        height:400px;
        background:#afa;
        position:relative;
    }
    .child{
        position:absolute;
        left:50%;
        top:50%;
        transform:translate(-50%,-50%);
        background:yellow;
        width:100px;
        height:100px;
    }

    (3)父元素设置为display:flex;justify-content:center;align-items:center

    .parent{
        width:400px;
        height:400px;
        display:flex;
        justify-content:center;
        align-items:center;
        background:#afa;
    }
    .child{
        width:100px;
        height:100px;
        background:yellow;
    }

     (4)设置父元素display:table-cell;text-align:center;vertical-align:center;子元素:display:inline-block;

    .parent{
        display:table-cell;
        width:400px;
        height:400px;
        background:#afa;
        text-align:center;
        vertical-align: middle;
    }
    .child{
        display:inline-block;
        width:100px;
        height:100px;
        background:yellow;
    }

                                                     

  • 相关阅读:
    细节决定成败,为什么他能挣15亿
    GLSL Notes
    Connecting Physics Bodies
    Pylint
    Physicals
    Advanced Scene Processing
    Working with Other Node Types II
    OperateParticleWithCodes
    SpriteParticle II
    Working with Other Node Types
  • 原文地址:https://www.cnblogs.com/mujinxinian/p/5887141.html
Copyright © 2011-2022 走看看