zoukankan      html  css  js  c++  java
  • div居中的几种方式

    方式一:flex 布局

    <style>
        .box {
             200px;
            height: 200px;
            border: 1px solid blue;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    
        .item {
            background: red;
        }
    </style>
    <div class="box">
        <div class="item">
            <h1 >宽高不定</h1>
        </div>
    </div>

    方式二: Position + transform

    <style>
        .box {
             200px;
            height: 200px;
            border: 1px solid blue;
            position: relative;
        }
    
        .item {
            background: red;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
    <div class="box">
        <div class="item">
            <h1>宽高不定</h1>
        </div>
    </div>

    方式三:position + margin

    <style>
    .box {
         200px;
        height: 200px;
        border: 1px solid blue;
        position: relative;
    }
    
    .item {
         50%;
        height: 50%;
        background: red;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
    </style>
    <div class="box">
        <div class="item">
            <h1>宽高不定</h1>
        </div>
    </div>

    方式四: Grid布局

    <!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="utf-8">
        <title>三十课 - 利用grid布局实现元素水平垂直居中示例</title>
        <style>
            .parent {
                height: 140px;
                display: grid;
                align-items:center;
                border: 2px dashed #f69c55;
            }
            .child {
                margin:auto;
                padding: 20px;
                10rem;
                color: #fff;
                background: black;
            }
        </style>
    </head>
    <body>
    <div class="parent">
        <div class="child">好的程序员能写出人能读懂的代码。</div>
    </div>
    </body>
    </html>
  • 相关阅读:
    【51nod1674】区间的价值 V2(算法效率--位运算合并优化+链表实现)
    【bzoj 2339】[HNOI2011]卡农(数论--排列组合+逆元+递推)
    关于中国剩余定理{附【转】中国剩余定理 }
    JavaScript操作BOM
    学员操作—统计考试平均成绩
    JavaScript基础
    JDBC
    进制
    事务
    复习
  • 原文地址:https://www.cnblogs.com/webljl/p/14517457.html
Copyright © 2011-2022 走看看