Block-level boxes are centered horizontally

1. Margins can align block-level boxes in the center, but two conditions must be met

(1) The box must specify a width (width)

(2) The left and right margins of the box are set to auto

2. Code:

<!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>块级盒子水平居中对齐</title>
    <style>
        .header{
                 width: 900px;
                 height: 200px;
                 background-color: pink;
                 margin: 0 auto;
        }
    </style>
</head>

<body>
        <div class="header"></div>
</body>

</html>

The most common way to write: margin: 0 auto

3. Operation effect:

 

Guess you like

Origin blog.csdn.net/weixin_42900834/article/details/123459811