How to Add a Border in CSS
Use the border property when you want to outline an element and make its edges more visible. Borders help separate cards, buttons, forms, alerts, images, and layout sections from surrounding content.
What you’ll build or solve
You’ll learn how to add a border in CSS using the most common shorthand syntax. You’ll also know how to control border width, style, and color.
Learn CSS on Mimo
When this approach works best
This approach is the right choice when an element needs stronger visual separation or a clearer interactive state.
Common real-world scenarios include:
- Card outlines
- Button borders
- Input field styling
- Alert boxes
- Image frames
This is a bad idea when whitespace or background color already creates enough separation and the design starts to feel visually heavy.
Prerequisites
You only need:
- A basic HTML file
- A text editor
- Basic HTML and CSS knowledge
Step-by-step instructions
Step 1: Add the border shorthand property
Use the border shorthand in this order: width, style, color.
HTML
<div class="card">USA pricing</div>
<style>
.card {
border: 1px solid black;
}
</style>
For more control, switch to side-specific borders.
HTML
<h2 class="section-title">France pricing</h2>
<style>
.section-title {
border-bottom: 2px solid #333;
}
</style>
What to look for:
1px solid blackis the most common starting pointsolid,dashed, anddottedare common styles- Side-specific borders work well for dividers
- Borders increase visual weight, so keep them intentional
- Add padding if the content feels too close to the border
Examples you can copy
Card border
HTML
<div class="card">UK course plan</div>
<style>
.card {
border: 1px solid #ddd;
}
</style>
Button outline
HTML
<button class="cta">Start lesson</button>
<style>
.cta {
border: 2px solid #000;
}
</style>
Input field border
HTML
<input class="email-input" placeholder="USA email">
<style>
.email-input {
border: 1px solid #999;
}
</style>
Common mistakes and how to fix them
Mistake 1: Forgetting the border style
What the reader might do:
HTML
.card {
border: 1px black;
}
Why it breaks: the border needs a style like solid, or it will not render correctly.
Corrected approach:
HTML
.card {
border: 1px solid black;
}
Mistake 2: Using borders for spacing
What the reader might do:
HTML
.card {
border-bottom: 20px solid white;
}
Why it breaks: borders should define edges, not create layout spacing.
Corrected approach:
HTML
.card {
margin-bottom: 20px;
}
Use margin for spacing between separate blocks.
Mistake 3: Adding a border without inner spacing
What the reader might do:
HTML
.card {
border: 1px solid black;
}
Why it breaks: the text may sit too close to the edge.
Corrected approach:
HTML
.card {
border: 1px solid black;
padding: 16px;
}
Troubleshooting
If the border does not appear, confirm you included the style keyword like solid.
If the border feels too strong, reduce the width or use a lighter color.
If the content touches the edge, add padding.
If the design feels crowded, replace some borders with whitespace.
Quick recap
- Use
borderto outline elements - Follow width, style, color order
- Use side-specific borders for dividers
- Add padding when needed
- Use margin for spacing, not borders
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot