Tillitsdone
down Scroll to discover

CSS Padding-Block Simplify Padding Management

Learn about CSS padding-block, a powerful tool for managing logical block padding.

Discover use cases and available options like length, percentage, inherit, and initial.
thumbnail

Introduction

The padding-block property in CSS is a shorthand tool for setting the logical block start and end padding of an element. It simplifies padding management by mapping values to physical padding properties based on the element’s writing mode, directionality, and text orientation. Introduced in September 2021, padding-block has become a reliable feature supported across many devices and browser versions.

This property helps you streamline your CSS code and ensure consistent padding across various elements, enhancing the overall design and functionality of your web pages. It’s especially useful for responsive design and accessibility.

Compatibility and Browser Support

The padding-block property is widely supported across various browsers, ensuring consistent designs across platforms.

Compatibility Summary:

  • Firefox: Full support.
  • Google Chrome: Full support.
  • Microsoft Edge: Full support.
  • Opera: Full support.

For detailed compatibility information, refer to the MDN Web Docs.

Syntax and Values

The padding-block property allows you to define the logical block start and end padding of an element. It accepts one or two values.

Syntax:

padding-block: <length> | <percentage> | inherit | initial | revert | revert-layer | unset;

Property Values:

  • <length>: Specifies a fixed value for the padding, like pixels (px) or centimeters (cm).
    padding-block: 10px; /* Sets both start and end padding to 10px */
    padding-block: 10px 20px; /* Sets start padding to 10px and end padding to 20px */
  • <percentage>: Sets the padding as a percentage relative to the nearest block container’s width.
    padding-block: 5% 10%; /* Sets start padding to 5% and end padding to 10% */
  • inherit: Inherits the padding from the parent element.
    padding-block: inherit;
  • initial: Resets the padding to its default value (0).
    padding-block: initial;
  • revert: Resets the padding to the browser’s default style.
    padding-block: revert;
  • revert-layer: Resets the padding to the browser’s default style for the cascade layer.
    padding-block: revert-layer;
  • unset: Removes any custom padding, allowing the element to use the default or inherited value.
    padding-block: unset;

Usage:

  • One value applies to both the start and end padding.
    padding-block: 15px; /* Sets both start and end padding to 15px */
  • Two values set the start padding first and the end padding second.
    padding-block: 20px 30px; /* Sets start padding to 20px and end padding to 30px */

Description and Use Cases

The padding-block property in CSS defines the logical block start and end padding of an element. It adapts to the element’s writing mode, directionality, and text orientation, making it ideal for responsive and accessible designs.

Description

padding-block corresponds to padding-top and padding-bottom, or padding-right and padding-left, depending on the writing mode (vertical-rl, vertical-lr, etc.). This logical approach ensures consistent layouts regardless of text direction.

Use Cases

  1. Responsive Design:
    • Ensures elements maintain appropriate spacing across different devices.
    .responsive-element {
    padding-block: 10px 20px;
    }
  2. Internationalization:
    • Ensures correct padding for different languages and writing modes.
    .international-element {
    writing-mode: vertical-rl;
    padding-block: 15px 25px;
    }
  3. Accessibility:
    • Improves readability and accessibility by ensuring adequate spacing.
    .accessible-element {
    padding-block: 20px;
    }
  4. Complex Layouts:
    • Simplifies padding management in dynamic and adaptive interfaces.
    .complex-layout {
    writing-mode: sideways-rl;
    padding-block: 10% 15%;
    }

Formal Definition and Syntax

The padding-block property is a shorthand for setting logical block start and end padding.

Formal Definition

  • Initial Value:
    • padding-block-start: 0
    • padding-block-end: 0
  • Applies To: All elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group, and table-column.
  • Inherited: No
  • Percentages: Relative to the logical width of the containing block.
  • Computed Value: As <length>
  • Animation Type: A length

Formal Syntax

padding-block = <'padding-block-start'> <'padding-block-end'>?
<padding-block-start> = <length-percentage>
<padding-block-end> = <length-percentage>
<length-percentage> = <length> | <percentage>
  • <length>: A fixed length value.
  • <percentage>: A percentage value relative to the logical width of the containing block.

Syntax Explanation

  • One value sets both padding-block-start and padding-block-end.
    padding-block: 10px; /* Sets both start and end padding to 10px */
  • Two values set padding-block-start and padding-block-end separately.
    padding-block: 20px 30px; /* Sets start padding to 20px and end padding to 30px */

Examples and Demonstrations

Here are practical examples to illustrate how the padding-block property works in different scenarios.

Example 1: Basic Usage

HTML:

<!DOCTYPE html>
<html>
<head>
<title>CSS padding-block Example</title>
<style>
.example-box {
background-color: yellow;
width: 120px;
height: 120px;
padding-block: 20px 40px;
text-align: center;
}
</style>
</head>
<body>
<div class="example-box">Example Text</div>
</body>
</html>

CSS:

.example-box {
background-color: yellow;
width: 120px;
height: 120px;
padding-block: 20px 40px;
text-align: center;
}

Result: The text “Example Text” will have 20px of padding at the top and 40px of padding at the bottom within the yellow box.

Example 2: Vertical Writing Mode

HTML:

<!DOCTYPE html>
<html>
<head>
<title>CSS padding-block with Vertical Writing Mode</title>
<style>
.vertical-text {
background-color: yellow;
width: 120px;
height: 120px;
writing-mode: vertical-rl;
padding-block: 20px 40px;
text-align: center;
}
</style>
</head>
<body>
<div class="vertical-text">Example Text</div>
</body>
</html>

CSS:

.vertical-text {
background-color: yellow;
width: 120px;
height: 120px;
writing-mode: vertical-rl;
padding-block: 20px 40px;
text-align: center;
}

Result: The text “Example Text” will be displayed vertically with 20px of padding on the right and 40px of padding on the left within the yellow box.

Example 3: Responsive Design

HTML:

<!DOCTYPE html>
<html>
<head>
<title>CSS padding-block for Responsive Design</title>
<style>
.responsive-box {
background-color: yellow;
width: 100%;
height: 100px;
padding-block: 5% 10%;
text-align: center;
}
</style>
</head>
<body>
<div class="responsive-box">Responsive Text</div>
</body>
</html>

CSS:

.responsive-box {
background-color: yellow;
width: 100%;
height: 100px;
padding-block: 5% 10%;
text-align: center;
}

Result: The text “Responsive Text” will have 5% padding at the top and 10% padding at the bottom within the yellow box, which will adapt to the width of the screen.

Related Properties and Values

The padding-block property is part of a set of logical properties that make CSS more intuitive and flexible. Here are some related properties and values to help you create adaptable web designs.

Logical Properties

  1. padding-block-start and padding-block-end

    • These properties set the padding at the start and end of a block. padding-block is a shorthand for these.
    .example {
    padding-block-start: 10px;
    padding-block-end: 20px;
    }
  2. padding-inline-start and padding-inline-end

    • These properties set the padding at the start and end of an inline element.
    .example {
    padding-inline-start: 15px;
    padding-inline-end: 25px;
    }
  3. padding-inline

    • This shorthand property sets the inline start and end padding.
    .example {
    padding-inline: 10px 20px;
    }

Physical Properties

  1. padding-top and padding-bottom

    • These properties set the padding at the top and bottom of an element.
    .example {
    padding-top: 10px;
    padding-bottom: 20px;
    }
  2. padding-right and padding-left

    • These properties set the padding at the right and left of an element.
    .example {
    padding-right: 15px;
    padding-left: 25px;
    }

Writing Mode and Direction Properties

  1. writing-mode

    • This property sets the writing mode, such as horizontal or vertical text.
    .example {
    writing-mode: vertical-rl;
    }
  2. direction

    • This property sets the text direction, such as left-to-right (ltr) or right-to-left (rtl).
    .example {
    direction: rtl;
    }
  3. text-orientation

    • This property sets the orientation of text within an element.
    .example {
    text-orientation: sideways;
    }

Example of Combined Usage

Here’s an example that combines these properties:

HTML:

<!DOCTYPE html>
<html>
<head>
<title>CSS Related Properties Example</title>
<style>
.example-box {
background-color: lightblue;
width: 200px;
height: 150px;
padding-block: 10px 20px;
padding-inline: 15px 25px;
writing-mode: vertical-rl;
text-align: center;
}
</style>
</head>
<body>
<div class="example-box">Example Text</div>
</body>
</html>

CSS:

.example-box {
background-color: lightblue;
width: 200px;
height: 150px;
padding-block: 10px 20px;
padding-inline: 15px 25px;
writing-mode: vertical-rl;
text-align: center;
}

In this example, the .example-box element uses the padding-block and padding-inline properties to set padding based on the logical block and inline directions. The writing-mode property is set to vertical-rl to display text vertically from right to left.

By using these related properties, you can create flexible and intuitive web designs that adapt to different writing modes, text directions, and screen sizes. These properties are essential for modern web development, helping you achieve a seamless and user-friendly experience.

For more details, check out the [CSS Logical Properties and Values Level 1 specification]WebsiteUrl.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
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.