Padding in CSS creates space inside an element, between the content and the border. It is the internal spacing of a box. Padding makes buttons easier to click, cards easier to read, form fields more comfortable, and sections less cramped. Without padding, content often touches edges and feels visually broken.
Padding is part of the CSS box model. It sits outside the content area but inside the border. Unlike margin, padding is included in the element’s background painting area. If an element has a background color, that background usually appears behind the padding.
Basic Padding Syntax
The simplest padding rule applies the same padding to all four sides of an element.
.card {
padding: 24px;
}
This creates 24px of internal space on the top, right, bottom, and left sides. The content moves away from the edges of the card, making the design easier to read.
Individual Padding Sides
CSS can set padding on each side separately using padding-top, padding-right, padding-bottom, and padding-left.
.box {
padding-top: 20px;
padding-right: 30px;
padding-bottom: 20px;
padding-left: 30px;
}
Individual padding properties are useful when one side needs different internal spacing. For example, an alert box may need extra left padding if it contains an icon.
Padding Shorthand
The padding shorthand can accept one, two, three, or four values. It follows the same clockwise pattern as margin.
| Syntax | Meaning |
|---|---|
| padding: 20px; | All four sides are 20px |
| padding: 10px 20px; | Top and bottom 10px, left and right 20px |
| padding: 10px 20px 30px; | Top 10px, left and right 20px, bottom 30px |
| padding: 10px 20px 30px 40px; | Top, right, bottom, left |
.button {
padding: 0.75rem 1.25rem;
}
This button has 0.75rem vertical padding and 1.25rem horizontal padding. This is a common button pattern because buttons usually need more horizontal space than vertical space.
Padding and Background
Padding is inside the element’s background area. If a card has a white background and padding, the white background fills the padded space around the content.
.notice {
background-color: #e6e7ef;
padding: 1.5rem;
border-radius: 8px;
}
This creates a notice box where text does not touch the edges. The background color makes the padded area visible, and the border radius softens the corners.
Padding and Width
Padding affects element size depending on the box sizing model. In the default content-box model, padding is added outside the declared width. In border-box, padding is included inside the declared width.
.box {
width: 300px;
padding: 20px;
}
With content-box sizing, the visible width becomes 340px because 20px is added on the left and 20px on the right. This surprises many beginners.
* {
box-sizing: border-box;
}
With border-box sizing, width includes content, padding, and border. This makes layout calculations easier and is common in modern CSS resets.
Padding vs Margin
Padding and margin both create space, but padding creates internal space and margin creates external space. If the space should be clickable, colored, or inside the component boundary, use padding. If the space should separate one component from another, use margin or gap.
| Situation | Better Choice |
|---|---|
| Button needs bigger click area | Padding |
| Cards need space between them | Margin or gap |
| Text should move away from border | Padding |
| Section should move away from next section | Margin |
| Input text should not touch field edge | Padding |
Padding for Click Targets
Padding is important for usability because it increases the clickable or tappable area of elements like buttons and links. A link with only text and no padding may be hard to tap on mobile. A button with enough padding feels easier to use.
.nav-link {
display: inline-block;
padding: 0.75rem 1rem;
}
The inline-block display allows vertical padding to behave more predictably while keeping the link inline with other navigation items.
Percentage Padding
Padding can use percentages. Percentage padding is based on the inline size of the containing block in many cases. This behavior has historically been used to create aspect-ratio boxes before the aspect-ratio property became common.
.ratio-box {
height: 0;
padding-top: 56.25%;
}
This old pattern creates a 16:9 ratio box. Modern CSS can often use aspect-ratio: 16 / 9 instead, which is clearer.
Logical Padding Properties
Logical padding properties follow writing direction. Instead of padding-left and padding-right, you can use padding-inline-start, padding-inline-end, padding-inline, and padding-block.
.content {
padding-inline: 1.5rem;
padding-block: 2rem;
}
For left-to-right layouts, padding-inline maps to left and right padding. In other writing directions, it adapts. Logical properties are useful for multilingual and modern layout systems.
Responsive Padding
Padding should respond to screen size. A desktop section may look good with 5rem of padding, but that same padding can waste too much space on a small phone. Responsive CSS often uses media queries or clamp() for fluid padding.
.section {
padding: clamp(2rem, 6vw, 5rem) 1.5rem;
}
This section has vertical padding that grows with the viewport but stays within a minimum and maximum range. This creates responsive spacing without writing multiple breakpoints.
Padding and Spacing Systems
Like margin, padding works best when it follows a spacing system. Consistent padding makes components feel related. If one card uses 17px, another uses 23px, and another uses 31px without reason, the design starts to feel accidental.
:root {
--card-padding: 1.5rem;
--section-padding: clamp(2rem, 6vw, 5rem);
}
.card {
padding: var(--card-padding);
}
.section {
padding-block: var(--section-padding);
}
Variables are not required, but they make spacing decisions reusable. A consistent padding system is especially useful for cards, article blocks, product boxes, forms, and dashboards.
Padding and Overflow
Padding can contribute to overflow when combined with fixed widths, long content, or the default content-box model. If a box is set to width: 100% and also has large horizontal padding, it may become wider than its parent unless border-box sizing is used.
.panel {
width: 100%;
padding: 2rem;
box-sizing: border-box;
}
The box-sizing: border-box rule keeps the panel inside the parent width. This is one reason many projects apply border-box globally.
Padding and Safe Areas
Modern devices may have notches, rounded screen corners, or system UI areas. CSS environment variables can help add safe-area padding for full-screen layouts on supported devices.
.app-shell {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
}
Most normal article layouts do not need this, but full-screen mobile web apps and fixed headers sometimes do. The goal is to prevent content from sitting under device hardware or system interface areas.
Padding and Border Radius
Padding works closely with border radius. A card with rounded corners and very little padding can feel cramped because the text visually fights the rounded edge. Good padding gives the content enough room to sit comfortably inside the shape.
.callout {
border-radius: 12px;
padding: 1.25rem 1.5rem;
}
When the border radius is large, the padding often needs to be larger too. This keeps text away from curved corners and makes the component look balanced.
Nested Padding
Nested components can accidentally create too much padding. For example, a section may have padding, a card inside it may have padding, and a content block inside the card may add more padding. Each value may look reasonable alone, but together they can waste space.
When a layout feels too spacious, inspect the parent and child elements together. The issue may not be one huge padding value. It may be several normal padding values stacked through nesting.
Padding in Tables and Forms
Tables and forms depend heavily on padding. Table cells need padding so data does not touch grid lines. Inputs need padding so typed text feels comfortable. Labels and help text need spacing that supports scanning.
td,
th {
padding: 0.75rem 1rem;
}
input {
padding: 0.7rem 0.9rem;
}
Small changes to padding can make tables and forms feel much more polished. Too little padding feels cramped. Too much padding can make dense information harder to scan.
Padding and Design Rhythm
Padding should match the rhythm of the surrounding layout. If a card uses large internal padding but nearby buttons use tiny padding, the interface can feel inconsistent. Reusing a small set of padding values helps components feel like they belong to the same design system.
This is especially useful on content-heavy sites where articles, tables, code blocks, callouts, and forms appear together. Consistent padding makes the page easier to scan.
Good padding quietly improves readability because users notice cramped layouts faster than they notice the exact spacing value.
This matters on mobile screens too.
Spacing affects touch comfort.
Always.
Padding in Common Components
Cards, buttons, inputs, alerts, modals, and navigation links all rely on padding. The right padding depends on the component purpose. Body text areas need comfortable reading space. Buttons need enough space to feel clickable. Inputs need enough internal room so typed text does not touch the border.
- Use larger padding for cards and sections.
- Use balanced vertical and horizontal padding for buttons.
- Use enough padding in form fields for comfortable typing.
- Use consistent padding values across similar components.
- Reduce padding carefully on mobile screens.
- Avoid using padding only to fix external spacing problems.
Common Padding Mistakes
- Using padding when margin or gap is the correct spacing tool.
- Forgetting that padding can increase element size in content-box mode.
- Using too little padding in buttons and inputs.
- Using huge fixed padding that breaks small screens.
- Assuming percentage padding is based on height.
- Repeating random padding values instead of using a spacing system.
Padding in CSS FAQ
What does padding do in CSS?
Padding creates space inside an element, between the content and the border.
Does padding affect background color?
Yes. The element background usually paints behind the padding area.
What is the difference between padding and margin?
Padding is internal space inside an element. Margin is external space outside an element.
Can padding use rem?
Yes. Padding can use px, rem, em, %, viewport units, and other length units depending on the design goal.
Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.