Thuta Learning
CSS
IntermediateWeb DevelopmentBeginner

Padding

CSSLesson 1220 min

What you'll walk away with

  • Read the one-, two-, three- and four-value forms of the padding shorthand
  • Write direction-aware padding with padding-block and padding-inline
  • Build tap targets at least 44px tall with padding instead of a fixed height
  • Explain that % padding uses the containing block's width, and how vertical padding behaves on inline elements

Explain it simply

Explain it simply

Padding is the space inside a box, between the content and the border. It is like the white mat between a photo and its frame: without it, the picture presses against the frame and feels cramped. The special thing about padding is that the element's background extends into it, so adding padding makes a button's colored area bigger and easier to tap.

It is easy to mix up with margin, but the rule is simple: padding is inside the box, margin is outside it.

Problem → Why → Solution

The problem: a hard-to-tap icon button and a cramped card

On a shopping cart page, the × button that removes an item is sized like its icon, 16px by 16px, with padding: 0. It works with a desktop mouse, but a fingertip is much larger than 16px, so phone users keep opening the neighbouring item instead of removing the one they wanted. The summary card on the same page has no padding either, so its text presses against the border and is hard to read.

.remove-btn {
  width: 16px;
  height: 16px;
  padding: 0;        /* the whole tap target is the 16px icon */
}

.summary-card {
  border: 1px solid #cbd5e1;
  /* no padding: text touches the border */
}

Why does this happen?

The clickable area is the element's whole border box: content plus padding plus border. With padding 0, only the icon itself can be tapped. Keep the icon at 16px and add padding: 14px, and the tap area becomes 44px while the visible icon stays the same size. The card likewise only needs padding to separate its text from the border.

Core concept: writing padding values and their rules

The padding shorthand takes one to four values. One value applies to all four sides. Two values mean top/bottom then left/right, the most common form for buttons. Three values mean top, left/right, bottom. Four values go clockwise from the top: top, right, bottom, left. To change just one side, use a longhand such as padding-top.

Logical properties read more clearly. padding-block is the direction in which lines stack (top and bottom in English and Myanmar), and padding-inline is the direction text runs (left and right). padding-block: 12px; padding-inline: 20px; equals padding: 12px 20px here, but it stays correct in vertical writing modes and says what it means. padding-inline-start targets only the side where text begins.

When you are unsure whether to use padding or margin, ask one question: should this space show the element's background and respond to clicks? If yes, it is padding, as around a button's label or inside a card. If it is outside space between two elements, use margin, or the gap property you will meet in later lessons.

Remember three rules. First, padding cannot be negative, unlike margin. Second, a percentage on any side, top and bottom included, is calculated from the containing block's width, so padding-top: 50% inside a 400px-wide parent is 200px. Third, on inline elements such as <a> and <span>, vertical padding enlarges the background but does not change the line height, so it can overlap the lines above and below; display: inline-block makes it take real space.

Finally, padding grows the element under content-box and eats into the content under border-box. Either way, skip the fixed height and let content plus padding decide how tall the box is.

Mental model: around the clock
  1. 1 value: all four sides
  2. 2 values: top+bottom | left+right
  3. 3 values: top | left+right | bottom
  4. 4 values: top, right, bottom, left

Start at 12 o'clock and go clockwise; a missing side copies its opposite side.

General pattern

css
selector {
  padding: all;
  padding: block inline;
  padding: top inline bottom;
  padding: top right bottom left;

  padding-block: start end;     /* top and bottom here */
  padding-inline: start end;    /* left and right here */
}

Pick one of the four shorthand forms, or set each direction with the two logical properties. A single value on a logical property applies to both its start and end.

Example: a pill button and a card

This example shows the two most common padding patterns: a two-value button and a one-value card. Notice that the button's height is never written anywhere.

.button {
  padding: 12px 20px; /* top/bottom, left/right */
  border-radius: 999px;
}

.card {
  padding: 24px;
}

Code breakdown

  1. Line 2padding: 12px 20px; /* top/bottom, left/right */

    12px top and bottom, 20px left and right. The button's height is the label's line-height plus 24px, so a label that wraps to two lines makes the button taller. On an <a>, add display: inline-block, or the vertical padding takes no space.

  2. Line 3border-radius: 999px;

    A radius far larger than the button's height, which the browser scales down into a pill with fully round ends. The background, which fills the padding, is clipped to that shape.

  3. Line 7padding: 24px;

    24px on all four sides of the card, so its heading, text and buttons all sit an even distance from the border.

You should see
.button gets 12px above and below its label and 20px on each side, inside a pill shape; its height is the label's line-height plus 24px. .card has 24px between its border and its content on every side.

Tap target size

WCAG 2.2 success criterion 2.5.8 Target Size (Minimum), level AA, asks for targets of at least 24×24 CSS px (or enough spacing from neighbouring targets), and 2.5.5 at level AAA sets 44×44 px. Apple's design guidelines also recommend 44pt. Padding grows the tappable area without growing the icon, which makes it the easiest tool for meeting these targets.

icon-button.css
.remove-btn {
  padding: 14px;            /* 16px icon + 14px + 14px = 44px target */
  line-height: 0;           /* no extra line box around the icon */
  border: 0;
  background: transparent;
}
.remove-btn svg {
  width: 16px;
  height: 16px;
}

Visually this button is still a small 16px icon, but select it in DevTools and you will find a 44 × 44px box. line-height: 0 stops a line box from adding extra space around the icon, so the size is exactly icon plus padding.

In real projects

Where this shows up in real projects

Design systems usually restrict padding to multiples of 4px or 8px (8, 12, 16, 24, 32); in Tailwind, p-4 is 1rem (16px) of padding and px-5 is 1.25rem of inline padding. Using a scale gives every component the same inner rhythm and ends the "13px or 14px?" debates between designers and developers.

Mobile-first apps keep navigation icons, close buttons and checkboxes visually small and use padding to bring the tap area to around 44px. Because their buttons have no fixed height, a longer translation such as Myanmar simply makes the button taller instead of overflowing. Responsive layouts also switch card padding, say from 16px on phones to 32px on desktop, with a media query or clamp().

Common mistakes

Common mistakes

1. Expecting % padding to use the parent's height

padding-top: 10% inside a banner that is 100px tall and 800px wide gives 80px, not 10px, because it is calculated from the width. Write vertical padding in px or rem.

2. Styling an inline link as a button without changing display

Give <a class="btn"> padding: 12px 20px while it is still inline, and its background overlaps the lines above and below without pushing anything away. Add display: inline-block.

3. Combining padding with a fixed height

height: 40px; padding: 12px; leaves only 16px for content under border-box, and a two-line label overflows. Delete the height, let padding and line-height decide, and use min-height if you need a floor.

Best practices

Best practices

  • Choose padding values from one 4px or 8px scale and store them in CSS variables such as --space-2 and --space-4.
  • Try buttons and link-buttons in a phone emulator as if tapping with a thumb, and measure the tap area in DevTools.
  • Never write a fixed height on cards or buttons, and test them with the longest translated label.
  • In new code, write horizontal padding with padding-inline and vertical padding with padding-block.

Mini challenge

Mini challenge: a workshop card

Build a card inviting people to a workshop. It holds a <button> and an <a> link styled the same way, and both must be 44px tall without any height property.

  • Card: padding-block 24px, padding-inline 20px (logical properties)
  • .btn: padding-block 12px, padding-inline 20px, line-height 1.25, font: inherit
  • The <a class="btn"> must take up its full padding too
  • No height or min-height; a longer label must make the button taller
workshop.html
<!doctype html>
<html lang="en">
<style>
  /* TODO: .card - max width, logical block/inline padding, border, radius */

  /* TODO: .btn - make the link behave like a box, logical padding,
     inherit the page font, a line-height that gives 20px of text,
     colors, no border, pill corners, no underline */
</style>
<div class="card">
  <h2>Join the workshop</h2>
  <p>Saturday, 10:00 at the community library.</p>
  <button class="btn" type="button">Reserve a seat</button>
  <a class="btn" href="#details">Read the full schedule</a>
</div>
</html>

Expected: Two blue pills sit side by side in the card, both exactly 44px tall. Make the link's label long enough to wrap onto two lines and that button grows to 64px with nothing overflowing.

Show hint

Buttons do not inherit the page's font-size by default (Chrome uses 13.33px); font: inherit brings in the 16px. 16px × 1.25 = 20px, and 20 + 12 + 12 = 44px.

Show solution
workshop.html
<!doctype html>
<html lang="en">
<style>
  .card {
    max-inline-size: 360px;
    padding-block: 24px;
    padding-inline: 20px;
    border: 1px solid #cbd5e1;
    border-radius: 12px;
  }
  .btn {
    display: inline-block;
    padding-block: 12px;
    padding-inline: 20px;
    font: inherit;
    line-height: 1.25;
    color: #ffffff;
    background: #2563eb;
    border: 0;
    border-radius: 999px;
    text-decoration: none;
  }
</style>
<div class="card">
  <h2>Join the workshop</h2>
  <p>Saturday, 10:00 at the community library.</p>
  <button class="btn" type="button">Reserve a seat</button>
  <a class="btn" href="#details">Read the full schedule</a>
</div>
</html>

Quick quiz 1 / 3

With padding: 10px 20px 30px;, what is the left padding?

Quick quiz 2 / 3

A child inside a parent 400px wide and 100px tall has padding-top: 50%. How much padding does it get?

Quick quiz 3 / 3

What happens when an inline <a> gets padding: 12px 20px?

What to remember

  • Padding sits between content and border and is covered by the background, so it enlarges the tappable area.
  • Shorthand values run clockwise from the top; padding-block and padding-inline name the direction plainly.
  • Percentage padding is calculated from the containing block's width, and padding can never be negative.
  • Reach roughly 44px tap targets with padding instead of fixed heights, and make inline links inline-block.

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.