Spec-Zone.ru › CSS

`scroll-padding` CSS property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It's been available across browsers since January 2020.

The scroll-padding CSS shorthand property sets scroll padding on all sides of an element at once. It specifies offsets that define the optimal viewing region of a scrollport within a scroll container.

Try it

scroll-padding: 0;
scroll-padding: 20px;
scroll-padding: 20%;
<section class="default-example" id="default-example">
  <div class="scroller" id="example-element">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </div>
  <div class="info">Scroll »</div>
</section>
.default-example .info {
  inline-size: 100%;
  padding: 0.5em 0;
  font-size: 90%;
  writing-mode: vertical-rl;
}

.scroller {
  text-align: left;
  height: 250px;
  width: 270px;
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  border: 1px solid black;
  scroll-snap-type: y mandatory;
}

.scroller > div {
  flex: 0 0 250px;
  background-color: rebeccapurple;
  color: white;
  font-size: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  scroll-snap-align: start;
}

.scroller > div:nth-child(even) {
  background-color: white;
  color: rebeccapurple;
}

Constituent properties

This property is a shorthand for the following CSS properties:

  • scroll-padding-bottom
  • scroll-padding-left
  • scroll-padding-right
  • scroll-padding-top

Syntax

/* Keyword values */
scroll-padding: auto;

/* <length> values */
scroll-padding: 10px;
scroll-padding: 1em 0.5em 1em 1em;
scroll-padding: 10%;

/* Global values */
scroll-padding: inherit;
scroll-padding: initial;
scroll-padding: revert;
scroll-padding: revert-layer;
scroll-padding: unset;

Values

<length-percentage>
An inwards offset from the corresponding edge of the scrollport, as a valid <length> or a <percentage>.
auto
The offset is determined by the user agent. This will generally be 0px, but the user agent is free to detect and do something else if a non-zero value is more appropriate.

Description

The scroll-padding property is a shorthand that sets scroll-padding-top, scroll-padding-right, scroll-padding-bottom, and scroll-padding-left, in that order, setting the top, right, bottom, and left scroll padding of a scroll container, respectively.

Useful when creating scroll-snap containers, the scroll-padding property enables defining offsets for the optimal viewing region of the scrollport: the region used as the target region for placing elements in view of the user. This allows you to create insets in the scrollport to make room for objects that might obscure the content, such as fixed-positioned toolbars or sidebars, or to put more breathing room between a targeted element and the edges of the scrollport.

While defined in the CSS scroll snap module, this property applies to all scroll containers, no matter the value of the scroll-snap-type property.

Formal definition

Initial value as each of the properties of the shorthand:
  • scroll-padding-bottom
  • scroll-padding-left
  • scroll-padding-right
  • scroll-padding-top
Applies to scroll containers
Inherited no
Percentages relative to the scroll container's scrollport
Computed value as each of the properties of the shorthand:
  • scroll-padding-bottom
  • scroll-padding-left
  • scroll-padding-right
  • scroll-padding-top
Animation type by computed value type

Formal syntax

scroll-padding = 
  [ auto | <length-percentage> ]{1,4}

<length-percentage> = 
  <length> |
  <percentage>

Examples

Preventing content from being hidden by a fixed header

A common use case for scroll-padding is to prevent scrolling content from being hidden by a fixed-position header. This example demonstrates how to use scroll-padding for this purpose.

HTML

The header contains links to three sections on the page.

<header>
  <nav>
    <a href="#section-1">Section 1</a>
    <a href="#section-2">Section 2</a>
    <a href="#section-3">Section 3</a>
  </nav>
</header>
<main>
  <section id="section-1">
    <h2>Section 1</h2>
    <p>Use the links in the header to jump to another section.</p>
  </section>
  <section id="section-2">
    <h2>Section 2</h2>
    <p>This heading remains visible below the fixed header.</p>
  </section>
  <section id="section-3">
    <h2>Section 3</h2>
    <p>This heading also remains visible below the fixed header.</p>
  </section>
</main>

CSS

header {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #eeeeee;
}

nav {
  display: flex;
  gap: 1rem;
}

section {
  box-sizing: border-box;
  padding: 1rem;
  min-height: 60vh;
}

h2 {
  margin-top: 0;
}

The header is fixed to the top of the viewport and is 60px tall. We set scroll-padding on the root element to reserve 60px at the top of the viewport when scrolling to a section, with no offset on the other sides.

html {
  --navbar-height: 60px;

  scroll-padding: var(--navbar-height) 0 0;
  scroll-behavior: smooth;
}

header {
  position: fixed;
  top: 0;
  height: var(--navbar-height);
  width: 100%;
}

We add padding to the main content so the first section starts below the header on initial load.

main {
  padding-top: var(--navbar-height);
}

Result

Click the links in the header to jump between sections. Each section appears below the header, keeping its heading visible. Open this example in the playground, remove the scroll-padding line, and click the links again to see the header obscure the headings.

Specifications

Specification
CSS Scroll Snap Module Level 1
# scroll-padding

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS
scroll-padding
69
79
68
56
14.1
11–14.1Scroll padding is not applied for scrolls to fragment target or scrollIntoView(), see bug 179379.
69
68
48
14.5
11–14.5Scroll padding is not applied for scrolls to fragment target or scrollIntoView(), see bug 179379.
10.0
69
14.5
11–14.5Scroll padding is not applied for scrolls to fragment target or scrollIntoView(), see bug 179379.
auto
69
79
68
56
11
69
68
48
11
10.0
69
11

See also

  • scroll-snap-type
  • Basic concepts of scroll-snap
  • CSS scroll snap module
  • CSS overflow module
  • CSS scroll-driven animations module

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scroll-padding

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API