美文网首页
垂直居中的这点事

垂直居中的这点事

作者: lvyweb | 来源:发表于2017-08-25 16:02 被阅读48次

标签(空格分隔): css


垂直居中浮动元素

垂直居中元素,在布局中经常使用,总结一下:

方法一:已知元素的高宽

#div{
    width:100px;
    height:100px;
    position: absolute;        /*父元素需要相对定位 */
    top: 50%;
    left: 50%;
    margin-top:-50px ;   /* 二分之一的height,width */
    margin-left: -50px;
    background-color:#6699FF;
    }

方法二:未知元素的宽高

#div{
    width: 100px;
    height: 100px;
    background-color: #6699FF;
    margin:auto;
    position: absolute;        /*父元素需要相对定位 */
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

}

方法三:使用translate

div{
    background-color: #6699FF;
    width: 100px;
    height: 100px;
    margin: auto;
    position: absolute;/*父元素需要相对定位 */
    left: 50%; 
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
}

方法四:使用flex布局

div{
    background-color: skyblue;
    width: 400px;
    height: 400px;
    display: flex;
    justify-content: center;/*实现水平居中*/
    align-items:center; /*实现垂直居中*/
}

垂直居中一个<img>

#div{      /*img的父盒子 */
    display:table-cell;
    text-align:center;
    vertical-align:middle;
}

垂直居中一个<img>并且图片自适应高度不失真

div{   /*父元素 */
   text-align: center;
    height: 110px;
    position: relative;
}
div img{
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    margin: auto;
    position: absolute;/*父元素需要相对定位 */
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
}

上面几种方法的demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垂直居中</title>
    <style type="text/css">
        div.item1{
            width: 400px;
            height: 400px;
            background-color: skyblue;
            position: relative;
        }
        .item1 .tip{
            width:100px;
            height:100px;
            position: absolute;        /*父元素需要相对定位 */
            top: 50%;
            left: 50%;
            margin-top:-50px ;   /* 二分之一的height,width */
            margin-left: -50px;
            background-color:#6699FF;
        }
        /* ======================================================= */
        .box{
            height: 400px;
            width: 400px;
            background-color: skyblue;
        }
        div.item2{
            width: 100%;
            height: 100%;
            position: relative;
        }
        .item2 .tip{
            width: 100px;
            height: 100px;
            background-color: #6699FF;
            margin:auto;
            position: absolute;        /*父元素需要相对定位 */
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;

        }
        /* ======================================================= */
        .item3{
            height: 400px;
            width: 400px;
            background-color: skyblue;
            position: relative;

        }
        .item3 .tip{
            background-color: #6699FF;
            width: 100px;
            height: 100px;
            margin: auto;
            position: absolute;
            left: 50%; 
            top: 50%;
            transform: translateX(-50%) translateY(-50%);
            -webkit-transform: translateX(-50%) translateY(-50%);
        }
        /* ======================================================= */
        .item4{
            background-color: skyblue;
            width: 400px;
            height: 400px;
            display: flex;
            justify-content: center;/*实现水平居中*/
            align-items:center; /*实现垂直居中*/
        }
        .item4 .tip{
            background-color: #6699FF;
            width: 100px;
            height: 100px;
        }
        /* ======================================================= */
        .item5{
            width: 400px;
            height: 400px;
            background-color: skyblue;
            display:table-cell;
            text-align:center;
            vertical-align:middle;
        }
    </style>
</head>
<body>
<h2>一、已知元素的宽高</h2>
<div class="item1">
    <div class="tip"></div>
</div>
<h2>二、未知元素的宽高</h2>
<div class="box">
    <div class="item2">
    <div class="tip"></div>
    </div>
</div>
<h2>三、利用translate</h2>
<div class="item3">
    <div class="tip"></div>
</div>
<h2>四、使用flex布局</h2>
<div class="item4">
    <div class="tip"></div>
</div>

<h2>四、img居中</h2>
<div class="item5">
    ![](https://img.haomeiwen.com/i5363773/3605530804c7f972.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
</div>
</body>
</html>

相关文章

  • 垂直居中的这点事

    标签(空格分隔): css 垂直居中浮动元素 垂直居中元素,在布局中经常使用,总结一下: 方法一:已知元素的高宽 ...

  • CSS居中布局方案

    水平居中 垂直居中 水平垂直居中

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • 元素居中的方式

    1 水平居中 2 垂直居中 3 水平垂直居中

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • 垂直居中

    文字的垂直居中 元素的垂直居中

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • 未知高度垂直水平居中

    未知宽高的弹出框,水平垂直居中 parrent 内的元素水平垂直居中 parrent 内的元素垂直居中

  • 居中对齐

    行内元素居中[#hang]垂直居中[#hc]水平居中[#hs] 块级元素居中[#kuai]垂直居中[#kc]水平居...

网友评论

      本文标题:垂直居中的这点事

      本文链接:https://www.haomeiwen.com/subject/jloudxtx.html