CSS - margin-block-start 属性



CSS margin-block-start 属性用于设置元素在块方向起始处的边距空间。writing-mode 属性决定块方向。

语法

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

属性值

描述
auto 浏览器会自动设置块起始边缘的边距。默认值。
长度值 使用长度单位(例如 px、em、rem 等)设置块起始边缘的边距空间。允许使用负值。
百分比 使用百分比值(例如 10%)相对于包含元素设置块起始边缘的边距空间。
initial 将其属性设置为默认值。
inherit 从父元素继承该属性。

CSS Margin Block Start 属性示例

以下示例说明了margin-block-start 属性的不同值。

使用 auto 值的 Margin Block Start 属性

为了让浏览器根据可用空间自动计算块起始边缘的边距,我们使用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-top: 4px solid red;
      }

      .auto {
         margin-block-start: auto;

      }
   </style>
</head>

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

</html>

使用长度值的 Margin Block Start 属性

要设置元素的block-start 边缘的边距,我们可以使用长度单位(例如 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-top: 4px solid red;
      }

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

      }

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

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

</html>

使用百分比值的 Margin Block Start 属性

要设置元素的block-start 边缘的边距,我们可以使用百分比值(例如 10%(包含元素宽度的 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-top: 4px solid red;
      }

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

      }

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

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

</html>

带有书写模式的 Margin Block Start 属性

margin-block-start 属性可以与writing-mode 属性结合使用,后者决定块起始方向。在水平模式(如 horizontal-lr)中,block-start 为顶部。在垂直模式(如 vertical-rl)中,block-start 为右侧。以下示例显示了这些情况。

示例

<!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-top: 4px solid red;
      }

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

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

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

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

<body>
   <h2>
      CSS margin-block-start property
   </h2>
   <h4>
      margin-block-start: 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-start edge margin = 50px 
         and writing-mode: horizontal-lr;
      </div>
      <div class="demo">
         bottom box
      </div>
   </div>
   <h4>
      margin-block-start: 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-start 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-start 87.0 87.0 41.0 12.1 73.0
css_properties_reference.htm
广告