margin-block

Baseline Widely available *

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

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

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

尝试一下

margin-block: 10px 20px;
writing-mode: horizontal-tb;
margin-block: 20px 40px;
writing-mode: vertical-rl;
margin-block: 5% 20%;
writing-mode: horizontal-tb;
margin-block: 1rem auto;
writing-mode: vertical-lr;
One
Two
Three
#container {
  width: 300px;
  height: 200px;
  display: flex;
  align-content: flex-start;
  flex-direction: column;
  justify-content: flex-start;
}

.row {
  height: 33.33%;
  display: inline-block;
  border: solid #ce7777 10px;
  background-color: #2b3a55;
  color: #ffffff;
  flex-shrink: 0;
}

#example-element {
  border: solid 10px #ffbf00;
  background-color: #2b3a55;
}

属性构成

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

语法

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

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

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

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

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

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

取值

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

形式定义

初始值该简写所对应的每个属性:
适用元素same as margin
是否是继承属性
Percentagesdepends on layout model
计算值该简写所对应的每个属性:
  • margin-block-start: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto
  • margin-block-end: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto
动画类型a length

形式语法

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

=
|
auto |


=
|


=
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-block: 20px 40px;
  background-color: tan;
}

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

HTML

html

示例文本

示例文本

结果

规范

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

浏览器兼容性

参见