CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Scroll Margin


The CSS scroll-margin property is a shorthand that defines the outset margin of an element which is used as the scroll snap area when scrolling. This allows you to create a visual separation between the edge of the scroll container and the snap position of the element, preventing content from being obscured by elements like fixed headers.


Example 1: scroll-margin-top

/* Target the element that will be snapped to */
.snap-target {
  scroll-margin-top: 50px; /* Creates a 50px top margin for scrolling */
  scroll-snap-align: start;
}

/* A container to enable scroll snapping */
.scroll-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

/* Example of a fixed header that might obscure the content */
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: white;
  border-bottom: 1px solid #ccc;
}

Explanation

This code adds a 50px margin to the top of the .snap-target element's scroll snap area. When you scroll and snap to this element, it will stop 50px below the top of the scroll container, preventing the .fixed-header from covering it.


Example 2: scroll-margin shorthand (all sides)

/* Apply a scroll margin to all four sides of the element */
.snap-element {
  scroll-margin: 2rem; /* Sets a 2rem scroll margin on top, right, bottom, and left */
  scroll-snap-align: center;
}

/* A scroll container for context */
.scroll-container {
  scroll-snap-type: both mandatory;
  overflow: scroll;
  width: 100vw;
  height: 100vh;
}

Explanation

The scroll-margin shorthand with a single value applies the specified outset to all four sides. Here, a 2rem gap is created around the .snap-element for scroll snapping purposes on both the vertical and horizontal axes.


Example 3: scroll-margin shorthand (vertical & horizontal)

/* Apply different scroll margins for vertical and horizontal axes */
.card {
  scroll-margin: 10px 20px; /* 10px for top/bottom, 20px for left/right */
  scroll-snap-align: start;
}

/* A horizontal scroll container */
.horizontal-scroller {
  display: flex;
  overflow-x: scroll;
  scroll-snap-type: x mandatory;
}

Explanation

When using two values, the first value sets scroll-margin-top and scroll-margin-bottom, while the second sets scroll-margin-left and scroll-margin-right. This is useful for creating different spacing for vertical and horizontal scrolling.


Example 4: scroll-margin-left

/* Target an element within a horizontal scroll container */
.list-item {
  scroll-margin-left: 15px; /* Adds a 15px left margin for scroll snapping */
  scroll-snap-align: start;
}

/* A container with horizontal scrolling */
.horizontal-list {
  display: flex;
  overflow-x: scroll;
  scroll-snap-type: x mandatory;
}

Explanation

The scroll-margin-left property specifically defines the outset on the left side of the element. This is ideal for horizontal scrolling interfaces where you need to ensure snapped items don't sit flush against the container's starting edge.


Example 5: Using viewport units (vh)

/* Use viewport height units for a responsive scroll margin */
.section {
  scroll-margin-top: 10vh; /* Sets the top scroll margin to 10% of the viewport height */
  scroll-snap-align: start;
}

.scrolling-wrapper {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

Explanation

Using relative units like vh (viewport height) for scroll-margin allows the space to be responsive to the user's screen size. This ensures the offset is proportional, which is great for accommodating navigation bars or headers of varying heights.


Example 6: scroll-margin with four values

/* Apply a different scroll margin to each side of the element */
.grid-item {
  /* top: 10px, right: 20px, bottom: 30px, left: 40px */
  scroll-margin: 10px 20px 30px 40px;
  scroll-snap-align: center;
}

.grid-container {
  display: grid;
  overflow: scroll;
  scroll-snap-type: both mandatory;
}

Explanation

The four-value shorthand follows the standard CSS box model order: top, right, bottom, and left. This provides granular control over the scroll snap area on each individual side of the element.


Example 7: scroll-margin-bottom

/* Define a scroll margin at the bottom of a snap target */
.footer-snap {
  scroll-margin-bottom: 25px; /* Ensures a 25px space below when snapping */
  scroll-snap-align: end;
}

.full-page-scroller {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

Explanation

scroll-margin-bottom creates an outset at the bottom of the element. When combined with scroll-snap-align: end, this ensures that when you snap to the end of the element, there is still space visible below it, preventing it from touching the edge of the viewport.