教育行业A股IPO第一股(股票代码 003032)

全国咨询/投诉热线:400-618-4000

盒子水平垂直居中方法汇总

更新时间:2021年09月20日16时01分 来源:传智教育 浏览次数:

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

好口碑IT培训


盒子水平垂直居中方案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如何去除图片间隙?

如何用css画圆?

传智教育HTML&JS+前端培训课程

0 分享到:
和我们在线交谈!