margin-block-start和margin-top有什么区别?

24

我试图理解这两者之间使用上的核心差异,但是我没有找到一篇详细比较它们的文章或文档。以这里提供的例子为例,假设以下情况:

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>

这个实例和使用margin-top的实例之间的区别非常小(但是很显著)。

规范说明margin-block-start取决于布局模型,而margin-top指的是包含块的宽度。希望有人能用通俗易懂的语言解释一下。


1
此处指的是包含块的宽度。这仅适用于在边距值中使用百分比时。值的百分比几乎总是使用父块的宽度进行计算。因此,如果您的margin-top10%,并且您增加了包含块的宽度,则相对于包含块的计算宽度,它将为10% - disinfor
2个回答

43

根据官方1规范,您可以阅读到:

这些属性对应于margin-top、margin-bottom、margin-left和margin-right属性。映射取决于元素的书写模式、方向和文本方向。

默认情况下,将具有以下映射:

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-*-*属性都将根据writing-modedirectiontext-orientation的值映射到一个margin-*属性上。

您可以尝试使用不同的值来查看不同的情况。


你的例子有点复杂,因为它涉及到了边距折叠和默认应用于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: 你正在使用的链接是 MDN 页面,这是一个很好的参考资料,但并非官方规范。


-1

margin-block-start CSS 属性定义了元素的逻辑块起始边距,根据元素的书写模式、方向性和文本方向映射到物理边距。

请运行代码段以查看其行为:

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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接