property

Scroll margin — CSS reference

Sets top, right, bottom and left using the usual one-to-four-value shorthand. Expands (or contracts with negative lengths) the target’s scroll snap area without adding layout margin. Accepts lengths, including negative lengths; percentages are not allowed. The spacing also affects scrollIntoView even without snap enabled.

Formal Syntax & Structure

css syntaxcss
scroll-margin: 12px 16px 20px 24px;

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>scroll-margin</title><style>body{margin:0}#a{padding:8px;background:rgb(232,237,241);color:rgb(0,0,0);box-sizing:border-box}#s{width:120px;height:100px;overflow:auto;position:relative;scroll-behavior:auto}#space{width:500px;height:500px;position:relative}#a{position:absolute;top:200px;left:200px;width:40px;height:40px;padding:0}#a{scroll-margin:12px 16px 20px 24px}</style><body><div id="s" tabindex="0" aria-label="Scrollable spacing example"><div id="space"><div id="a">Target</div></div></div><button id="go">Reveal target</button><script>document.querySelector("#go").onclick=()=>document.querySelector("#a").scrollIntoView({block:"start",inline:"start"});</script></body></html>
CSS Reference