zoukankan      html  css  js  c++  java
  • CSS实现垂直居中布局

    垂直居中

    首先将<html><body>的高度设置为100%(为演示父元素不定宽高的效果),并清除<body>的默认样式。

        html,body{
            margin: 0;
            height: 100%;
        }
    

    垂直居中大致分为两类,父元素定宽高与父元素不定宽高,将两类样式以及子容器设定好。

        .set-parent,.dy-parent{
             300px;
            height: 200px;
            background: #eee;
            margin: 10px 0;
        }
        .child{
             20px;
            height: 10px;
            background: #fff;
        }
        .dy-parent{ 
             30%;
            height: 20%;
        }
    

    父元素定宽高 position+margin

    使用margin: auto使水平居中,将子容器设定为relative以在不脱离文档流的情况下偏移50%,由于本身子容器占用一定宽高,会将其撑下,使用margin使其向上偏移。
    若是子容器使用absolute定位,则父容器应设置为relative,否则会其相对于static定位以外的第一个父元素进行定位,在本示例中会以浏览器为基准定位,此外absolute无法使用margin: auto水平居中。

        <!-- 父元素定宽高 position+margin -->
        <div class="set-parent" >
            <div class="child" style="position: relative;top: 50%;margin: auto;margin-top: -5px;"></div>
        </div>
    

    父元素定宽高 position+transform

    原理与position+margin相同,CSS3的transform使得div向上平移自身高度的50%

    <!-- 父元素定宽高 position+transform -->
        <div class="set-parent" >
            <div class="child" style="position: relative;top: 50%;margin: auto;transform: translateY(-50%);"></div>
        </div>
    

    父元素定宽高 position+calc

    css3提供calc函数,能够进行动态计算。

        <div class="set-parent" >
            <div class="child" style="position: relative;top: calc(50% - 5px);left: calc(50% - 10px);"></div>
        </div>
    

    父元素不定宽高 flex

    flex布局可以说是布局神器,极其强大,绝大部分现代浏览器都兼容性flex布局。

        <div class="dy-parent" style="display: flex;justify-content: center;align-items: center;">
            <div class="child" ></div>
        </div>
    

    父元素不定宽高 grid

    grid布局将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,grid布局与flex布局有一定的相似性,都可以指定容器内部多个项目的位置。但是,它们也存在重大区别,flex布局是轴线布局,只能指定"项目"针对轴线的位置,可以看作是一维布局,grid布局则是将容器划分成"行"和"列",产生单元格,然后指定"项目"所在的单元格,可以看作是二维布局,可以认为grid布局比flex布局强大。

        <div class="dy-parent" style="display: grid;justify-content: center;align-content: center;">
            <div class="child" ></div>
        </div>
    

    父元素不定宽高 table

    table中有vertical-align属性设置垂直对齐方式。

        <div class="dy-parent" style="display: table;">
            <div style="display: table-cell;vertical-align: middle;">
                <div class="child" style="margin: auto;" ></div>
            </div>
        </div>
    

    示例

    <!DOCTYPE html>
    <html>
    <head>
        <title>垂直居中</title>
        <meta charset="utf-8">
    </head>
    <body>
    
        <!-- 父元素定宽高 position+margin -->
        <div class="set-parent" >
            <div class="child" style="position: relative;top: 50%;margin: auto;margin-top: -5px;"></div>
        </div>
    
        <!-- 父元素定宽高 position+transform -->
        <div class="set-parent" >
            <div class="child" style="position: relative;top: 50%;margin: auto;transform: translateY(-50%);"></div>
        </div>
    
        <!-- 父元素定宽高 position+calc -->
        <div class="set-parent" >
            <div class="child" style="position: relative;top: calc(50% - 5px);left: calc(50% - 10px);"></div>
        </div>
    
        <!-- 父元素不定宽高 flex -->
        <div class="dy-parent" style="display: flex;justify-content: center;align-items: center;">
            <div class="child" ></div>
        </div>
    
        <!-- 父元素不定宽高 grid -->
        <div class="dy-parent" style="display: grid;justify-content: center;align-content: center;">
            <div class="child" ></div>
        </div>
    
        <!-- 父元素不定宽高 table -->
        <div class="dy-parent" style="display: table;">
            <div style="display: table-cell;vertical-align: middle;">
                <div class="child" style="margin: auto;" ></div>
            </div>
        </div>
    
    
    </body>
    <style type="text/css">
        html,body{
            margin: 0;
            height: 100%;
        }
        .set-parent,.dy-parent{
             300px;
            height: 200px;
            background: #eee;
            margin: 10px 0;
        }
        .child{
             20px;
            height: 10px;
            background: #fff;
        }
        .dy-parent{
             30%;
            height: 20%;
        }
    </style>
    </html>
    

    每日一题

    https://github.com/WindrunnerMax/EveryDay
    
  • 相关阅读:
    Ruby on Rails中的Rake教程(Rake如何把我灌醉!)
    十一、Spring Boot 集成Shiro和CAS
    Spring Boot 快速入门
    一位创业者的血泪史----献给85前创业者的反思
    罗振宇送给新员工的四句话
    Axure 入门
    XMind常用快捷方式汇总
    CAS 单点登陆
    mysql互换表中两列数据
    mysql默认安装目录说明
  • 原文地址:https://www.cnblogs.com/WindrunnerMax/p/12558294.html
Copyright © 2011-2022 走看看