margin-block

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

尝试一下

属性构成

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

语法

css
/* <length> 值 */
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 属性相同。

形式定义

初始值as each of the properties of the shorthand:
适用元素same as margin
是否是继承属性
Percentagesdepends on layout model
计算值as each of the properties of the shorthand:
  • 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
Animation typea length

形式语法

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

示例

设置块首和块末外边距

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
<div>
  <p>示例文本</p>
</div>
<div class="verticalExample">
  <p>示例文本</p>
</div>

结果

规范

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

浏览器兼容性

BCD tables only load in the browser

参见