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 Padding


The scroll-padding property is a CSS feature that defines the optimal viewing region of a scroll container, allowing you to create a padding-like offset from the edges. This is particularly useful for preventing content from being obscured by fixed elements like headers or navigation bars when a user navigates to an anchor link.


Example 1: Basic scroll-padding

/* Sets a 20-pixel scroll padding on all sides of the scroll container */
.scroll-container {
  scroll-padding: 20px;
  height: 200px;
  overflow-y: scroll;
}

Explanation: This code applies a 20-pixel padding on all four sides of the .scroll-container. When you navigate to an element within this container, the scrolling will stop 20 pixels before the edge.


Example 2: scroll-padding-top

/* Adds a 50-pixel offset from the top of the scroll container */
.container {
  scroll-padding-top: 50px;
  height: 250px;
  overflow-y: scroll;
}

Explanation: The scroll-padding-top property is used here to ensure that when a user jumps to an anchor link within .container, there is a 50-pixel space from the top, preventing the target element from being hidden under a fixed header.


Example 3: scroll-padding with different values

/* Applies 20px to top/bottom and 40px to left/right */
.scroll-box {
  scroll-padding: 20px 40px;
  height: 300px;
  overflow-y: auto;
}

Explanation: This shorthand sets the scroll-padding-top and scroll-padding-bottom to 20 pixels, and scroll-padding-left and scroll-padding-right to 40 pixels.


Example 4: Using logical properties

/* Sets scroll padding at the start of the block direction */
.logical-padding {
  scroll-padding-block-start: 3rem;
  height: 200px;
  overflow-y: scroll;
}

Explanation: The scroll-padding-block-start property is a logical property that corresponds to scroll-padding-top in a horizontal writing mode. This ensures predictable behavior regardless of the document's writing direction.


Example 5: Individual side scroll padding

/* Sets individual scroll padding for each side */
.individual-padding {
  scroll-padding-top: 10px;
  scroll-padding-right: 20px;
  scroll-padding-bottom: 15px;
  scroll-padding-left: 25px;
  height: 220px;
  overflow-y: scroll;
}

Explanation: This example demonstrates how to set different scroll padding values for each side of the scroll container individually, offering fine-grained control over the scroll snapping behavior.


Example 6: scroll-padding with smooth scrolling

/* Combines scroll-padding with smooth scrolling for a better user experience */
html {
  scroll-behavior: smooth;
}

.smooth-scroll-container {
  scroll-padding-top: 60px;
  height: 280px;
  overflow-y: scroll;
}

Explanation: Here, scroll-padding-top is used in conjunction with scroll-behavior: smooth on the root element. This combination creates a smooth scrolling effect to anchor links while maintaining the desired top offset.


Example 7: Using calc() for dynamic scroll-padding

/* Dynamically calculates the scroll padding based on the viewport height */
:root {
  --header-height: 70px;
}

.dynamic-padding {
  scroll-padding-top: calc(var(--header-height) + 10px);
  height: 300px;
  overflow-y: scroll;
}

Explanation: This advanced example uses calc() and CSS variables to set the scroll-padding-top. This makes the scroll padding responsive; if the header height changes, the padding will adjust accordingly.