要让box居中,可以使用Flexbox布局或者使用绝对定位。下面是两种方法的示例代码:

使用Flexbox布局:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>WebCat</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
      }
      .box {
        width: 200px;
        height: 200px;
        background: rgba(0, 0, 0, 1);
      }
    </style>
  </head>
  <body> 
    <div class="box"></div>
  </body>
</html>

使用绝对定位:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>WebCat</title>
    <style>
      body {
        position: relative;
        height: 100vh;
        margin: 0;
      }
      .box {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 200px;
        height: 200px;
        background: rgba(0, 0, 0, 1);
      }
    </style>
  </head>
  <body> 
    <div class="box"></div>
  </body>
</html>

这两种方法都可以将box居中显示在页面上。可以根据实际情况选择使用哪种方法

!DOCTYPE htmlhtml head	meta charset=utf-8 name=viewport content=width=device-width initial-scale=1 maximum-scale=1 user-scalable=no	titleWebCattitle style body positionrelative; wid

原文地址: https://www.cveoy.top/t/topic/iGyn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录