Padding - Property CSS

padding
padding-block-start
padding-block-end
padding-inline-start
padding-inline-end

Summary of characteristics of the padding property

Quick description
Interior margins on the four sides.
Status
Standard
Usable for
HTML
Predefined values
0
Percentages
Calculated relative to the logical width of the parent.
Initial value
0
Inherited by default
No.
Animation type
Computed value : during an animation, the padding property gradually changes from one value to another.
W3C Module
CSS Box Model Module
References (W3C)
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: CR (Candidate Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of padding.

padding - Syntax DiagramSyntax diagram of the padding CSS property. length / % length / % {1,4} {1,4}padding:;padding:;
Syntax diagram of the padding property.
The links in the diagram provide more details about the values.

In the diagram, the bold terms are the predefined words of CSS, the other terms are described below:

  • length is a positive or zero numeric value, followed by one of the CSS dimension units.
  • % is a percentage calculated relative to the width of the element.

Description of the padding property.

padding defines the space between the border of an element and its content. This means it is the inner margins, not to be confused with the outer margins, which are managed by the property margin.

The inner margins in CSS

padding is a shorthand property that allows you to define the four inner margins of an element in a single rule. These can be defined individually by the properties:

  • padding-top: top inner margin.
  • padding-right: right inner margin.
  • padding-bottom: bottom inner margin.
  • padding-left: left inner margin.

Note: padding can apply to a table or the cells of a table, but not to other inner elements of the table: headers, rows, columns, etc.

Language support.

If we want to process text in a non-Latin language, which is written for example from right to left like Arabic, or from top to bottom like Japanese, it is preferable to use the so-called 'logical' properties:

padding-block: margins at the beginning and end of the block, taking into account the direction of writing.
Short hand property equivalent to the following two:

  • padding-block-start: margin at the beginning of the block, taking into account the writing direction. For European languages, it is equivalent to padding-top.
  • padding-block-end: margin at the end of the block, taking into account the writing direction. It is equivalent to padding-bottom for European languages.

padding-inline : Margins at the beginning and end of lines, in the direction of writing.
Summarized property equivalent to:

  • padding-inline-start : margin at the end of lines, in the direction of writing. For Latin languages, it is equivalent to padding-left.
  • padding-inline-end : Margin at the beginning of the line, in the direction of writing. It is equivalent to padding-right for Latin languages.

Refer to the property writing-mode for more details on the management of non-Latin languages.

Note: the shorthand property padding, with its four values corresponding to top, right, bottom, and left, is not dependent on the writing direction.

Here are the equivalences between physical properties and logical properties depending on the language of the text.

   

padding-top
padding-left
The direction and writing mode of this text adapt to the chosen language.
padding-right
padding-bottom

Values for padding.

  • padding: 15px 10px 10px 20px; a b c d

    From one to four positive or zero numerical values, along with their dimension unit (see the CSS dimension units). The values are separated by a space. They apply to the four inner margins, according to their number and the rule defined below.

    If the values are expressed as percentages, they are calculated based on the width of the parent element, including the top and bottom padding. This may come as a surprise: one might think that the percentages of vertical margins (top and bottom) are calculated relative to the height of the parent element. But this would lead to an unsolvable circular calculation: the margins depend on the height of the parent, and the latter depends on the margins.

    Here is how the values are treated, depending on their number:

    1. If a single value is specified, it applies to all four inner margins.
    2. If two values are specified, the first defines the top and bottom inner margins, and the second value defines the left and right inner margins.
    3. When three values are indicated, they respectively define the top inner margin, the left and right inner margins, and the bottom inner margin.
    4. Finally, if the four values are specified, they define the margins in the following order: top inner margin, right inner margin, bottom inner margin, left inner margin.
    Padding property with one value
    padding:a;
    Only one value
    Padding property with two value
    padding:a b;
    Two values
    Padding property with three value
    padding:a b c;
    Three values
    Padding property with four value
    padding:a b c d;
    Four values
  • padding: initial; (0) padding: inherit; padding: revert; padding: revertLayer; padding: unset;

    These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, unset.

Animation of the padding property.

The small example below consists of a main block (white background, bordered) containing another block (blue background, rounded corners). For this second element, it would have been possible to use an image.
The animation is achieved by varying the inner margins of the main block.

A third block is the parent of this set. When a background color is applied to it, it allows to materialize the inner margins during the animation.

 

Manipulating the padding property programmatically.

With Javascript, change the value of padding.

In Javascript, here is how to modify the value of padding. We see that Javascript offers a syntax with the typical CSS notation, in kebab-case, and another syntax with the notation in camel-case.

Javascript
let el = document.getElementById('id'); el.style['padding'] = '2%'; // or let el = document.getElementById('id'); el.style.padding = '2%';

With Javascript, read the value of padding.

TThis code example works if the property has been assigned directly to the element itself via its style attribute, without going through a CSS selector. The value is returned as it was defined, with the same unit.

Javascript
let el = document.getElementById('id'); let value = el.style['padding']; // or let el = document.getElementById('id'); let value = el.style.padding;

With Javascript, read the calculated value of padding.

The calculated value is the one that results from the evaluation of the relative units and the potential consideration of inherited values. If no value has been assigned to padding either to the element or to any of its parents, it will be the initial value.

In the case of padding, the value is returned in pixels or percentages.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('padding');

With JQuery, change the value of padding.

JQuery

$('#id').css('padding', '2%');

With JQuery, read the calculated value of padding.

As in Javascript, the value is returned in pixels or percentages.

JQuery
let value = $('#id').css('padding');

Other code examples.

Other examples of Javascript and JQuery code are provided on the page Javascript and CSS.

Test it yourself.

The buttons below apply the entered value to the property padding and then display either the values as they have been applied, or the calculated values. This second option allows you to see how the value of padding is serialized: it is observed that percentages are well stored as percentages, but that all other units are converted to pixels.

Simulator.

This first simulator allows you to vary the four values of padding. The inner margins are highlighted in a blue color (in fact, it is the background color of the parent element).

The height of the element being not fixed, changing the top and bottom padding causes a change in the height of the element. In the horizontal direction, it is the width of the content that adapts to the values of padding because the width of the element is limited.

padding :
One day CSS will rule the world. But not today, today it's pool.

Simulator for logical properties, sensitive to the sense of writing.

This simulator uses padding properties that are sensitive to the writing direction.


Writing mode:

padding-block-start :
padding-block-end :
padding-inline-start :
padding-inline-end :

One day CSS will rule the world.
But not today, today is pony.

Browsers compatibility with padding.

The property padding, in its physical version, has existed since the very first version of CSS. It does not pose any compatibility issues. More recent, the logical properties are however well recognized as well.

Column 1
Support for physical properties (not sensitive to writing mode): paddind, padding-top, padding-right, padding-bottom and padding-left.
Column 2
Logical property support, sensitive to write mode: padding-block, padding-inline, padding-block-start, padding-block-end, padding-inline-start and padding-inline-end.
1
Physical
properties
2
Logical
properties
Estimated overall support.
97%
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Edge

Opéra

Safari

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Firefox

Chrome

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini

Historic of the padding property.

The physical properties for defining inner margins (padding) have existed since the very first specification of the CSS language. The equivalent logical properties, supporting writing modes based on the language, were then added.

  • Cascading Style Sheets specification - Version 1

    Initial definition of the padding... properties and the padding shorthand property.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    Adding inherit value for defining paddings.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Box Model Module Level 3

    No modification concerning the padding... properties.
    WD
    July 26, 2001
    Working Draft.
    CR
    December 22, 2020
    Candidate Recommendation.
    PR
    February 16, 2023
    Proposed for recommendation.
    REC
    April 06, 2023
    Recommendation .
  • CSS Logical Properties and Values Level 1

    Definition of logical properties (taking into account the writing mode depending on the language).
    WD
    May 18, 2017
    Working Draft.
    CR
    PR
    REC
  • CSS Box Model Module Level 4

    No modification concerning the padding... properties.
    WD
    April 21, 2020
    Working Draft.
    CR
    PR
    REC

See also, regarding margins.

The margins (inner and outer) are described in this module of the CSS specification: CSS Box Model Module. The following definitions are also described in this same module.

Properties:

margin
Shorthand of the four margins.
margin-block-end
Defines the end of the block margin, given the writing mode of the text for this element.
margin-block-start
Defines the start of the block margin, given the writing mode of the text for this element.
margin-bottom
Bottom margin.
margin-inline-end
Defines the margin after the end of line, given the writing mode.
margin-inline-start
Defines the margin before the start of the lines of text, given the writing mode.
margin-left
Marge de gauche.
margin-right
Right margin.
margin-top
Top margin.
margin-trim
Langue française
Defines the management of successive margins within the same container.
padding-block-end
Defines the inner margin at the end of the block, given the writing mode.
padding-block-start
Defines the inner margin at the start of the block, given the writing mode.
padding-bottom
Inner margin at the bottom.
padding-inline-end
Defines the inner margin on the side of the end of the lines of text, given the writing mode.
padding-inline-start
Sets the padding on the side of the beginning of lines of text, given the writing mode.
padding-left
Interior margin on the left.
padding-right
Inner margin on the right.
padding-top
Inner margin at the top.