CKTIS / CSS
CSS property `margin-top`
`margin-top` sets the physical top margin; in vertical writing its logical name changes with direction, but its physical side remains the top.
Version: CSS modules; captured CSSWG editor index includes draft and obsolete features requiring review
Syntax
<length-percentage> | auto | <anchor-size()>margin-top sets the physical top margin; in vertical writing its logical name changes with direction, but its physical side remains the top.
The example measures a 12px top gap in both vertical LTR, where it is inline-start, and RTL, where it is inline-end. Separately, 10% of a 200px inline size produces 20px, adjacent 30px and 20px margins collapse to 30px, and -6px produces overlap.
Grammar: <length-percentage> | auto | <anchor-size()>.
Collapsing follows the logical block axis, not physical verticality: the example’s pair is on the block axis in default horizontal writing and may collapse, but in vertical writing physical top is an inline side and margin-top does not collapse there. Flex and grid item margins also do not collapse; a percentage uses the containing block’s inline size, and negative values and auto are allowed.
Examples
<!doctype html><meta charset="utf-8"><div id="vl"><i></i></div><div id="vr"><i></i></div><div id="pct"><i></i></div><div id="collapse"><i></i><i></i></div><div id="neg"><i></i><i></i></div><pre id="result"></pre><script>
const vertical=(box,dir)=>{box.style.cssText='position:relative;writing-mode:vertical-rl;direction:'+dir+';width:80px;height:80px';box.firstElementChild.style.cssText='position:absolute;top:0;right:0;width:20px;height:20px;margin-top:12px'};vertical(vl,'ltr');vertical(vr,'rtl');pct.style.cssText='position:relative;width:200px;height:60px';pct.firstElementChild.style.cssText='position:absolute;top:0;left:0;width:20px;height:20px;margin-top:10%';collapse.style.cssText='display:flow-root;width:100px';collapse.children[0].style.cssText='display:block;height:10px;margin-bottom:30px';collapse.children[1].style.cssText='display:block;height:10px;margin-top:20px';neg.style.cssText='display:flex;flex-direction:column;width:100px';neg.children[0].style.cssText='height:20px';neg.children[1].style.cssText='height:20px;margin-top:-6px';const gapTop=box=>Math.round(box.firstElementChild.getBoundingClientRect().top-box.getBoundingClientRect().top),ls=getComputedStyle(vl.firstElementChild),rs=getComputedStyle(vr.firstElementChild),[ca,cb]=[...collapse.children].map(x=>x.getBoundingClientRect()),[na,nb]=[...neg.children].map(x=>x.getBoundingClientRect());result.textContent='ltr-top='+gapTop(vl)+' start/end='+ls.marginInlineStart+'/'+ls.marginInlineEnd+'; rtl-top='+gapTop(vr)+' start/end='+rs.marginInlineStart+'/'+rs.marginInlineEnd+'; percent-top='+gapTop(pct)+'; collapsed-gap='+Math.round(cb.top-ca.bottom)+'; negative-gap='+Math.round(nb.top-na.bottom);
</script>Result
ltr-top=12 start/end=12px/0px; rtl-top=12 start/end=0px/12px; percent-top=20; collapsed-gap=30; negative-gap=-6
The example was executed locally in an isolated, network-free Chrome session; the result comes from the browser CSS API.