margin-block-start 和 margin-top 有什么区别?
What is the difference between margin-block-start and margin-top?
试图了解两者在用法上的核心区别,但我找不到详细说明此类比较的文章或文档。以提供的示例 here 为例:
div {
background-color: yellow;
width: 120px;
height: 120px;
}
.exampleText {
writing-mode: vertical-lr;
margin-block-start: 20px;
background-color: #c8c800;
}
<div>
<p class="exampleText">Example text</p>
</div>
this instance, and one in which margin-top
is used 之间的差异非常小(但是可见)。
specifications 声明 margin-block-start
取决于布局模型 而 margin-top
指的是包含的宽度块。如果有人能通俗地解释一下,我会很高兴的。
来自官方1 specification您可以阅读:
These properties correspond to the margin-top, margin-bottom, margin-left, and margin-right properties. The mapping depends on the element’s writing-mode, direction, and text-orientation.
默认情况下,您将拥有以下映射:
margin-block-start = margin-top
margin-block-end = margin-bottom
margin-inline-start = margin-left
margin-inline-end = margin-right
示例:
.margin {
margin-top:50px;
margin-bottom:10px;
margin-left:100px;
margin-right:200px;
}
.margin-alt {
margin-block-start:50px;
margin-block-end:10px;
margin-inline-start:100px;
margin-inline-end:200px;
}
div {
border:2px solid;
padding:20px;
}
<div class="margin">
A
</div>
<hr>
<div class="margin-alt">
A
</div>
现在如果我们改变书写模式,你会看到我们会有不同的映射。
.margin {
margin-top:50px;
margin-bottom:10px;
margin-left:100px;
margin-right:200px;
}
.margin-alt {
margin-block-start:50px;
margin-block-end:10px;
margin-inline-start:100px;
margin-inline-end:200px;
}
div {
border:2px solid;
padding:20px;
writing-mode: vertical-lr;
}
<div class="margin">
A
</div>
<hr>
<div class="margin-alt">
A
</div>
<div class="margin-alt" style="writing-mode: vertical-rl;">
A
</div>
在上面,您会注意到在使用 vertical-lr
时映射等于
margin-block-start = margin-left
margin-block-end = margin-right
margin-inline-start = margin-top
margin-inline-end = margin-bottom
并且在使用 vertical-rl
时
margin-block-start = margin-right
margin-block-end = margin-left
margin-inline-start = margin-top
margin-inline-end = margin-bottom
我不会详细说明所有情况,但基本上每个 margin-*-*
属性 都会根据 [=23= 的值映射到 margin-*
属性 ]、direction
和text-orientation
。
您可以使用不同的值来查看不同的情况。
你的例子有点复杂,因为你有边距折叠和默认边距应用到 p
所以很难理解。
这是一个更好的:
div:not([class]) {
background-color: yellow;
width: 120px;
height: 120px;
border:1px solid;
}
.exampleText {
writing-mode: vertical-lr;
margin-block-start: 20px; /* we will end with a margin-left */
background-color: #c8c800;
}
.exampleText2 {
writing-mode: vertical-lr;
margin-top: 20px; /* this will remain a margin-top */
background-color: #c8c800;
}
<div>
<div class="exampleText">Example text</div>
</div>
<div>
<div class="exampleText2">Example text</div>
</div>
1: 您使用的 link 是 MDN 页面,这是一个很好的参考,但不是官方规范。
The margin-block-start CSS property defines the logical block start
margin of an element, which maps to a physical margin depending on the
element's writing mode, directionality, and text orientation.
请 运行 片段以查看实际行为:
document.addEventListener('DOMContentLoaded', event => {
const choice = document.querySelectorAll('.choice');
const target = document.querySelectorAll('.row-target')[0];
choice.forEach(el => el.addEventListener('click', event => {
choice.forEach(el => el.classList.remove('selected'));
event.target.classList.add('selected');
target.setAttribute('style', event.target.innerText);
}));
})
* {
box-sizing: border-box;
}
#container {
width: 300px;
height: 200px;
display: flex;
align-content: flex-start;
flex-direction: column;
justify-content: flex-start;
margin-left: 20px;
}
.row {
height: 33.33%;
display: inline-block;
border: solid #5b6dcd 10px;
background-color: rgba(229,232,252,.6);
flex-shrink: 0;
}
.row-target {
border: solid 10px #ffc129;
background-color: rgba(255,244,219,.6);
}
.transition-all {
transition: all .3s ease-in;
}
.choice {
background-color: #fff;
display: flex;
align-items: center;
flex-grow: 1;
position: relative;
z-index: 1;
display: block;
margin: .2em 0;
width: 100%;
border: 2px solid #d6dee0;
border-left: 5px solid #d6dee0;
font-size: 14px;
cursor: pointer;
transition: background-color .2s ease-out,border .2s ease-out;
}
.choice.selected:before {
opacity: 1;
right: -1.5em;
}
.choice:before {
content: '';
position: absolute;
top: 50%;
right: -10px;
z-index: 1;
opacity: 0;
transition: all .2s ease-out;
transform: translateY(-50%);
border-left: 10px solid #1b76c4;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
}
.selected {
border-color: #1b76c4;
border-left-color: #1b76c4;
box-shadow: inset 0 2px 2px -2px rgb(0 0 0 / 20%);
transition: height .5s;
cursor: text;
}
<table>
<tr>
<td>
<div class="choice selected">
margin-block-start: 20px; <br>
writing-mode: horizontal-tb;
</div>
<div class="choice">
margin-block-start: 20px; <br>
writing-mode: vertical-rl;
</div>
<div class="choice">
margin-block-start: 20%; <br>
writing-mode: horizontal-tb;
</div>
<div class="choice">
margin-block-start: auto; <br>
writing-mode: vertical-lr;
</div>
</td>
<td valign="top">
<div id="container">
<div class="row">One</div>
<div class="row row-target transition-all" id="example-element" style="margin-block-start: 20px; writing-mode: horizontal-tb;">Two</div>
<div class="row">Three</div>
</div>
</td></tr>
</table>
试图了解两者在用法上的核心区别,但我找不到详细说明此类比较的文章或文档。以提供的示例 here 为例:
div {
background-color: yellow;
width: 120px;
height: 120px;
}
.exampleText {
writing-mode: vertical-lr;
margin-block-start: 20px;
background-color: #c8c800;
}
<div>
<p class="exampleText">Example text</p>
</div>
this instance, and one in which margin-top
is used 之间的差异非常小(但是可见)。
specifications 声明 margin-block-start
取决于布局模型 而 margin-top
指的是包含的宽度块。如果有人能通俗地解释一下,我会很高兴的。
来自官方1 specification您可以阅读:
These properties correspond to the margin-top, margin-bottom, margin-left, and margin-right properties. The mapping depends on the element’s writing-mode, direction, and text-orientation.
默认情况下,您将拥有以下映射:
margin-block-start = margin-top
margin-block-end = margin-bottom
margin-inline-start = margin-left
margin-inline-end = margin-right
示例:
.margin {
margin-top:50px;
margin-bottom:10px;
margin-left:100px;
margin-right:200px;
}
.margin-alt {
margin-block-start:50px;
margin-block-end:10px;
margin-inline-start:100px;
margin-inline-end:200px;
}
div {
border:2px solid;
padding:20px;
}
<div class="margin">
A
</div>
<hr>
<div class="margin-alt">
A
</div>
现在如果我们改变书写模式,你会看到我们会有不同的映射。
.margin {
margin-top:50px;
margin-bottom:10px;
margin-left:100px;
margin-right:200px;
}
.margin-alt {
margin-block-start:50px;
margin-block-end:10px;
margin-inline-start:100px;
margin-inline-end:200px;
}
div {
border:2px solid;
padding:20px;
writing-mode: vertical-lr;
}
<div class="margin">
A
</div>
<hr>
<div class="margin-alt">
A
</div>
<div class="margin-alt" style="writing-mode: vertical-rl;">
A
</div>
在上面,您会注意到在使用 vertical-lr
时映射等于
margin-block-start = margin-left
margin-block-end = margin-right
margin-inline-start = margin-top
margin-inline-end = margin-bottom
并且在使用 vertical-rl
margin-block-start = margin-right
margin-block-end = margin-left
margin-inline-start = margin-top
margin-inline-end = margin-bottom
我不会详细说明所有情况,但基本上每个 margin-*-*
属性 都会根据 [=23= 的值映射到 margin-*
属性 ]、direction
和text-orientation
。
您可以使用不同的值来查看不同的情况。
你的例子有点复杂,因为你有边距折叠和默认边距应用到 p
所以很难理解。
这是一个更好的:
div:not([class]) {
background-color: yellow;
width: 120px;
height: 120px;
border:1px solid;
}
.exampleText {
writing-mode: vertical-lr;
margin-block-start: 20px; /* we will end with a margin-left */
background-color: #c8c800;
}
.exampleText2 {
writing-mode: vertical-lr;
margin-top: 20px; /* this will remain a margin-top */
background-color: #c8c800;
}
<div>
<div class="exampleText">Example text</div>
</div>
<div>
<div class="exampleText2">Example text</div>
</div>
1: 您使用的 link 是 MDN 页面,这是一个很好的参考,但不是官方规范。
The margin-block-start CSS property defines the logical block start margin of an element, which maps to a physical margin depending on the element's writing mode, directionality, and text orientation.
请 运行 片段以查看实际行为:
document.addEventListener('DOMContentLoaded', event => {
const choice = document.querySelectorAll('.choice');
const target = document.querySelectorAll('.row-target')[0];
choice.forEach(el => el.addEventListener('click', event => {
choice.forEach(el => el.classList.remove('selected'));
event.target.classList.add('selected');
target.setAttribute('style', event.target.innerText);
}));
})
* {
box-sizing: border-box;
}
#container {
width: 300px;
height: 200px;
display: flex;
align-content: flex-start;
flex-direction: column;
justify-content: flex-start;
margin-left: 20px;
}
.row {
height: 33.33%;
display: inline-block;
border: solid #5b6dcd 10px;
background-color: rgba(229,232,252,.6);
flex-shrink: 0;
}
.row-target {
border: solid 10px #ffc129;
background-color: rgba(255,244,219,.6);
}
.transition-all {
transition: all .3s ease-in;
}
.choice {
background-color: #fff;
display: flex;
align-items: center;
flex-grow: 1;
position: relative;
z-index: 1;
display: block;
margin: .2em 0;
width: 100%;
border: 2px solid #d6dee0;
border-left: 5px solid #d6dee0;
font-size: 14px;
cursor: pointer;
transition: background-color .2s ease-out,border .2s ease-out;
}
.choice.selected:before {
opacity: 1;
right: -1.5em;
}
.choice:before {
content: '';
position: absolute;
top: 50%;
right: -10px;
z-index: 1;
opacity: 0;
transition: all .2s ease-out;
transform: translateY(-50%);
border-left: 10px solid #1b76c4;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
}
.selected {
border-color: #1b76c4;
border-left-color: #1b76c4;
box-shadow: inset 0 2px 2px -2px rgb(0 0 0 / 20%);
transition: height .5s;
cursor: text;
}
<table>
<tr>
<td>
<div class="choice selected">
margin-block-start: 20px; <br>
writing-mode: horizontal-tb;
</div>
<div class="choice">
margin-block-start: 20px; <br>
writing-mode: vertical-rl;
</div>
<div class="choice">
margin-block-start: 20%; <br>
writing-mode: horizontal-tb;
</div>
<div class="choice">
margin-block-start: auto; <br>
writing-mode: vertical-lr;
</div>
</td>
<td valign="top">
<div id="container">
<div class="row">One</div>
<div class="row row-target transition-all" id="example-element" style="margin-block-start: 20px; writing-mode: horizontal-tb;">Two</div>
<div class="row">Three</div>
</div>
</td></tr>
</table>