Trying to understand the core difference in usage between the two, but I fail to find an article or doc that details such a comparison. Taking the example provided here, assuming the following:
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>
The difference between this instance, and one in which margin-top is used, is quite small (however visible).
The specifications state that margin-block-start depends on layout model while margin-top refer to the width of the containing block. Would love it if someone could explain it in layman's term.
From the official1 specification you can read:
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.
By default, you will have the following mapping:
margin-block-start = margin-top
margin-block-end = margin-bottom
margin-inline-start = margin-left
margin-inline-end = margin-right
Example:
.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>
Now if we change the writing mode, you will see that we will have a different mapping.
.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>
In the above, you will notice when using vertical-lr a mapping equal to
margin-block-start = margin-left
margin-block-end = margin-right
margin-inline-start = margin-top
margin-inline-end = margin-bottom
and when using vertical-rl
margin-block-start = margin-right
margin-block-end = margin-left
margin-inline-start = margin-top
margin-inline-end = margin-bottom
I will not detail all the cases, but basically each margin-*-* property will be mapped to a margin-* property based on the values of writing-mode, direction, and text-orientation.
You can play with the different values to see the different cases.
Your examples are a bit complex because you are having margin-collapsing and the default margin applied to p so it's difficult to understand.
Here is a better one:
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: the link you are using is the MDN page which is a good reference but not the official specification.
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.
Please run the snippet to see the behavior in action:
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>