CKTIS / CSS
CSS-eigenschap `margin-top`
`margin-top` stelt de fysieke bovenmarge in; bij verticaal schrijven verandert de logische naam met de richting, maar de fysieke zijde blijft boven.
Versie: CSS modules; captured CSSWG editor index includes draft and obsolete features requiring review
Syntax
<length-percentage> | auto | <anchor-size()>margin-top stelt de fysieke bovenmarge in; bij verticaal schrijven verandert de logische naam met de richting, maar de fysieke zijde blijft boven.
Het voorbeeld meet zowel bij verticaal LTR, waar dit inline-start is, als bij RTL, waar dit inline-end is, 12 px bovenruimte. Afzonderlijk geeft 10% van een inline-grootte van 200 px 20 px, vallen aangrenzende marges van 30 px en 20 px samen tot 30 px en veroorzaakt -6px overlap.
Grammatica: <length-percentage> | auto | <anchor-size()>.
Samenvallen volgt de logische blokas en niet de fysieke verticale richting: het paar in het voorbeeld ligt bij standaard horizontaal schrijven op de blokas en kan samenvallen, maar bij verticaal schrijven is de fysieke bovenkant een inline-zijde en valt margin-top daar niet samen. Marges van flex- en griditems vallen evenmin samen; een percentage gebruikt de inline-grootte van het omvattende blok, en negatieve waarden en auto zijn toegestaan.
Voorbeelden
<!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>Resultaat
ltr-top=12 start/end=12px/0px; rtl-top=12 start/end=0px/12px; percent-top=20; collapsed-gap=30; negative-gap=-6
Het voorbeeld is lokaal uitgevoerd in een geïsoleerde Chrome-sessie zonder netwerk; het resultaat komt uit de CSS-API van de browser.