CSS Scroll Margin Generator
Set scroll snap margins for better scroll positioning
Presets
Target Element
Scroll Margin
Scroll Behavior
Scroll Padding (Container)
Scroll Snap Options
Actions
Live Preview
Click navigation links to see scroll behavior with current settings
Introduction
Welcome to our documentation. This section provides an overview of the key concepts.
Getting Started
Learn how to set up your development environment and begin building.
Configuration
Detailed configuration options for customizing your setup.
API Reference
Complete API documentation with examples and use cases.
Best Practices
Tips and recommendations for optimal performance and maintainability.
Scroll Margin Comparison
Generated CSS
section {
scroll-margin: 80px 0 0;
}
html {
scroll-behavior: smooth;
}
/* Scroll Margin for Anchored Navigation */
/* Prevents content from being hidden behind fixed headers */
section {
scroll-margin: 80px 0 0;
}
/* Smooth Scrolling */
html {
scroll-behavior: smooth;
}
Code Examples
Basic Scroll Margin
/* Offset for fixed navbar */
section {
scroll-margin-top: 80px;
}
/* Smooth scrolling */
html {
scroll-behavior: smooth;
}With CSS Variables
:root {
--header-height: 60px;
--scroll-offset: 20px;
}
section,
[id] {
scroll-margin-top: calc(
var(--header-height) +
var(--scroll-offset)
);
}Responsive Scroll Margin
section {
scroll-margin-top: 60px;
}
@media (min-width: 768px) {
section {
scroll-margin-top: 80px;
}
}
@media (min-width: 1024px) {
section {
scroll-margin-top: 100px;
}
}All Anchor Targets
/* Target any element with id */
[id] {
scroll-margin-top: 80px;
}
/* Or specific heading levels */
h1, h2, h3, h4, h5, h6 {
scroll-margin-top: 80px;
}
/* Fragment target styling */
:target {
scroll-margin-top: 80px;
animation: highlight 2s;
}Scroll Padding (Container)
/* Alternative: padding on container */
html {
scroll-behavior: smooth;
scroll-padding-top: 80px;
}
/* Useful when you can't modify
individual elements */Scroll Snap with Margin
/* Container */
.scroll-container {
scroll-snap-type: y mandatory;
scroll-padding-top: 80px;
overflow-y: scroll;
}
/* Children */
.scroll-container section {
scroll-snap-align: start;
scroll-margin-top: 80px;
}CSS Properties Reference
| Property | Description | Values |
|---|---|---|
scroll-margin | Margin around scroll snap area | <length> (px, rem, em, vh, etc.) |
scroll-margin-top | Top scroll margin | <length> |
scroll-padding | Padding on scroll container | <length> | auto |
scroll-behavior | Scrolling animation | auto | smooth |
scroll-snap-type | Snap strictness and axis | none | x | y | both + mandatory | proximity |
scroll-snap-align | Snap alignment | start | center | end | none |
Browser Support
| Property | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
scroll-margin | 69+ | 68+ | 14.1+ | 79+ |
scroll-padding | 69+ | 68+ | 14.1+ | 79+ |
scroll-behavior | 61+ | 36+ | 15.4+ | 79+ |
scroll-snap-* | 69+ | 68+ | 11+ | 79+ |
Common Use Cases
Fixed Header Offset
Most common use - prevent anchored content from being hidden behind fixed navigation headers.
Table of Contents Navigation
Documentation sites with sticky TOC that need proper scroll positioning for sections.
Single Page Applications
SPAs with smooth scrolling between sections need proper offset for fixed UI elements.
Skip Links (Accessibility)
Ensure skip-to-content links position content properly below fixed headers.
Best Practices
- Use CSS variables for header height to keep scroll-margin in sync with actual header size
- Add extra padding (10-20px) beyond header height for breathing room
- Apply to all [id] elements to catch all potential anchor targets
- Test with scroll-behavior: smooth to verify smooth scrolling works correctly
- Consider responsive values if header height changes at different breakpoints
- Use scroll-padding on html as an alternative when you cant modify individual elements
- Combine with :target pseudo-class for highlighting active sections