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


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

    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;
    }

                                                     

  • 相关阅读:
    MySQL数据库优化的八种方式(经典必看)
    HTML5之应用缓存---manifest---缓存使用----HTML5的manifest缓存
    ajax方法总结
    十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less))
    MySQL主从复制技术(纯干货)
    table不能遗露了tbody
    DOM 之selection
    DOM 其他一些特性
    CSSOM视图模式
    DOM 节点实例操作
  • 原文地址:https://www.cnblogs.com/mujinxinian/p/5887141.html
Copyright © 2020-2023  润新知