How does css achieve fast centering?

How does css achieve fast centering?


To put it simply, quick centering is to set the display: flex; attribute for its parent, and set the margin: auto; attribute for itself

For example

<!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>Document</title>
    <style>
        * {
      
      
            margin: 0px;
            padding: 0px;
        }

        html,body {
      
      
            width: 100%;
            height: 100%;
            display: flex;
        }
        .allitem{
      
      
            width: 100px;
            height: 100px;
            background-color: yellow;
            margin: auto;
        }
        .all {
      
      
            width: 500px;
            height: 500px;
            background-color: red;
            margin: auto;
            display: flex;
        }
    </style>
</head>

<body>
    <div class="all">
        <div class="allitem">
            
        </div>
    </div>
</body>

</html>

Guess you like

Origin blog.csdn.net/zhaojiaxing123/article/details/129685152