CSS Padding
Learn to control space inside elements with CSS padding
What is CSS Padding?
CSS padding creates internal space between element content and borders, directly affecting visual presentation and touch target sizes. This fundamental box model property accepts length values in pixels, percentages, or viewport units. Proper padding implementation improves readability through adequate text breathing room and enhances accessibility through sufficient interactive target areas. Search engines recognize well-padded content as positive user experience signals, particularly for mobile interfaces where touch targets significantly impact engagement metrics.
Blue area = Padding space
๐น Padding vs Margin
Margin controls external space outside element borders, managing relationships between adjacent components in document flow. margin-collapse behavior creates efficient spacing without excessive gap accumulation in vertical stacks. This external spacing doesn't absorb background properties, remaining transparent to reveal parent element backgrounds. Proper margin usage establishes visual hierarchy and breathing room between content sections, improving scannability and reducing cognitive load for better user experience.
Padding (Inside)
Space between border and content
Margin (Outside)
Space outside the border
๐น Basic Padding Syntax
CSS padding syntax supports individual side declarations through padding-top, padding-right, padding-bottom, and padding-left properties. This granular control enables precise spacing adjustments for complex layouts and responsive adaptations. Longhand syntax provides maximum control for asymmetrical spacing requirements, while shorthand alternatives offer efficient code reduction. Understanding both approaches ensures optimal implementation for various design scenarios and maintenance requirements throughout project lifecycles.
/* All sides */
.box {
padding: 20px; /* 20px on all sides */
}
/* Individual sides */
.box {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
}
Result:
๐น Padding Shorthand Values
Padding shorthand properties accept one to four values following clockwise ordering from top to left. Single values apply uniformly to all sides, two values set vertical then horizontal spacing, three values define top, horizontal, then bottom spacing, while four values specify each side individually. This efficient syntax reduces CSS file size and improves code maintainability while providing flexible spacing control essential for responsive design implementations.
/* 1 value - all sides */
padding: 20px;
/* 2 values - vertical | horizontal */
padding: 10px 20px; /* top/bottom: 10px, left/right: 20px */
/* 3 values - top | horizontal | bottom */
padding: 10px 20px 30px; /* top: 10px, left/right: 20px, bottom: 30px */
/* 4 values - top | right | bottom | left (clockwise) */
padding: 10px 15px 20px 25px;
Visual Examples:
๐น Padding with Backgrounds
Card components demonstrate how padding extends background colors to create self-contained content containers with clear visual boundaries. This approach separates card content from surrounding layout elements while maintaining consistent internal spacing. The extended background area provides ample touch targets for interactive cards on mobile devices, improving accessibility and user experience metrics. Proper implementation follows responsive spacing systems that adapt padding values across breakpoints for optimal presentation.
.button {
background-color: #007bff;
color: white;
padding: 12px 24px; /* Vertical: 12px, Horizontal: 24px */
border: none;
border-radius: 5px;
cursor: pointer;
}
.card {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
padding: 20px;
border-radius: 8px;
}
Result:
Card Title
This card has padding that extends the background color.
๐น Percentage Padding
Percentage padding frequently creates responsive aspect ratios for video containers and image wrappers using the padding-top: 56.25% technique. This approach establishes height relative to width, maintaining consistent proportions during responsive scaling. Implementation combines percentage padding with position: absolute for content centering within the created space. This technique ensures embedded media maintains intended aspect ratios across all viewport sizes, improving visual consistency and Core Web Vitals metrics.
.responsive-box {
width: 100%;
padding: 5%; /* 5% of parent width on all sides */
background-color: #e3f2fd;
border: 1px solid #007bff;
}
.aspect-ratio-box {
width: 100%;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
background-color: #f0f0f0;
position: relative;
}
Result:
๐ก Tip:
Percentage padding is often used to create responsive aspect ratios for videos and images.
๐น Box-Sizing and Padding
Using box-sizing: border-box creates predictable layouts by including padding and borders within declared element dimensions. This approach simplifies responsive calculations and prevents unexpected overflow conditions during spacing adjustments. Global border-box implementation through CSS reset establishes consistent box model behavior across all elements, reducing calculation complexity and improving maintainability. This technique represents modern best practice for manageable responsive design systems.
/* Default: content-box */
.content-box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 2px solid #333;
/* Total width = 200px + 40px padding + 4px border = 244px */
}
/* Better: border-box */
.border-box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 2px solid #333;
/* Total width = 200px (padding and border included) */
}
Comparison:
content-box (244px total)
border-box (200px total)
โ Best Practice:
Use
box-sizing: border-box
for more predictable layouts:
* {
box-sizing: border-box;
}
๐น Practical Examples
Button Styles
/* Small button */
.btn-sm {
padding: 8px 16px;
font-size: 14px;
}
/* Medium button */
.btn-md {
padding: 12px 24px;
font-size: 16px;
}
/* Large button */
.btn-lg {
padding: 16px 32px;
font-size: 18px;
}
Result:
Card Layout
.card {
background: white;
border: 1px solid #ddd;
border-radius: 8px;
padding: 24px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.card-header {
padding: 16px 24px;
border-bottom: 1px solid #eee;
margin: -24px -24px 20px -24px; /* Negative margins to extend to edges */
}
.card-footer {
padding: 16px 24px;
border-top: 1px solid #eee;
margin: 20px -24px -24px -24px;
}
Form Elements
.form-input {
padding: 12px 16px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
width: 100%;
}
.form-textarea {
padding: 12px 16px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
min-height: 100px;
}
๐น Common Padding Patterns
/* Container padding */
.container {
padding: 0 20px; /* Horizontal padding only */
max-width: 1200px;
margin: 0 auto;
}
/* Section spacing */
.section {
padding: 60px 0; /* Vertical padding only */
}
/* Card content */
.card-content {
padding: 20px;
}
/* Button padding */
.btn {
padding: 10px 20px; /* More horizontal than vertical */
}
/* Input padding */
.input {
padding: 8px 12px; /* Comfortable input padding */
}