CSS - margin-bottom 屬性



CSS margin-bottom 屬性設定元素底部邊距的寬度。

語法

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

屬性值

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

CSS 底部邊距屬性示例

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

使用 auto 值的底部邊距屬性

要允許瀏覽器根據可用空間自動計算底部邊距,我們使用 auto 值。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding: 20px;
      }

      .dimensions {
         padding: 10px;
         border: 3px solid black;
         text-align: center;
         width: 50%;
      }

      .box {
         background-color: lightblue;
         margin-bottom: auto;
      }

      .sample-box {
         background-color: lightgreen;
      }
   </style>
</head>

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

</body>

</html>

使用長度值的底部邊距屬性

要設定底部邊距,我們可以使用長度單位(例如 px、em、rem 等)指定邊距大小。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding: 20px;
      }

      .dimensions {
         background-color: lightblue;
         padding: 10px;
         border: 3px solid black;
         text-align: center;
         width: 50%;
      }

      .sample-box {
         background-color: lightgreen;
      }

      .box1 {
         margin-bottom: 40px;
      }

      .box2 {
         margin-bottom: 5em;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-bottom property
   </h2>
   <h4>
      margin-bottom: 40px;
   </h4>
   <div class="container">
      <div class="box1 dimensions">
         This box has margin-bottom: 40px
      </div>
      <div class="sample-box dimensions"> 
         sample box
      </div>
   </div>
   <h4>
      margin-bottom: 5em;
   </h4>
   <div class="container">
      <div class="box2 dimensions">
         This box has margin-bottom: 5em
      </div>
      <div class="sample-box dimensions"> 
         sample box
      </div>
   </div>

</body>

</html>

使用百分比值的底部邊距屬性

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

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         padding: 20px;
      }

      .dimensions {
         background-color: lightblue;
         padding: 10px;
         border: 3px solid black;
         text-align: center;
         width: 50%;
      }

      .sample-box {
         background-color: lightgreen;
      }

      .box1 {
         margin-bottom: 4%;
      }

      .box2 {
         margin-bottom: 18%;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-bottom property
   </h2>
   <h4>
      margin-bottom: 4%;
   </h4>
   <div class="container">
      <div class="box1 dimensions">
         This box has margin-bottom: 4% of container width
      </div>
      <div class="sample-box dimensions"> 
         sample box
      </div>
   </div>
   <h4>
      margin-bottom: 18%;
   </h4>
   <div class="container">
      <div class="box2 dimensions">
         This box has margin-bottom: 18% of container width
      </div>
      <div class="sample-box dimensions"> 
         sample box
      </div>
   </div>

</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
margin-bottom 1.0 6.0 1.0 1.0 3.5
css_properties_reference.htm
廣告