HTML 垂直居中body中的应用

应用场景: 在body中书写一个代码块, 使其相对于body垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>居中测试</title>
    <style type="text/css">
        body{
            position: relative;
            margin: 0;
            padding: 0;
            width: 100vw;
            height: 100vh;
        }
        #clock{
            width: 300px;
            height: 100px;
            margin: auto;
            position: absolute;
            top: 0; bottom: 0;
            left: 0; right: 0;
            background: chocolate;
        }
    </style>
</head>
<body>
    <!-- 需求: 利用setInterval定时器写一个走动的时钟 -->
    <div id="clock">
        111
    </div>
     
    <script>
        window.onload = function(){
        }
    </script>
     
</body>
</html>

版权声明:本文为u011033906原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。