margin-inline

CSS 简写属性 margin-inline 定义了元素的逻辑行首和行末外边距,并根据元素的书写模式、行内方向和文本朝向对应至实体外边距。

尝试一下

属性构成

此属性为下列 CSS 属性的简写属性:

语法

css
/* <length> 值 */
margin-inline: 10px 20px; /* 绝对长度 */
margin-inline: 1em 2em; /* 相对于文本尺寸 */
margin-inline: 5% 2%; /* 相对于最近区块容器的宽度 */
margin-inline: 10px; /* 同时设置行首和行末值 */

/* 关键词值 */
margin-inline: auto;

/* 全局值 */
margin-inline: inherit;
margin-inline: initial;
margin-inline: revert;
margin-inline: revert-layer;
margin-inline: unset;

根据 writing-modedirectiontext-orientation 所定义的值,此属性对应于 margin-topmargin-bottom,或者 margin-rightmargin-left 属性。

margin-inline 属性可用一个或两个值指定。

  • 一个值指定时,行首和行末应用同样的外边距。
  • 两个值指定时,第一个外边距应用于行首,第二个应用于行末

取值

margin-inline 属性的取值与 margin 属性相同。

形式定义

初始值as each of the properties of the shorthand:
适用元素same as margin
是否是继承属性
Percentagesdepends on layout model
计算值as each of the properties of the shorthand:
  • margin-inline-start: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto
  • margin-inline-end: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto
Animation typea length

形式语法

margin-inline = 
<'margin-top'>{1,2}

示例

设置行首和行末外边距

CSS

css
div {
  background-color: yellow;
  width: 120px;
  height: auto;
  border: 1px solid green;
}

p {
  margin: 0;
  margin-inline: 20px 40px;
  background-color: tan;
}

.verticalExample {
  writing-mode: vertical-rl;
}

HTML

html
<div>
  <p>示例文本</p>
</div>
<div class="verticalExample">
  <p>示例文本</p>
</div>

结果

规范

Specification
CSS Logical Properties and Values Level 1
# propdef-margin-inline

浏览器规范性

BCD tables only load in the browser

参见