首页技术文章正文

如何使一个盒子水平垂直居中?

更新时间:2021-09-20 来源:黑马程序员 浏览量:

让盒子水平垂直居中的方法有哪些?有以下6中方式:

CSS培训班


盒子水平垂直居中方案1:利用定位(常用方法,推荐)


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方法1</title>
    <style>
        .parent {
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        .child {
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
            background-color: pink;
            width: 100px;
            height: 100px;
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>


盒子水平垂直居中方案2:利用 margin:auto;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方案2-黑马高级web前端培训http://web.itheima.com</title>
    <style>
        .parent {
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        .child {
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>


盒子水平垂直居中方案3:利用 display:table-cell

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方案3-黑马高级web前端培训http://web.itheima.com</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
        .child {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            background-color: red;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>


盒子水平垂直居中方案4:利用display:flex;设置垂直水平都居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方案4-黑马高级web前端培训http://web.itheima.com</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .child {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>


盒子水平垂直居中方案5:计算父盒子与子盒子的空间距离

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方案5-黑马高级web前端培训http://web.itheima.com</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        .child {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            margin-top: 200px;
            margin-left: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>


盒子水平垂直居中方案6:利用transform

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子居中方案6-黑马高级web前端培训http://web.itheima.com</title>
    <style>
        .parent {
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        .child {
            position: absolute;
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">子元素</div>
    </div>
</body>
</html>



猜你喜欢:

CSS怎样更换鼠标样式?

圆角矩形css怎么写?

清除input默认样式

CSS中px、em、rem有什么区别和不同?

分享到:
在线咨询 我要报名
和我们在线交谈!