使用 CSS 的值自動的 margin 屬性的作用


具有自動值的 margin 屬性用於水平居中容器內的元素。你可以嘗試執行下列程式碼來實現 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>

輸出

更新於︰2020 年 6 月 22 日

158 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.