CSS - margin-bottom 屬性



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

正值使其遠離相鄰元素,而負值使其更靠近。

可能的值

以下列表包含margin-bottom屬性的所有可能值。

  • <length> - 外邊距大小的固定值。

  • <percentage> - 外邊距的百分比,相對於包含塊的內聯大小測量,或在水平語言中writing-mode定義的寬度。

  • auto - 瀏覽器自行選擇合適的值。

應用於

所有元素,除了具有表格display型別(除了table-captiontableinline-table)的元素。它也應用於::first-letter

語法

margin-bottom = <length-percentage> | auto   

CSS margin-bottom - 基本示例

這是一個示例,其中包含可以傳遞給margin-bottom CSS 屬性的所有不同值。

<html>
<head>
<style>
   p {
      background-color: rgb(201, 238, 240);
      border: 1px solid black;
      width: fit-content;
   }

   .margin-px {
      margin-bottom: 30px;
   }

   .margin-perc {
      margin-bottom: 10%;
   }

   .margin-em {
      margin-bottom: 3em;
   }

   .margin-auto {
      margin-bottom: auto;
   }

   .margin-neg {
      margin-bottom: -10px;
   }
</style>
</head>
   
<body>
   <p class="margin-px">
      The bottom margin is 30px.
   </p>
   
   <p class="margin-perc">
      The bottom margin is 10%.
   </p>
   
   <p class="margin-em">
      The bottom margin is 3em.
   </p>
   
   <p class="margin-auto">
      The bottom margin will be set by the browser.
   </p>
   
   <p class="margin-neg">
      The bottom margin is -10px.
   </p>
</body>
</html>

inherit - 當您希望子元素的底部外邊距與其父元素的底部外邊距匹配時,請使用inherit屬性。

注意:inherit 值只能用於子元素,不能用於父元素。

CSS margin-bottom - 繼承

這是一個示例,其中子元素的margin-bottom繼承自父元素:

<html>
<head>
<style>
   .p {
      margin-bottom: 20%;
      }
   p.example {
      margin-bottom: inherit;
      border: 1px solid #4CAF50;
      }
</style>
</head>
<body>
<h4>margin-bottom property - inherit</h4> 
   <p>The margin-bottom is 20% for parent element.</p>
   <p class="example">A child element where the bottom margin is inherited from the parent (p) and is 20%.</p>
</body>
</html>
廣告