垂直居中

垂直居中

1. display:flex

  • 通过display:flex实现CSS垂直居中的方法是给父元素display:flex;而子元素align-self:center;
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
            display: flex;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;
            align-self: center;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in"></div>
    </div>
</body>
</html>

2. :before

  • 为父元素添加伪元素:before,使得子元素实现垂直居中。
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
        }

        .box:before{
            content: "";
            display: inline-block;
            vertical-align: middle;
            height: 100%;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;
            display: inline-block;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in"></div>
    </div>
</body>
</html>

3. 隐藏节点

  • 创建一个隐藏节点.hide,使得隐藏节点的height值为剩余高度的一半
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
        }

        .hide{
            width: 50%;
            height: 25%;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="hide"></div>
        <div class="in"></div>
    </div>
</body>
</html>

4. transform(已知父元素高度)

  • 给子元素的position:relative,再通过translateY即可定位到垂直居中的位置。
  • 必须知道父元素高度
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;
            position: relative;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in"></div>
    </div>
</body>
</html>

5. transform(未知父元素高度)

  • 先给父元素position:relative,再给子元素position:absolute,通过translateY即可定位到垂直居中的位置
  • 无需知道父元素高度
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in"></div>
    </div>
</body>
</html>

6. margin

  • 给子元素添加margin: 0 auto;
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
        }

        .in{
            background-color: green;
            width: 50%;
            height: 50%;

            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in"></div>
    </div>
</body>
</html>

7. line-height

  • 适用于文本
  • 设置子元素的line-height值等于父元素的height,这种方法适用于子元素为单行文本的情况
  • 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            background-color: yellow;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .in{
            width: 50%;
            height: 50%;
            line-height: 300px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="in">hello world</div>
    </div>
</body>
</html>
上一篇:padding设置和清除


下一篇:JPQL和SQL的比较