Tillitsdone
down Scroll to discover

CSS scroll-padding-block-start Enhance Scroll Containers

CSS scroll-padding-block-start enhances scroll containers by defining start padding.

Use auto, length, percentage, and global values to control spacing.
thumbnail

Introduction

The scroll-padding-block-start property in CSS is a handy tool for web developers and designers. It helps ensure that content within a scrollable area is displayed properly, especially when dealing with fixed elements like toolbars or sidebars.

Introduced in July 2019, this property is widely supported across many devices and browser versions. It allows you to control the spacing at the beginning of scrollable content, making sure important elements are not hidden.

This property is great for enhancing the user experience by creating a smoother and more intuitive scrolling experience. Whether you’re a seasoned developer or just starting out, understanding and using scroll-padding-block-start can greatly improve your web projects.

Specification

The scroll-padding-block-start property is defined in the CSS Scroll Snap Module Level 1. This module introduces properties that control the snapping behavior of scrollable areas, enhancing the user experience.

The specific reference for scroll-padding-block-start can be found in the “padding-longhands-logical” section of the CSS Scroll Snap Module Level 1 specification. Understanding this specification is crucial for using the property effectively.

For more detailed information, you can refer to the official documentation:

Description

The scroll-padding-block-start property in CSS defines the offset from the start edge of the optimal viewing region within a scroll container. It’s particularly useful for ensuring that certain areas of the scroll container are not obscured by fixed elements like toolbars or sidebars.

By setting this property, you can control the spacing at the beginning of the scrollable content, adding extra room to make sure the content is displayed well. The property can be set using various values, including keywords, lengths, and percentages, providing flexibility depending on your design needs.

For example, if you have a fixed toolbar at the top of your page, you can use scroll-padding-block-start to ensure that the content below the toolbar is not hidden when the user scrolls. This creates a more seamless and intuitive browsing experience.

This property is especially beneficial in web design and development, as it helps to maintain the visual integrity of your layouts. Whether you’re working on a simple webpage or a complex application, this property can significantly improve the aesthetics and usability of your project.

Syntax

The scroll-padding-block-start property can be defined using various syntax options. Here are the different ways to set this property:

/* Keyword values */
scroll-padding-block-start: auto;
/* <length> values */
scroll-padding-block-start: 10px;
scroll-padding-block-start: 1em;
scroll-padding-block-start: 10%;
/* Global values */
scroll-padding-block-start: inherit;
scroll-padding-block-start: initial;
scroll-padding-block-start: revert;
scroll-padding-block-start: revert-layer;
scroll-padding-block-start: unset;

Values

The scroll-padding-block-start property accepts the following values:

  • <length-percentage>: An inwards offset from the block start edge of the scrollport, specified as a valid length or a percentage.
  • auto: The offset is determined by the user agent. This typically defaults to 0px, but the user agent can adjust it if a non-zero value is more appropriate.
  • Global values: These include inherit, initial, revert, revert-layer, and unset.

Formal Definition

Initial valueauto
Applies toscroll containers
Inheritedno
Percentagesrelative to the scroll container’s scrollport
Computed valueas specified
Animation typeby computed value type

Formal Syntax

scroll-padding-block-start =
auto |
<length-percentage [0,]>
<length-percentage> =
<length> |
<percentage>

Browser Compatibility

The scroll-padding-block-start property is widely supported across modern browsers. Here is a summary of the browser compatibility:

  • Opera 56 and above
  • Safari 15 and above
  • Chrome 69 and above
  • Edge 79 and above
  • Firefox 68 and above

For detailed compatibility information, you can refer to the Browser Compatibility Data (BCD) tables.

Example

Here is an example demonstrating the use of scroll-padding-block-start:

<!DOCTYPE html>
<html>
<head>
<style>
.geek {
width: 275px;
height: 300px;
border: solid red;
color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 50px;
scroll-snap-align: start none;
}
.Website {
width: 300px;
height: 300px;
border: 2px solid green;
text-align: center;
overflow-x: hidden;
overflow-y: auto;
white-space: nowrap;
scroll-snap-type: y mandatory;
}
</style>
</head>
<body>
<div class="Website" style="scroll-padding-block-start: 90px;">
<div class="geek" style="background-color:darkgreen;">Geeks 1</div>
<div class="geek" style="background-color:rgb(129, 245, 21);">for 1</div>
<div class="geek" style="color:green;">Geeks 2</div>
<div class="geek" style="background-color:rgb(115, 223, 43);">for 2</div>
<div class="geek" style="color:green;">Geeks 3</div>
<div class="geek" style="color:black;">for 3</div>
</div>
</body>
</html>

Supported Browsers

The scroll-padding-block-start property is supported by the following browsers:

  • Opera 56 and above
  • Safari 15 and above
  • Chrome 69 and above
  • Edge 79 and above
  • Firefox 68 and above

This broad support ensures that you can use scroll-padding-block-start confidently in your web projects, knowing that it will work across a wide range of browsers and devices.

Explanation of Syntax

  • Keyword Values:
    • auto: The offset is determined by the user agent, typically defaulting to 0px but can be adjusted if a non-zero value is more appropriate.
  • <length> Values:
    • You can specify the offset using standard CSS length units such as pixels (px), ems (em), and percentages (%).
  • Global Values:
    • inherit: The property takes the same computed value as the parent element.
    • initial: The property is set to its initial value (auto).
    • revert: The property is reset to the value defined in the user-agent stylesheet.
    • revert-layer: The property is reset to the value defined in the user-agent stylesheet, taking cascade layers into account.
    • unset: The property is set to its inherited value if it inherits, otherwise to its initial value.

Example of Usage

Here is an example demonstrating the use of different values for scroll-padding-block-start:

/* Keyword value */
.container {
scroll-padding-block-start: auto;
}
/* Length value */
.container {
scroll-padding-block-start: 20px;
}
/* Percentage value */
.container {
scroll-padding-block-start: 10%;
}
/* Global value */
.container {
scroll-padding-block-start: inherit;
}

When to Use Each Value

  • Length and Percentage: Use these values when you need a specific amount of padding at the start of the scroll container. Lengths are useful for fixed designs, while percentages are useful for responsive designs.
  • auto: Use this value when you want the browser to handle the padding automatically. This is useful for dynamic content where the padding needs to adjust based on the context.
  • Global Values: Use these values when you need to inherit or reset the padding based on the parent element or user-agent stylesheet. This is useful for maintaining consistency across different parts of your website.

By understanding and utilizing these values, you can create a more user-friendly and visually appealing scrolling experience in your web projects.

Formal Definition

The scroll-padding-block-start property in CSS is formally defined with specific characteristics that outline its behavior and usage. These definitions help developers understand how the property interacts with other CSS properties and how it affects the layout of scroll containers. Below is the formal definition of scroll-padding-block-start:

PropertyDefinition
Initial valueauto
Applies toScroll containers
InheritedNo
PercentagesRelative to the scroll container’s scrollport
Computed valueAs specified
Animation typeBy computed value type

Explanation of Formal Definition

  • Initial value: The default value for scroll-padding-block-start is auto. This means that if no value is explicitly set, the browser will determine the appropriate offset.
  • Applies to: This property applies to scroll containers, which are elements that have scrollable content. This includes elements with overflow properties set to auto, scroll, or hidden.
  • Inherited: The scroll-padding-block-start property is not inherited from parent elements. This means that each element must have its own value specified if necessary.
  • Percentages: When using percentage values, they are calculated relative to the height of the scroll container’s scrollport. This allows for responsive design, as the padding will adjust based on the size of the scroll container.
  • Computed value: The computed value is the same as the specified value. This means that the browser will use the exact value you provide, whether it’s a length, percentage, or keyword.
  • Animation type: The property can be animated using CSS animations or transitions. The animation type is determined by the computed value type, allowing for smooth transitions between different padding values.

Understanding the formal definition of scroll-padding-block-start is crucial for developers who want to make the most of this property. By knowing how the property behaves and interacts with other CSS properties, you can create more sophisticated and user-friendly scrolling experiences in your web projects.

Explanation of Formal Syntax

  • auto: The browser decides the best offset. By default, it’s usually 0px unless the browser needs a different value.
  • <length-percentage [0,∞]>: You can set the offset using a length or percentage. The value must be non-negative.
  • <length>: Use units like pixels (px), ems (em), or rems (rem). For example, scroll-padding-block-start: 10px;.
  • <percentage>: Set the offset as a percentage of the scroll container’s height. For example, scroll-padding-block-start: 10%;.

Example of Formal Syntax

/* Keyword value */
.container {
scroll-padding-block-start: auto;
}
/* Length value */
.container {
scroll-padding-block-start: 20px;
}
/* Percentage value */
.container {
scroll-padding-block-start: 10%;
}

When to Use Each Syntax Option

  • Keyword Value (auto): Use this when you want the browser to handle the padding automatically. Great for dynamic content.
  • Length Value: Use this for a fixed amount of padding. Perfect for fixed designs.
  • Percentage Value: Use this for responsive padding that adjusts with the scroll container’s size. Ideal for responsive designs.

Browser Compatibility

The scroll-padding-block-start property is supported by:

  • Opera: Version 56 and above.
  • Safari: Version 15 and above.
  • Chrome: Version 69 and above.
  • Edge: Version 79 and above.
  • Firefox: Version 68 and above.

Example

Here’s how to use scroll-padding-block-start in practice:

<!DOCTYPE html>
<html>
<head>
<style>
.geek {
width: 275px;
height: 300px;
border: solid red;
color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 50px;
scroll-snap-align: start none;
}
.Website {
width: 300px;
height: 300px;
border: 2px solid green;
text-align: center;
overflow-x: hidden;
overflow-y: auto;
white-space: nowrap;
scroll-snap-type: y mandatory;
}
</style>
</head>
<body>
<div class="Website" style="scroll-padding-block-start: 90px;">
<div class="geek" style="background-color:darkgreen;">Geeks 1</div>
<div class="geek" style="background-color:rgb(129, 245, 21);">for 1</div>
<div class="geek" style="color:green;">Geeks 2</div>
<div class="geek" style="background-color:rgb(115, 223, 43);">for 2</div>
<div class="geek" style="color:green;">Geeks 3</div>
<div class="geek" style="color:black;">for 3</div>
</div>
</body>
</html>

Explanation

  1. HTML Structure:

    • A container (.Website) that acts as the scroll container.
    • Multiple .geek elements inside the container, each styled differently.
  2. CSS Styling:

    • The .Website class has a fixed width and height, with overflow set to auto for the y-axis.
    • The scroll-padding-block-start property is set to 90px.
    • Each .geek element is styled with specific dimensions, colors, and text alignment.
    • scroll-snap-align and scroll-snap-type ensure smooth scrolling.

Output

Running this example in a browser shows a scrollable container with multiple sections. The scroll-padding-block-start property ensures a 90px padding at the start, providing ample space for optimal viewing.

Supported Browsers

The scroll-padding-block-start property is widely supported:

  • Opera: Version 56 and above.
  • Safari: Version 15 and above.
  • Chrome: Version 69 and above.
  • Edge: Version 79 and above.
  • Firefox: Version 68 and above.

By using scroll-padding-block-start, you can enhance the scrolling experience, making your web projects more user-friendly across different platforms.

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.