HTML full name hypertext Markup Language, is a markup language. It includes a series of tags. These tags unify the format of documents on the network and connect scattered Internet resources into a logical whole. HTML text is descriptive text composed of HTML commands that describe text, graphics, animations, sounds, tables, links, etc

There are two cases for vertically and horizontally centering block-level elements:

1. Element size is fixed

2. Element size is not fixed

<! 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>
        .wrapper {
            position: relative;
            height: 200px;
            width: 200px;
            margin-right: 20px;
            border: 1px solid black;
            float: left;
        }

        .box1 {
            width: 50px;
            height: 50px;
            border: 1px solid red;
            /* Change the element to the location element */
            position: absolute;
            /* Element distance, left is 50% */
            left: 50%;
            top: 50%;
            /* Make the left margin of the element, the top margin, 1/2 the width and height of the element
            margin-top: -25px;
            margin-left: -25px;
        }

        .box2 {
            width: 50px;
            height: 50px;
            border: 1px solid red;
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

        .box3 {
            width: 50px;
            height: 50px;
            border: 1px solid red;
            position: absolute;
            left: 50%;
            top: 50%;
            /* Sets the offset of the element with respect to itself to negative 50%(that is, half the element's size)*/
            transform: translate(-50%, -50%);
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="box1"></div>
    </div>
    <div class="wrapper">
        <div class="box2"></div>
    </div>
    <div class="wrapper">
        <div class="box3"></div>
    </div>
</body>

</html>
Copy the code