zoukankan      html  css  js  c++  java
  • 详解flex布局和常用垂直居中

    1. 介绍

    上一篇讲了Flex是Flexible Box的缩写,意为"弹性布局"。任何一个容器都可以指定为Flex布局

    注意,父元素设为Flex布局后,子元素的float、clear和vertical-align属性都将失效

    2. 相对定位+绝对定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #box {
                 300px;
                height: 200px;
                background-color: indianred;
                /* 相对定位 */
                position: relative;
            }
            #child {
                 100px;
                height: 100px;
                background-color: gray;
                /* 绝对定位 */
                position: absolute;
                top: 0;
                left: 0;
                bottom: 0;
                right: 0;
                margin: auto;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id="child">
    
            </div>
        </div>
    </body>
    </html>
    

    父元素设置相对定位(relative),子元素设置绝对定位(absolute),margin:auto

    3. flex布局

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #box {
                 300px;
                height: 200px;
                background-color: indianred;
                /* 设置flex */
                display: flex;
                /* 决定flex中项目的排列方向:column是列,default是row */
                flex-direction: column;
                /* 决定项目的对齐方式:center是居中 */
                justify-content: center;
            }
            #child {
                 100px;
                height: 100px;
                background-color: gray;
                /* 继承父元素align-items属性,如果没有,则等同于stretch。就是继承父元素(id为box)的align-items */
                align-self: center;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id="child">
            </div>
        </div>
    </body>
    </html>
    
    

    4. 相对定位+绝对定位+位移(transform)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #box {
                 300px;
                height: 200px;
                background-color: indianred;
                position: relative;
            }
    
            #child {
                 100px;
                height: 100px;
                background-color: gray;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%)
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id="child">
            </div>
        </div>
    </body>
    </html>
    

    5. table布局(不推荐使用)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #box {
                 300px;
                height: 200px;
                background-color: indianred;
                padding: 100px;
                display: table;
            }
            #child {
                 100px;
                height: 100px;
                background-color: gray;
                display: table-cell;
                vertical-align: center;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id="child">
            </div>
        </div>
    </body>
    </html>
    
  • 相关阅读:
    网上购物瘾,你怎么能退出?
    POJ 1006 Biorhythms 中国的法律来解决剩余的正式
    【Android接口实现】PhotoView——单点支持/多图像缩放,实现了触摸
    线程同步synchronized
    阿里云CentOS 6.5 设备、执行Docker容器和步骤的方法
    打破了中国电信华为无线路由猫(HG522-C)自己主动拨号+任意数量的计算机+iTV
    GCC 命令行具体解释
    Nginx 负载均衡
    Linux pipe功能
    Java有用的经验--Swing片
  • 原文地址:https://www.cnblogs.com/Ink-kai/p/12451553.html
Copyright © 2011-2022 走看看