Border Radius: Rounded Corners, Pills, and Circular Avatars
In the early days of the web, every single box, button, and image had sharp, 90-degree razor corners like an old iron trunk box. 🗃️
Today, look at your smartphone screen, YouTube cards, Instagram buttons, and school ID badges: almost everything has soft, friendly, and elegant curved corners.
In CSS, the property that rounds off corners is called border-radius.
In this lesson, you will master:
1
How border-radius works under the hood
2
The 3 most popular UI patterns: Cards, Pill Buttons, and Circular Avatars
3
Styling individual corners for creative shapes (like speech bubbles and leaves)
4
Why overflow: hidden is essential when rounding card images
The Cricket Ball & Compass Analogy 🏏
How does the browser calculate a rounded corner?
Imagine taking a geometry compass, placing it inside the corner of a square piece of cardboard, and drawing a circular arc of radius R:
1 SHARP CORNER (0px) ROUNDED CORNER (16px)
2
3 +─────────────── ╭───────────────
4 │ / (Radius: 16px)
5 │ │
6 │ │
When border-radius: 0; → The corner is a sharp 90-degree point.
When you increase the radius (e.g., 8px, 16px, 24px) → The corner curves more gently!
Pro Tip
Fun Fact: You do not need a visible border for border-radius to work! Even if an element has no border, border-radius curves the background color and clipping boundary of the element.
Pattern 1: Subtle Modern Card Corners (8px to 16px)
The most common use of border-radius in modern web design is giving cards, modals, and text inputs a soft, welcoming feel:
1.card{
2background-color: white;
3padding: 24px;
4border: 1px solid #e2e8f0;
5border-radius: 12px;/* Soft, modern curve */
6}
7
8input[type="text"]{
9border: 1px solid #cbd5e1;
10border-radius: 6px;/* Subtle curve for inputs */
11padding: 8px 12px;
12}
Pattern 2: The Pill Button (Capsule Shape ⭐)
Have you seen pill-shaped buttons on YouTube, Spotify, or Netflix with completely semi-circular ends?
1.pill-btn{
2background-color: #2563eb;
3color: white;
4padding: 12px 30px;
5border: none;
6border-radius: 9999px;/* Or 50px */
7cursor: pointer;
8font-weight: bold;
9}
Why 9999px?
If you give border-radius a number much larger than the height of the button (like 50px or 9999px), the browser automatically caps the curve to a perfect semicircle on both ends, creating an indestructible pill shape regardless of how wide the button stretches!
Pattern 3: The Perfect Circular Avatar (50% ⭐)
How do platforms like Google, WhatsApp, and GitHub make student profile pictures display as perfect circles?
The Two Golden Rules for Perfect Circles:
1
The element must have equal width and height (a perfect square: width: 80px; height: 80px;).
2
Set border-radius: 50%;.
1.avatar-img{
2width: 90px;
3height: 90px;
4border-radius: 50%;/* Makes a perfect square into a perfect circle! */
5object-fit: cover;/* Prevents the student's photo from stretching */
6border: 3px solid #3b82f6;
7}
⚠️ Common Beginner Trap: The Oval / Egg Shape!
If your image is width: 120px and height: 80px (a rectangle), setting border-radius: 50% will turn it into an oval or egg shape, NOT a circle! The width and height must be strictly equal.
10border-radius: 16px 16px 16px 0;/* Bottom-left stays sharp like a tail! */
11}
⚠️ The Card Image Bleed Bug (and How to Fix It!)
Have you ever created a card with border-radius: 16px, placed an image at the top of the card, and noticed that the image's square corners poke out past the card's rounded border?
75 "Leading with empathy, excellence, and dedication to our school community."
76</div>
77</div>
78</body>
79</html>
1
Open the file in your browser to admire how the card, circle, pill, and speech bubble all demonstrate the versatility of border-radius! 🎯
🚀 What's Next?
In the next lesson, we will continue your web styling journey with Box Shadows and Text Shadows: Depth and Elevation (Borders, Shadows and Rounded Corners).