CSS Padding
Learn how to control the space between content and borders with CSS padding
Understanding CSS Padding
CSS padding creates space inside elements, between the content and the border. It's essential for improving readability and creating well-spaced layouts.
Key Concepts:
- Padding Area: Space between content and border
- Box Model: Part of the CSS box model
- Box Sizing: Affects total element size
- Individual Sides: Control padding on each side
1. Basic Padding Properties
/* Individual padding properties */
.element {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
/* Shorthand padding property */
.element {
/* All sides */
padding: 10px;
/* vertical | horizontal */
padding: 10px 20px;
/* top | horizontal | bottom */
padding: 10px 20px 15px;
/* top | right | bottom | left */
padding: 10px 20px 15px 25px;
}
2. Padding Units
/* Different units for padding */
.padding-units {
/* Pixels */
padding: 20px;
/* Em - relative to font size */
padding: 1.5em;
/* Rem - relative to root font size */
padding: 1.5rem;
/* Percentage - relative to container width */
padding: 5%;
/* Viewport units */
padding: 2vw;
}
3. Box Sizing and Padding
/* Content-box (default) */
.content-box {
box-sizing: content-box;
width: 200px;
padding: 20px;
/* Total width = 200px + 20px + 20px = 240px */
}
/* Border-box */
.border-box {
box-sizing: border-box;
width: 200px;
padding: 20px;
/* Total width = 200px (padding included) */
}
4. Responsive Padding
/* Responsive padding using media queries */
.responsive-element {
padding: 15px; /* Default for mobile */
}
@media screen and (min-width: 768px) {
.responsive-element {
padding: 20px; /* Larger padding on tablets */
}
}
@media screen and (min-width: 1024px) {
.responsive-element {
padding: 30px; /* Even larger padding on desktop */
}
}
/* Using clamp for fluid padding */
.fluid-padding {
padding: clamp(1rem, 5vw, 3rem);
}
5. Common Use Cases
Button Padding
.button {
/* Horizontal padding larger than vertical */
padding: 10px 20px;
/* Small button */
&.small {
padding: 5px 10px;
}
/* Large button */
&.large {
padding: 15px 30px;
}
}
Card Component
.card {
padding: 20px;
/* Header section */
.card-header {
padding-bottom: 15px;
}
/* Content section */
.card-content {
padding: 15px 0;
}
/* Footer section */
.card-footer {
padding-top: 15px;
}
}
Navigation Menu
.nav {
padding: 10px 20px;
.nav-item {
padding: 8px 15px;
/* Responsive padding */
@media (max-width: 768px) {
padding: 12px 10px;
}
}
}
6. Practical Examples
Form Elements
/* Form styling with padding */
.form {
padding: 20px;
.form-group {
padding-bottom: 15px;
}
.input {
padding: 10px 12px;
}
.textarea {
padding: 12px;
}
.select {
padding: 10px 30px 10px 12px;
}
}
Content Containers
/* Content container padding */
.container {
padding: 0 20px;
max-width: 1200px;
margin: 0 auto;
@media (min-width: 768px) {
padding: 0 40px;
}
}
.section {
padding: 40px 0;
@media (min-width: 768px) {
padding: 60px 0;
}
@media (min-width: 1024px) {
padding: 80px 0;
}
}
List Styling
/* List padding examples */
.list {
padding-left: 20px;
.list-item {
padding: 10px 0;
}
}
.custom-list {
padding: 0;
list-style: none;
.list-item {
padding: 12px 15px;
border-bottom: 1px solid #eee;
&:last-child {
border-bottom: none;
}
}
}
Best Practices
Padding Guidelines:
- Use consistent spacing: Maintain a consistent padding scale
- Consider box-sizing: Use border-box for predictable sizing
- Be responsive: Adjust padding for different screen sizes
- Use relative units: Consider em/rem for scalable designs
- Maintain hierarchy: Use padding to create visual hierarchy
- Test thoroughly: Check padding on different devices
Common Issues
Troubleshooting:
- Unexpected sizes: Check box-sizing property
- Overflow issues: Ensure content fits with padding
- Mobile layout: Test padding on small screens
- Inconsistent spacing: Use a consistent padding scale
- Performance: Avoid large padding changes on scroll