A, positioning

1. The width and height of the box are known

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* The first type of width is known as the child absolute parent margin */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #f0f;
            background-color: steelblue;
        }
        .demo {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            margin-left: -100px;
            margin-top: -100px;
            background-color: thistle;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

2. Unknown width and height of the box

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* The second kind of width and height is unknown: the box itself has the width and height, I do not know the parent */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #ff0;
            background-color: tomato;
        }
        .demo {
            position: absolute;
            /* Position */
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            width: 200px;
            height: 200px;
            margin: auto;
            background-color: turquoise;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

3. Translation + positioning

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* Third translation + positioning */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #0ff;
            background-color: violet;
        }
        .demo {
            position: absolute;
            /* The origin of the upper left corner of the box is centered, but the element itself is not centered */
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            /* Each move 50% of its length and width left and up to center */
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

Second, the margin + padding

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* The fourth standard flow margin center: the width of the child box is lower than the width of the parent box, you can set the margin of the child box, horizontal values are set to auto. * /
        .box {
            width: 400px;
            /* Vertical center */
            padding: 100px 0;
            border: 10px solid #00f;
        }
        .demo {
            width: 200px;
            height: 200px;
            /* Horizontal center */
            margin: 0 auto;
            background-color: springgreen;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

Third, translation

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* Fifth translation */
        .box {
            width: 400px;
            height: 400px;
            border: 10px solid #0ff;
            background-color: violet;
        }
        .demo {
            width: 200px;
            height: 200px;
            margin-left: 50%;
            margin-top: 50%;
            background-color: yellowgreen;
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

Table -cell layout

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* Table -cell layout */
        .box {
            display: table-cell;
            vertical-align: middle;
            width: 400px;
            height: 400px;
            border: 10px solid rgb(0.255.98);
            background-color: rgb(238.130.197);
        }
        .demo {
            width: 200px;
            height: 200px;
            margin: 0 auto;
            background-color: rgb(50.187.205);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code

Flex layout

<! DOCTYPEhtml>
<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* Seventh flex layout */
        .box {
            display: flex;
            /* Set the spindle to vertical from the top down Y-axis */
            flex-direction: column;
            /* Side axis, loaded from the middle vertically centered */
            align-items: center;
            /* Align and center the spindle horizontally */
            justify-content: center;
            width: 400px;
            height: 400px;
            border: 10px solid rgb(200.255.0);
            background-color: rgb(234.130.238);
        }
        .demo {
            width: 200px;
            height: 200px;
            background-color: rgb(50.205.146);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
Copy the code