zoukankan      html  css  js  c++  java
  • 元素水平垂直居中(transform,margin,table-cell,jQuery)

    1.水平居中

    .div{
        margin:0 auto; (或者 margin:auto;
        width:500px;   
        height:300px;   
    }  

    2.使用margin水平垂直居中

    方式一:

    .div {
        text-align: center;
        line-height: 200px;
        border: 2px pink solid;
        width: 300px;
        height: 200px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin: -100px 0 0 -150px;
    }

    方式二:

    <!DOCTYPE html>
    <html>
        <head>
            <title>块级元素水平,垂直居中</title>
            <meta charset="utf-8">
            <style>
                .wrapper {
                    height: 600px;
                    border: 1px solid gray;
                }
                .box {
                    width: 100px;
                    height: 100px;
                    background: gold;
                    margin: 250px auto;
                }
            </style>
        </head>
        <body>
            <div class="wrapper">
                <div class="box"></div>
            </div>
        </body>
    </html>

    3.jquery实现DIV水平垂直居中

    .div {
        text-align: center;
        line-height: 200px;
        border: 2px pink solid;
        width: 300px;
        height: 200px; 
    }
    < script >
    $(window).resize(function(){ 
        $(".div").css({ 
            position: "absolute", 
            left: ($(window).width() - $(".div").outerWidth())/2, 
            top: ($(window).height() - $(".div").outerHeight())/2 
        });   
    }); 
    
    $(function(){ 
        $(window).resize(); 
    }); 
    < /script >

    4.使用css3 tansform属性

    <!DOCTYPE html>
    <html>
        <head>
            <title>块级元素水平,垂直居中</title>
            <meta charset="utf-8">
            <style>
                .wrapper {
                    height: 400px;
                    width:600px;
                    border: 2px solid pink;
                    border-radius:10px;
                }
                .box {
                    position:relative;
                    height:200px;
                    width:200px;
                    top:50%;
                    left:50%;
                    transform: translate(-50%,-50%);
                    background:#abcdef;
    
                }
            </style>
        </head>
        <body>
            <div class="wrapper">
                <div class="box">adfagagafajkfhla</div>
            </div>
        </body>
    </html>

    效果如下:

    单独设置垂直居中可使用:

    top:50%;
    tansfrom:translateY(-50%);

    单独使用水平居中可使用:

    left:50%;
    tramsform:translateX(-50%);

    5.table-cell

    注意:可能会破坏页面整体布局

    <!DOCTYPE html>
    <html>
        <head>
            <title>块级元素水平,垂直居中</title>
            <meta charset="utf-8">
            <style>
                .wrapper {
                    height: 400px;
                    width:600px;
                    border: 2px solid pink;
                    border-radius:10px;
                    display:table;
                }
                .box {
                    text-align:center;
                    position:relative;
                    display:table-cell;
                    vertical-align:middle;
                    background:#abcdef;
    
                }
            </style>
        </head>
        <body>
            <div class="wrapper">
                <div class="box">adfagagafajkfhla</div>
            </div>
        </body>
    </html>

    效果如下:

    6.使用示例:DIV创建水平垂直居中遮罩层

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" / >
    <title></title>
    <style>
    #overlay {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: #000;
        opacity: 0.5;
        filter: alpha(opacity = 50);
    }
    
    #win {
       position: absolute;
        top: 50%;
        left: 50%;
        width: 400px;
        height: 200px;
        background: #fff;
        margin: -102px 0 0 -202px;
        line-height: 200px;
        text-align: center;
        border: 4px solid #CCC;
    
    }
    </style>
    </head>
    <body>
        <div id="overlay" ></div >
        <div id="win" >
            Div层居中
        </div >
    </body>
    </html>

    效果:

  • 相关阅读:
    使用 Dockerfile 定制镜像
    UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
    UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
    LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
    LeetCode Number of Islands 岛的数量(DFS,BFS)
    LeetCode Triangle 三角形(最短路)
    LeetCode Swap Nodes in Pairs 交换结点对(单链表)
    LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
    HDU 5312 Sequence (规律题)
    LeetCode Letter Combinations of a Phone Number 电话号码组合
  • 原文地址:https://www.cnblogs.com/wishyouhappy/p/3681037.html
Copyright © 2011-2022 走看看