百度总结的垂直居中方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度总结的垂直居中方法相关的知识,希望对你有一定的参考价值。

一  绝对定位与负边距

<!--兼容性不错,缺陷就是必须提前知道被居中块级元素的大小-->

技术分享

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>绝对定位与负边距</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background: #ccc;
            position: relative;
        }
        .child {
            width: 150px;
            height: 100px;
            background: lawngreen;
            position: absolute;
            top: 50%;
            margin: -50px 0 0 0;   /*50px为child高度的一半*/
            line-height: 100px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child">蜗牛小</div>
    </div>
</body>
</html>

二  使用绝对定位和transform

<!--不必提前知道被居中元素的尺寸了,translate偏移的百分比就是相对于元素自身的尺寸而言-->

技术分享

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>使用绝对定位和transform</title>
    <style>
        .box {
            width: 300px;
            height: 200px;
            background: lightpink;
            position: relative;
        }
        .child {
            background: springgreen;
            position: absolute;
            top: 50%;
            transform: translate(0, -50%);
        }
    </style>
</head>
<body>
<div class="box">
    <div class="child">
        蜗牛小蜗牛小蜗牛小
    </div>
</div>
</body>
</html>

三  绝对定位结合margin: auto

<!--居中元素的宽高也可以不设置,但不设置的话就必须是图片这种自身就包含尺寸的元素, 否则无法实现-->

技术分享

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>绝对定位结合margin: auto</title>
    <style>
        .box {
            width: 300px;
            height: 200px;
            background: lightpink;
            position: relative;
        }
        .child {
            width: 200px;
            height: 60px;
            background: #A1CCFE;
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            line-height: 60px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child">蜗牛小蜗牛小</div>
    </div>
</body>
</html>

四  使用flex布局

技术分享

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>使用flex布局</title>
    <style>
        .box {
            width: 300px;
            height: 200px;
            background: #ddd;
            display: flex;
            align-items: center;
        }
        .child {
            width: 300px;
            height: 60px;
            background: #8194AA;
            line-height: 60px;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="child">
        蜗牛小蜗牛小
    </div>
</div>
</body>
</html>

五 使用 line-height 和 vertical-align 对图片进行垂直居中

技术分享

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>使用 line-height 和 vertical-align 对图片进行垂直居中</title>
        <style>
            .box{
                width: 300px;
                height: 200px;
                background: #ddd;
                line-height: 200px;
            }
            .box img {
                vertical-align: middle;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <img src="tip.png" alt="请添加图片">
        </div>
    </body>
</html>

 

以上是关于百度总结的垂直居中方法的主要内容,如果未能解决你的问题,请参考以下文章

总结div里面水平垂直居中的实现方法

CSS水平垂直居中方法总结

实现文字图片垂直居中的总结性方法

CSS水平居中+垂直居中+水平/垂直居中的方法总结

转 CSS制作水平垂直居中对齐各种方法总结

CSS—总结常用垂直居中方法