No Login Data Private Local Save

scroll‑padding & scroll‑margin Demo - Online Anchor Position

7
0
0
0

scroll‑padding & scroll‑margin Demo

Interactive visualization of how scroll-padding (container offset) and scroll-margin (element offset) affect anchor positioning & scrollIntoView() behavior.

scroll-padding region scroll-margin region
↥ scroll-padding zone (browser reserves this space)
⬆ Click any nav pill above to trigger scrollIntoView() — observe how padding & margin shift the final position.
scroll-padding-top Container

Space reserved at the top of the scroll container's viewport.

40px
scroll-margin-top Elements

Extra margin above each target element when scrolled into view.

20px
⚡ Quick Presets

Frequently Asked Questions

What is the difference between scroll-padding and scroll-margin?
scroll-padding is set on the scroll container and acts like an "inner offset" for its scrollport — it pushes the snap/scroll target away from the container's edge. scroll-margin is set on child elements and pushes that specific element away from the scrollport edge. Think of scroll-padding as "container-level padding" and scroll-margin as "element-level margin" — both affect scroll alignment, but at different levels.
When should I use scroll-padding instead of scroll-margin?
Use scroll-padding when you have a fixed header or sticky element that obscures the top of the scrollport — it ensures all scroll targets clear that obstruction globally. Use scroll-margin when only specific elements need extra breathing room (e.g., a section with a special callout that shouldn't touch the viewport edge). They can be combined for layered offset control.
Does scroll-padding work with scrollIntoView()?
Yes! Both scroll-padding and scroll-margin are fully respected by element.scrollIntoView() and anchor-link navigation (URL hash). The browser automatically factors them into the final scroll position. This demo uses scrollIntoView({ behavior: 'smooth', block: 'start' }) under the hood — try adjusting the sliders and clicking the nav pills to see it in action.
Can I use different scroll-margin values for different elements?
Absolutely. Each child element can have its own scroll-margin-top value. This is one key advantage over scroll-padding: you can fine-tune individual targets. For example, a hero section might need scroll-margin-top: 80px while a footer anchor only needs scroll-margin-top: 20px.
What about browser compatibility?
Both properties are supported in all modern browsers (Chrome 69+, Firefox 68+, Safari 14.5+, Edge 79+). They are part of the CSS Scroll Snap Module Level 1 specification. For older browsers, they gracefully degrade — the scroll still works, just without the offset.
Can I use logical properties like scroll-padding-inline-start?
Yes. scroll-padding-inline-start, scroll-padding-block-end, and other logical property variants are supported. These are especially useful for internationalization (RTL layouts) and when working with writing modes. The same applies to scroll-margin-inline-* and scroll-margin-block-*.