VOOZH about

URL: https://blog.logrocket.com/css-reference-guide-padding/

⇱ CSS Reference Guide: padding - LogRocket Blog


2020-10-30
452
#css#reference guide
Solomon Eseme
27748

The CSS padding shorthand property is used to create space within an element. It defines the content portion of the box model.

πŸ‘ CSS Reference Guide: Padding Shorthand Property

Jump ahead:

The padding property can be specified using lengths, percentages, and keywords such as auto. It can also accept negative values.


Demo

See the Pen
CSS Padding Example
by Solomon Eseme (@kaperskyguru)
on CodePen.


Syntax

div {
 padding: <length> | <percentage> | auto
}

The CSS padding shorthand property is used to define up to four values for padding-top, padding-right, padding-left, and padding-bottom.

div {
 padding: 2px 3px 4px 5px;
}

The equivalent padding can be defined as below:

div {
 padding-top: 2px;
 padding-right: 3px;
 padding-left: 5px;
 padding-bottom: 4px;
}

Values

The padding property can accept one to four values.

One value

When one value is supplied to the CSS padding shorthand property, it applies the same padding value to all four sides.

div {
 padding: 5px;
}
 // SAME AS
div {
 padding-top: 5px;
 padding-right: 5px;
 padding-left: 5px;
 padding-bottom: 5px;
}

Two values

When two values are specified to the padding property, the first value is applied to the top and bottom padding, while the second is applied to the left and right.

div {
 padding: 5px 3px;
}
 // SAME AS
div {
 padding-top: 5px;
 padding-right: 3px;
 padding-left: 3px;
 padding-bottom: 5px;
}

Three values

When three values are supplied, the first value is applied to the top padding; the second value is applied to the left and right padding; and the third value is applied to the bottom padding.

div {
 padding: 5px 3px 1px;
}
 // SAME AS
div {
 padding-top: 5px;
 padding-right: 3px;
 padding-left: 3px;
 padding-bottom: 1px;
}

Four values

When four values are supplied, the values are applied in clockwise order. In other words: the first value is applied to the top padding; the second value is applied to the right padding; the third value is applied to the bottom; and the fourth the value is applied to the left padding.

div {
 padding: 5px 3px 1px 6px;
}
 // SAME AS
div {
 padding-top: 5px;
 padding-right: 3px;
 padding-left: 6px;
 padding-bottom: 1px;
}

Centering elements

With the use of the auto keyword, it is very easy to center elements in the container using the padding property.

div {
 padding: 2em auto; /* top and bottom: 2em padding */
 /* Box is horizontally centered */
}

div {
 padding: auto; /* top and bottom: 0 padding */
 /* Box is horizontally centered */
}

Negative values

When negative values are supplied to the padding, they pull the element in a particular direction rather than push it.

For example, the snippet below will pull the element towards the top by 5px, towards the right by 3px, towards the left by 6px, and towards the bottom by 1px.


Over 200k developers use LogRocket to create better digital experiences

πŸ‘ Image
Learn more β†’

div {
 padding: -5px -3px -1px -6px;
}

Is your frontend hogging your users' CPU?

As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If you’re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.

πŸ‘ LogRocket Dashboard Free Trial Banner

LogRocket lets you replay user sessions, eliminating guesswork around why bugs happen by showing exactly what users experienced. It captures console logs, errors, network requests, and pixel-perfect DOM recordings β€” compatible with all frameworks.

LogRocket's Galileo AI watches sessions for you, instantly identifying and explaining user struggles with automated monitoring of your entire product experience.

Modernize how you debug web and mobile apps β€” start monitoring for free.

πŸ‘ Image
πŸ‘ Image
πŸ‘ Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

AI made design faster, but did it make it less intentional?

AI has accelerated design execution, but speed can come at the expense of intentionality. Learn how UX teams can preserve product thinking and judgment.

πŸ‘ Image
Eric Chung
Jun 18, 2026 β‹… 9 min read

How to build a virtual engineering team with Gemini CLI subagents

Learn how to use Gemini CLI subagents to delegate frontend, backend, testing, and docs tasks to specialized agents with guardrails and clear ownership.

πŸ‘ Image
Emmanuel John
Jun 18, 2026 β‹… 10 min read

Debug Next.js apps with AI agents and next-browser

Learn how next-browser gives AI agents runtime context for debugging Next.js apps, including React props, hydration, PPR, forms, and performance.

πŸ‘ Image
Emmanuel John
Jun 17, 2026 β‹… 9 min read

How PMs can use communication and AI to prevent duplicate work

Learn how PMs can use AI and communication to spot duplicate work early, align teams, and protect engineering capacity.

πŸ‘ Image
Zeynep Cansu Yildirim
Jun 17, 2026 β‹… 11 min read
View all posts