CSS 中 margin 属性 auto 的作用

cssweb developmentfront end technology更新于 2024/4/5 7:08:17

margin 属性 auto 用于使元素在其容器内水平居中。您可以尝试运行以下代码来实现 margin: auto;

示例

<!DOCTYPE html>
<html>
   <head>
      <style>
         div {
            width: 200px;
            margin: auto;
            border: 2px dashed blue;
         }
      </style>
   </head>
   <body>
      <p>This is demo text</p>
      <div>
         This is horizontally centered because it has margin: auto;
      </div>
   </body>
</html>

输出


相关文章


有用资源