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 Snap Align


The scroll-snap-align property is a crucial part of the CSS Scroll Snap module, designed to create well-controlled scrolling experiences. This property specifies the alignment of the snap area of an element within the container's snapport (the visible area) when the scrolling action ceases.


Example 1: scroll-snap-align: start

/* CSS for the parent container */
.snap-container {
  width: 100%;
  height: 300px;
  overflow-x: scroll; /* Enable horizontal scrolling */
  display: flex; /* Lay out children in a row */
  scroll-snap-type: x mandatory; /* Enforce snapping on the horizontal axis */
}

/* CSS for the child items */
.snap-item {
  flex: 0 0 100%; /* Each item takes the full width of the container */
  height: 300px;
  scroll-snap-align: start; /* Align the start of the item with the start of the container */
}

Explanation

This code creates a horizontal scrolling container where each child item will snap into place. The scroll-snap-align: start; rule ensures that the left edge of the scrolling item will align precisely with the left edge of the visible scroll container when the user stops scrolling.


Example 2: scroll-snap-align: center

/* CSS for the parent container */
.gallery-container {
  width: 100%;
  height: 400px;
  overflow-x: scroll;
  display: flex;
  scroll-snap-type: x mandatory;
}

/* CSS for the gallery images */
.gallery-image {
  flex: 0 0 80%; /* Each image takes up 80% of the container width */
  margin: 0 10%; /* Center the image with some spacing */
  height: 400px;
  scroll-snap-align: center; /* Align the center of the image with the center of the container */
}

Explanation

In this example, as the user scrolls through a gallery of images, each image will snap to the center of the container. The scroll-snap-align: center; is ideal for focusing on the main content of each section.


Example 3: scroll-snap-align: end

/* CSS for the vertical scrolling container */
.vertical-snapper {
  height: 500px;
  overflow-y: scroll; /* Enable vertical scrolling */
  scroll-snap-type: y mandatory; /* Enforce snapping on the vertical axis */
}

/* CSS for the sections to snap */
.snap-section {
  height: 500px;
  scroll-snap-align: end; /* Align the end of the section with the end of the container */
}

Explanation

This code demonstrates a full-page vertical scrolling effect. scroll-snap-align: end; aligns the bottom edge of each section with the bottom edge of the viewport as the user scrolls down.


Example 4: scroll-snap-align: none

/* CSS for the mixed behavior container */
.mixed-snap-container {
  height: 400px;
  overflow-y: scroll;
  scroll-snap-type: y proximity; /* Looser snapping on the vertical axis */
}

/* CSS for the snap and non-snap items */
.snap-item-regular {
  height: 400px;
  scroll-snap-align: center;
}

.no-snap-item {
  height: 200px;
  scroll-snap-align: none; /* This item will not have a snap point */
}

Explanation

The scroll-snap-align: none; value is used to explicitly prevent an element from being a snap target. In this container, "snap-item-regular" elements will snap to the center, while the "no-snap-item" will not, allowing for more complex scroll interactions.


Example 5: Two-axis alignment (start end)

/* CSS for a 2D grid container */
.grid-container {
  width: 100%;
  height: 600px;
  overflow: scroll; /* Enable both horizontal and vertical scrolling */
  display: grid;
  grid-template-columns: repeat(3, 100%);
  scroll-snap-type: both mandatory;
}

/* CSS for grid items */
.grid-item {
  scroll-snap-align: start end; /* Align to start on x-axis, end on y-axis */
}

Explanation

This example shows a two-dimensional grid where items snap. scroll-snap-align: start end; sets the alignment for the block (vertical) and inline (horizontal) axes respectively, meaning the item will snap to the top of the container vertically and the right edge horizontally.


Example 6: Combining with scroll-padding

/* CSS for the container with padding */
.padded-container {
  width: 100%;
  height: 350px;
  overflow-x: scroll;
  display: flex;
  scroll-snap-type: x mandatory;
  scroll-padding: 0 50px; /* Add 50px padding to the left and right of the snapport */
}

/* CSS for the snapped items */
.padded-item {
  flex: 0 0 90%;
  height: 350px;
  scroll-snap-align: center; /* Center the item within the padded area */
}

Explanation

Here, scroll-padding is used to create an offset within the scroll container. scroll-snap-align: center; will then align the snap item to the center of the snapport, respecting the defined padding for a visually balanced layout.


Example 7: Aligning Different Sized Items

/* CSS for the varied content container */
.varied-container {
  width: 100%;
  height: 450px;
  overflow-x: scroll;
  display: flex;
  scroll-snap-type: x mandatory;
}

/* CSS for different sized items */
.item-small {
  flex: 0 0 60%;
  height: 450px;
  scroll-snap-align: center;
}

.item-large {
  flex: 0 0 95%;
  height: 450px;
  scroll-snap-align: center; /* Both item types will center align */
}

Explanation

This demonstrates that scroll-snap-align works effectively even with items of different sizes within the same scroll container. Regardless of their width, each item with scroll-snap-align: center; will snap to the horizontal center of the viewport.