CSS - margin-inline-start 屬性



CSS margin-inline-start 屬性設定元素的內聯起始邊的邊距。 書寫模式方向 屬性定義了內聯方向。

語法

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

屬性值

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

CSS 內聯起始邊距屬性示例

以下示例使用不同的值解釋了margin-inline-start 屬性。

使用 auto 值的內聯起始邊距屬性

為了允許瀏覽器根據可用空間自動計算元素的內聯起始邊的邊距,我們使用auto 值。以下示例顯示了這一點。

示例

<!DOCTYPE html>
<html>

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

      .container>div {
         width: 150px;
         height: 200px;
         float: left;
         box-sizing: border-box;
      }

      .props {
         text-align: center;
         padding: 10px;
      }

      .auto-box {
         background-color: lightblue;
         border: 3px solid blue;
         margin-inline-start: auto;
      }

      .demo-box {
         background-color: #04bf52;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-start property
   </h2>
   <h4>
      margin-inline-start: auto
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="auto-box props">
         This box has margin-inline-start: auto
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
</body>

</html>

使用長度值的內聯起始邊距屬性

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

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         height: 200px;
         width: 490px;
         padding: 20px;
      }

      .container>div {
         width: 150px;
         height: 200px;
         float: left;
         box-sizing: border-box;
      }

      .props {
         text-align: center;
         padding: 10px;
      }

      .center {
         background-color: lightblue;
         border: 3px solid blue;
      }

      .px-box {
         margin-inline-start: 20px;
      }

      .em-box {
         margin-inline-start: 2em;
      }

      .demo-box {
         background-color: #04bf52;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-start property
   </h2>
   <h4>
      margin-inline-start: 10px
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="px-box props center">
         This box has margin-inline-start: 10px 
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
   <h4>
      margin-inline-start: 2em
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="em-box props center">
         This box has margin-inline-start: 2em;
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
</body>

</html>

使用百分比值的內聯起始邊距屬性

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

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         height: 200px;
         width: 490px;
         padding: 20px;
      }

      .container>div {
         width: 150px;
         height: 200px;
         float: left;
         box-sizing: border-box;
      }

      .props {
         text-align: center;
         padding: 10px;
      }

      .center {
         background-color: lightblue;
         border: 3px solid blue;
      }

      .example1 {
         margin-inline-start: 3%;
      }

      .example2 {
         margin-inline-start: 5%;
      }

      .demo-box {
         background-color: #04bf52;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-start property
   </h2>
   <h4>
      margin-inline-start: 3%
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="example1 props center">
         This box has margin-inline-start: 3% for 
         the inline-start edge of the width of 
         the container.
      </div>
      <div class="demo-box props"> 
      sample box
      </div>
   </div>
   <h4>
      margin-inline-start: 5%
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="example2 props center">
         This box has margin-inline-start: 5% for 
         the inline-start edge of the width of 
         the container.
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
</body>

</html>

帶方向的內聯起始邊距屬性

margin-inline-start 屬性可以與direction 屬性結合使用,該屬性確定文字和內聯元素的水平方向。如果 direction 為 ltr,則inline-start 在左側;如果 direction 為 rtl,則inline-start 在右側。以下示例顯示了這些情況。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         height: 200px;
         width: 490px;
         padding: 20px;
      }

      .container>div {
         width: 150px;
         height: 200px;
         float: left;
         box-sizing: border-box;
      }

      .props {
         text-align: center;
         padding: 10px;
      }

      .center {
         background-color: lightblue;
         border: 3px solid blue;
         margin-inline-start: 15px;
      }

      .demo-box {
         background-color: #04bf52;
      }

      .direction-rtl {
         direction: rtl;
      }

      .direction-ltr {
         direction: ltr;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-start property
   </h2>
   <h4>
      margin-inline-start: 15px; direction: rtl
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="direction-rtl props center">
         This box has margin-inline-start: 15px for 
         inline-start edge with rtl direction.
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
   <h4>
      margin-inline-start: 15px; direction: ltr
   </h4>
   <div class="container">
      <div class="demo-box props">
         sample box
      </div>
      <div class="direction-ltr props center">
         This box has margin-inline-start: 15px for 
         inline-start edge with ltr direction.
      </div>
      <div class="demo-box props"> sample box</div>
   </div>
</body>

</html>

帶書寫模式的內聯起始邊距屬性

margin-inline-start 屬性可以與writing-mode 屬性結合使用,該屬性確定文件中文字流的方向和方向。例如,在垂直模式(如 vertical-rl)中,inline-start 為頂部。以下示例顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         height: 50%;
         width: 50%;
      }

      .container > div {
         writing-mode: vertical-rl;
         block-size: 100%;
         box-sizing: border-box;
      }

      .demo-box {
         background-color: #04bf52;
         inline-size: 20%;
      }

      .main-box {
         background-color: lightblue;
         inline-size: 50%;
         border: solid black 1px;
         margin-inline-start: 30px;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-start property
   </h2>
   <h4>
      margin-inline-start: 30px; 
      writing-mode: vertical-rl
   </h4>
   <div class="container">
      <div class="demo-box">
         sample box
      </div>
      <div class="main-box">
         <p>
            This box has margin-inline-start: 30px for 
            inline-start edge with writing-mode: vertical-rl;
         </p>
      </div>
      <div class="demo-box">
         sample box
      </div>
   </div>

</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
margin-inline-start 69.0 79.0 68.0 14.1 56.0
css_properties_reference.htm
廣告

© . All rights reserved.