margin-inline

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

* Some parts of this feature may have varying levels of support.

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 属性相同。

形式定义

初始值该简写所对应的每个属性:
适用元素same as margin
是否是继承属性
Percentagesdepends on layout model
计算值该简写所对应的每个属性:
  • 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
动画类型a length

形式语法

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

<margin-top> =
<length-percentage> |
auto |
<anchor-size()>

<length-percentage> =
<length> |
<percentage>

<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

<anchor-name> =
<dashed-ident>

<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline

示例

设置行首和行末外边距

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

浏览器规范性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
margin-inline
anchor-size()
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.

参见