flex和body标签实现垂直和水平居中

492 阅读1分钟

直接看代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .flex-container{
            background-color: green;
            height: 100%;
            width: 100%;
            display: flex;
            justify-content:center;
            position: absolute;
            align-items:center;
        }
        .flex-item{
            width: 200px;
            height: 200px;
            background-color: red;
            justify-content: center;
        }
    </style>
</head>
<body class="flex-container">
       <div class="flex-item">
        </div> 
</body>
</html>