Borders and Shadows
Borders, rounded corners and shadows turn flat boxes into cards, buttons and avatars. A few lines of CSS make a big visual difference.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Learning objectives#
- Write border shorthand
- Round corners and make circles with border-radius
- Add depth with box-shadow
- Know the difference between border and outline
Explanation#
border: width style color — for example border: 2px solid #ccc. Styles include solid, dashed, dotted and double. You can set one side only: border-bottom.
border-radius rounds corners. 8px gives a soft card; 50% on a square makes a circle; 999px makes a pill-shaped button.
box-shadow: x y blur spread color. 0 4px 12px rgb(0 0 0 / 15%) is a soft shadow below the box. Multiple shadows can be separated with commas.
outline looks like a border but takes up no space and does not affect layout. Browsers use it for keyboard focus rings.
Syntax#
border: 1px solid #ddd;
border-radius: 12px;
box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);
outline: 3px solid blue;
Code example: Card, avatar and buttons#
<style>
body { font-family: sans-serif; background: #f8fafc; padding: 20px; }
.card {
max-width: 260px; padding: 20px; background: white;
border: 1px solid #e2e8f0;
border-radius: 12px;
box-shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 8px 24px rgb(0 0 0 / 8%);
}
.avatar { width: 64px; height: 64px; border-radius: 50%; border: 3px solid #2965f1; object-fit: cover; }
.pill { padding: 8px 18px; border: 0; border-radius: 999px; background: #2965f1; color: white; }
.ghost { padding: 8px 18px; border: 2px dashed #94a3b8; border-radius: 8px; background: none; }
button:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
h3 { text-shadow: 0 1px 1px rgb(0 0 0 / 15%); }
</style>
<div class="card">
<img class="avatar" src="https://i.pravatar.cc/128?img=12" alt="Profile photo of Rahul">
<h3>Rahul Mehta</h3>
<button class="pill">Follow</button>
<button class="ghost">Message</button>
</div>
A white rounded card with a soft shadow, a circular avatar with a blue ring, a pill-shaped button and a dashed outline button. Tab to see an orange focus ring.
Step-by-step explanation#
- The card has a light border, rounded corners and two layered shadows for a natural look.
- border-radius: 50% turns the square avatar into a circle; object-fit: cover prevents stretching.
- border-radius: 999px makes a fully rounded pill.
- The ghost button uses a dashed border with no background.
- outline with outline-offset draws a focus ring away from the button without moving anything.
Real-world use case#
Almost every modern UI — product cards, profile pictures, chat bubbles, buttons — is built from border, border-radius and box-shadow.
Common mistakes#
box-shadow: 5px 5px black looks dated. Use larger blur, a small y offset and low-opacity colour.
Adding a 2px border on hover makes the element grow and nudges its neighbours. Reserve the border (transparent) beforehand, or use outline or box-shadow.
outline: none on buttons hides keyboard focus. Restyle it instead.
Best practices#
- Keep one border-radius scale (e.g. 4px, 8px, 12px)
- Use soft, layered shadows
- Use outline for focus states
- Use box-sizing: border-box so borders do not break widths
Remember this#
border = width style colour (“2px solid red”).
50% radius on a square = circle. 999px = pill.
Outline = border that takes no space — perfect for focus rings.
Practice exercise#
Your task: Create a 120px circular image with a 4px white border and a soft shadow.
Need a hint?
Show solution
.round {
width: 120px; height: 120px;
border-radius: 50%;
border: 4px solid white;
box-shadow: 0 4px 16px rgb(0 0 0 / 0.2);
object-fit: cover;
}
Mini quiz#
How do you make a square image a circle?
Want more? Practise all CSS MCQs · CSS interview questions
My notes#
Saved in this browser.