CSS - margin-left 屬性



CSS margin-left 屬性設定元素左側邊距的寬度。

語法

margin-left: auto | length | percentage | initial | inherit;

屬性值

描述
auto 瀏覽器自動設定左側邊距。預設值。
長度 使用長度單位(例如 px、em、rem 等)設定左側邊距空間。允許負值。
百分比 使用百分比值(例如 10%)相對於包含元素設定左側邊距空間。
initial 將屬性設定為其預設值。
inherit 從父元素繼承屬性。

CSS margin-left 屬性示例

以下示例說明了使用不同值的 margin-left 屬性。

使用 auto 值的 margin-left 屬性

要允許瀏覽器根據可用空間自動計算左側邊距,我們使用 auto 值。以下示例顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding-top: 40px;
         padding-bottom: 40px;
         height: 70px;
         width: 490px;
         border: 2px solid gray;
      }

      .box {
         background-color: lightblue;
         padding: 10px;
         text-align: center;
         width: 30%;
         margin-left: auto;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-left property
   </h2>
   <h4>
      margin-left: auto;
   </h4>
   <div class="container">
      <div class="box">
         This box has margin-left: auto
      </div>
   </div>

</body>

</html>

使用長度值的 margin-left 屬性

要設定左側邊距,我們可以使用長度單位(例如 px、em、rem 等)指定邊距大小。以下示例顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding-top: 40px;
         padding-bottom: 40px;
         height: 70px;
         width: 490px;
         border: 2px solid gray;
      }

      .props {
         background-color: lightblue;
         padding: 10px;
         text-align: center;
         width: 30%;
      }

      .box1 {
         margin-left: 80px;
      }

      .box2 {
         margin-left: 10em;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-left property
   </h2>
   <h4>
      margin-left: 80px;
   </h4>
   <div class="container">
      <div class="box1 props">
         This box has margin-left: 80px
      </div>
   </div>
   <h4>
      margin-left: 10em;
   </h4>
   <div class="container">
      <div class="box2 props">
         This box has margin-left: 10em
      </div>
   </div>

</body>

</html>

使用百分比值的 margin-left 屬性

要設定左側邊距,我們可以使用百分比值(例如 10%(包含元素寬度的 10%))指定邊距大小。以下示例顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding-top: 40px;
         padding-bottom: 40px;
         height: 90px;
         width: 490px;
         border: 2px solid gray;
      }

      .props {
         background-color: lightblue;
         padding: 10px;
         text-align: center;
         width: 30%;
      }

      .box1 {
         margin-left: 20%;
      }

      .box2 {
         margin-left: 50%;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-left property
   </h2>
   <h4>
      margin-left: 20%;
   </h4>
   <div class="container">
      <div class="box1 props">
         This box has margin-left: 20% 
         of the width of the container
      </div>
   </div>
   <h4>
      margin-left: 50%;
   </h4>
   <div class="container">
      <div class="box2 props">
         This box has margin-left: 50% 
         of the width of the container
      </div>
   </div>

</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
margin-left 1.0 6.0 1.0 1.0 3.5
css_properties_reference.htm
廣告
© . All rights reserved.