zoukankan      html  css  js  c++  java
  • 如何让设置浮动的元素水平垂直居中

    1.多个子元素同时设置浮动后,欲想实现水平垂直居中,实现代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .clearFix::after{
                display:block;
                clear:both;
                content:"";
                visibility:hidden;
                height:0;
               
            }
            .clearFix{
                zoom: 1;
            }
            .container{
                 100%;
                height: 800px;
                background-color: blue;
                /* 水平垂直居中 */
                position: relative;
            }
            .child1{
                200px;
                height:200px;
                background-color: red;
            }
            .child2{
                200px;
                height: 200px;
                background-color: goldenrod;
            }
            .lf{
                float: left;
            }
            /* 水平垂直居中 */
            .box{
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%,-50%);
            }
        </style>
    </head>
    <body>
        <div class="container clearFix">
            <div class="box clearFix">
                <div class="child1 lf">child1</div>
                <div class="child2 lf">child2</div>
            </div>
        </div>
    </body>
    </html>
    

    2.使用flex布局(有兼容性)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .clearFix::after{
                display:block;
                clear:both;
                content:"";
                visibility:hidden;
                height:0
            }
            .clearFix{
                zoom: 1;
            }
            .container{
                 100%;
                height: 800px;
                background-color: blue;
                
                display: flex;
                /* 垂直居中 */
                align-items: center;
                /* 水平居中 */
                justify-content:center;
            }
            .child1{
                200px;
                height:200px;
                background-color: red;
            }
            .child2{
                200px;
                height: 200px;
                background-color: goldenrod;
            }
            .lf{
                float: left;
            }
        </style>
    </head>
    <body>
        <div class="container clearFix">
            <div class="child1 lf">child1</div>
            <div class="child2 lf">child2</div>
        </div>
    </body>
    </html>
    

    3.垂直居中使用display: table-cell; vertical-align: middle; 水平居中:嵌套一层div,设置宽度为子元素宽度,在设置margin: 0 auto;

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .clearFix::after{
                display:block;
                clear:both;
                content:"";
                visibility:hidden;
                height:0;
               
            }
            .clearFix{
                zoom: 1;
            }
            .container{
                 600px;
                height: 800px;
                background-color: blue;
                /* 水平垂直居中 */
                display: table-cell;
                vertical-align: middle;
            }
            .child1{
                200px;
                height:200px;
                background-color: red;
            }
            .child2{
                200px;
                height: 200px;
                background-color: goldenrod;
            }
            .lf{
                float: left;
            }
            /* 水平垂直居中 */  
            .box{
                400px;
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div class="container clearFix">
            <div class="box clearFix">
                <div class="child1 lf">child1</div>
                <div class="child2 lf">child2</div>
            </div>
        </div>
    </body>
    </html>
    
  • 相关阅读:
    npm 与 yarn 发展史
    关于oracle sql语句查询时表名和字段名要加双引号的问题
    Navicat工具mysql转库oracle步骤
    Linux根目录扩容方法及其涉及的相关磁盘操作
    Oracle中的数据类型详解
    一张图看懂钢铁生产工艺流程
    MYBATIS-PLUS关联查询,一对一、一对多
    直接替换Springboot jar包中的文件
    springboot配置数据库密码特殊字符报错问题
    教你一招,把 Win10 更新暂停到 N 年后的神奇方法
  • 原文地址:https://www.cnblogs.com/web-record/p/9144359.html
Copyright © 2011-2022 走看看