CSS - margin-block-end 屬性



CSS margin-block-end 屬性用於設定元素在塊方向末尾的邊距空間。 writing-mode 屬性決定了塊方向。

語法

margin-block-end: auto | length | percentage | initial | inherit;

屬性值

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

CSS Margin Block end 屬性示例

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

使用 Auto 值的 Margin Block End 屬性

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

示例

<!DOCTYPE html>
<html>

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

      .demo {
         background-color: lightgreen;
         width: 50%;
         padding: 15px;
         border: 3px dashed red;
         text-align: center;
      }

      .item {
         background-color: lightblue;
         border-bottom: 4px solid red;
      }

      .auto {
         margin-block-end: auto;

      }
   </style>
</head>

<body>
   <h2>
      CSS margin-block-end property
   </h2>
   <h4>
      margin-block-end: auto
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="auto demo item">
         This box has block-end edge margin = auto.
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
</body>

</html>

使用 Length 值的 Margin Block End 屬性

要設定元素的 block-end 邊緣的邊距,我們可以使用長度單位(例如 px、em、rem 等)指定邊距大小。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

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

      .demo {
         background-color: lightgreen;
         width: 50%;
         padding: 15px;
         border: 3px dashed red;
         text-align: center;
      }

      .item {
         background-color: lightblue;
         border-bottom: 4px solid red;
      }

      .single-value-px {
         margin-block-end: 30px;

      }

      .single-value-em {
         margin-block-end: 4em;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-block-end property
   </h2>
   <h4>
      margin-block-end: 30px
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="single-value-px demo item">
         This box has block-end edge margin = 30px
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
   <h4>
      margin-block-end: 4em
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="single-value-em demo item">
         This box has block-end edge margin = 4em
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
</body>

</html>

使用 Percentage 值的 Margin Block End 屬性

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

示例

<!DOCTYPE html>
<html>

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

      .demo {
         background-color: lightgreen;
         width: 50%;
         padding: 15px;
         border: 3px dashed red;
         text-align: center;
      }

      .item {
         background-color: lightblue;
         border-bottom: 4px solid red;
      }

      .example1 {
         margin-block-end: 7%;

      }

      .example2 {
         margin-block-end: 15%;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-block-end property
   </h2>
   <h4>
      margin-block-end: 7%
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="example1 demo item">
         This box has block-end edge margin = 7% 
         of the width of containing element.
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
   <h4>
      margin-block-end: 15%
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="example2 demo item">
         This box has block-end edge margin = 15% 
         of the width of containing element.
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
</body>

</html>

使用 Writing Mode 的 Margin Block End 屬性

margin-block-end 屬性可以與 writing-mode 屬性結合使用,後者決定了 block-end 的方向。在水平模式(如 horizontal-lr)中,block-end 為底部。在垂直模式(如 vertical-rl)中,block-end 為左側。這些在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

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

      .demo {
         background-color: lightgreen;
         width: 50%;
         padding: 15px;
         border: 3px dashed red;
         text-align: center;
      }

      .item-horizontal {
         border-bottom: 4px solid red;
      }

      .item-vertical {
         border-left: 4px solid red;
      }

      .margin {
         background-color: lightblue;
         margin-block-end: 50px;
      }

      .horizontal {
         writing-mode: horizontal-lr;
      }

      .vertical {
         writing-mode: vertical-rl;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-block-end property
   </h2>
   <h4>
      margin-block-end: 50px; writing-mode: horizontal-lr;
   </h4>
   <div class="container">
      <div class="demo">
         top box
      </div>
      <div class="horizontal demo item-horizontal margin">
         This box has block-end edge margin = 50px 
         and writing-mode: horizontal-lr;
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
   <h4>
      margin-block-end: 50px; writing-mode: vertical-rl;
   </h4>
   <div class="container vertical">
      <div class="demo">
         top box
      </div>
      <div class="vertical demo item item-vertical margin">
         This box has block-end edge margin = 50px 
         and writing-mode: vertical-rl;
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
margin-block-end 87.0 87.0 41.0 12.1 73.0
css_properties_reference.htm
廣告