垂直居中

标签(空格分隔): 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">
    ![](http://upload-images.jianshu.io/upload_images/5363773-3605530804c7f972.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
</div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,813评论 1 92
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,215评论 3 30
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,806评论 0 2
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,339评论 0 11
  • 标签: 1、2个男孩子的妈妈 2、集团公司的HRM 3、打造事业生涯的践行者 第二个90天目标: 1、体重90斤 ...
    左叶右李阅读 146评论 0 0