margin-block
Baseline 广泛可用 *
margin-block 是一个 CSS 简写属性,它定义了一个元素的逻辑块起始和结束外边距,这些外边距会根据元素的书写模式、方向性和文本方向映射到物理外边距。
试一试
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;
<section id="default-example">
<div id="container">
<div class="row">One</div>
<div class="row transition-all" id="example-element">Two</div>
<div class="row">Three</div>
</div>
</section>
#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: white;
flex-shrink: 0;
}
#example-element {
border: solid 10px #ffbf00;
background-color: #2b3a55;
}
构成属性
此属性是以下 CSS 属性的简写:
语法
css
/* <length> values */
margin-block: 10px 20px; /* An absolute length */
margin-block: 1em 2em; /* relative to the text size */
margin-block: 5% 2%; /* relative to the nearest block container's width */
margin-block: 10px; /* sets both start and end values */
margin-block: anchor-size(inline);
margin-block: calc(anchor-size(width) / 4) 1em;
/* Keyword values */
margin-block: auto;
/* Global values */
margin-block: inherit;
margin-block: initial;
margin-block: revert;
margin-block: revert-layer;
margin-block: unset;
此属性对应于 margin-top 和 margin-bottom,或者 margin-right 和 margin-left 属性,具体取决于为 writing-mode、direction 和 text-orientation 定义的值。
margin-block 属性可以使用一个或两个值来指定。
- 当指定一个值时,它会将相同的边距应用于起始和结束。
- 当指定两个值时,第一个边距应用于起始,第二个边距应用于结束。
值
margin-block 属性接受与 margin 属性相同的值。
正式定义
| 初始值 | 作为简写中的每个属性 |
|---|---|
| 应用于 | 与 margin 相同 |
| 继承性 | 否 |
| 百分比 | 取决于布局模型 |
| 计算值 | 作为简写中的每个属性
|
| 动画类型 | 一个长度 |
正式语法
margin-block =
<'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-block: 20px 40px;
background-color: tan;
}
.verticalExample {
writing-mode: vertical-rl;
}
HTML
html
<div>
<p>Example text</p>
</div>
<div class="verticalExample">
<p>Example text</p>
</div>
结果
规范
| 规范 |
|---|
| CSS 逻辑属性和值第 1 级 # propdef-margin-block |
浏览器兼容性
加载中…