CSS - margin-inline-end 属性



CSS margin-inline-end 属性设置元素的inline-end边缘的外边距。书写模式方向属性定义了内联方向。

语法

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

属性值

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

CSS Margin Inline End 属性示例

以下示例解释了具有不同值的margin-inline-end属性。

具有 Auto 值的 Margin Inline End 属性

要允许浏览器根据可用空间自动计算元素的inline-end边缘的外边距,我们使用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-end: auto;
      }

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

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

</html>

具有长度值的 Margin Inline End 属性

要设置元素的inline-end边缘的外边距,我们可以使用长度单位(例如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-end: 20px;
      }

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

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

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

</html>

具有百分比值的 Margin Inline End 属性

要设置元素的inline-end边缘的外边距,我们可以使用百分比值(例如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-end: 3%;
      }

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

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

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

</html>

具有方向的 Margin Inline End 属性

margin-inline-end属性可以与direction属性结合使用,该属性确定文本和内联元素的水平方向。如果direction为ltr,则inline-end位于右侧;如果direction为rtl,则inline-end位于左侧。这些在以下示例中显示。

示例

<!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-end: 15px;
      }

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

      .direction-rtl {
         direction: rtl;
      }

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

<body>
   <h2>
      CSS margin-inline-end property
   </h2>
   <h4>
      margin-inline-end: 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-end: 15px for 
         inline-end edge with rtl direction.
      </div>
      <div class="demo-box props"> 
         sample box
      </div>
   </div>
   <h4>
      margin-inline-end: 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-end: 15px for 
         inline-end edge with ltr direction.
      </div>
      <div class="demo-box props"> sample box</div>
   </div>
</body>

</html>

具有书写模式的 Margin Inline End 属性

margin-inline-end属性可以与writing-mode属性结合使用,该属性确定文档中文本流的方向和方向。例如,在垂直模式(如vertical-rl)中,inline-end位于底部。这在以下示例中显示。

示例

<!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-end: 30px;
      }
   </style>
</head>

<body>
   <h2>
      CSS margin-inline-end property
   </h2>
   <h4>
      margin-inline-end: 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-end: 30px for 
            inline-end 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-end 87.0 87.0 41.0 12.1 73.0
css_properties_reference.htm
广告