EthicsComputerCourses
Join training +91 90532 10052

Border Radius

CSS · Lesson 38 of 240 Beginner 10 min read Updated

By the end of this lesson you will be able to round any corner of a box exactly the way a design asks: all four corners at once, each corner differently, perfect circles and pill-shaped buttons, and stretched oval corners with the slash syntax. You will also learn why a child element sometimes pokes out of a rounded card, and the two ways to fix it.

CSS Borders & Outlines showed the basics of border-radius. This lesson explains how the values really work, so you can build any shape instead of copying numbers.

What border-radius does

Every box in CSS starts with sharp, square corners. border-radius replaces each corner with a curve. Imagine placing a coin in the corner of a square card and drawing around its edge. The radius is the size of that coin: a bigger coin gives a softer, wider curve.

The curve applies to the whole box: the background, the border and any box-shadow all follow it. You do not need a border for it to work. A plain coloured box gets round corners too.

CSS
.card  { border-radius: 12px; }   /* gentle rounding */
.badge { border-radius: 4px; }    /* barely rounded */
.photo { border-radius: 50%; }    /* circle or oval */

Negative values are invalid, and 0 gives a sharp corner.

One to four values

The border-radius shorthand takes one to four values. Like margin and padding, they go clockwise, starting at the top-left corner.

ValuesExampleCorners
One16pxAll four corners
Two16px 4pxTop-left and bottom-right get 16px; top-right and bottom-left get 4px
Three16px 4px 0Top-left 16px; top-right and bottom-left 4px; bottom-right 0
Four16px 4px 0 8pxTop-left, top-right, bottom-right, bottom-left

With two values, the corners are paired diagonally, which surprises many beginners. Picture an "X" drawn across the box: the first value takes one diagonal, the second takes the other.

Step 1: see every pattern

Each box below uses a different number of values. Run it and match each shape to the table.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>One to four values</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; display: flex; flex-wrap: wrap; gap: 1rem; }
    .box {
      width: 9rem;
      height: 6rem;
      background: rgb(13 148 136);
      color: white;
      display: grid;
      place-content: center;
      font-family: ui-monospace, monospace;
      font-size: 0.85rem;
    }
    .one   { border-radius: 24px; }
    .two   { border-radius: 24px 0; }
    .three { border-radius: 24px 0 8px; }
    .four  { border-radius: 24px 0 24px 0; }
    .tab   { border-radius: 16px 16px 0 0; }
    .bubble { border-radius: 20px 20px 20px 0; }
  </style>
</head>
<body>
  <div class="box one">24px</div>
  <div class="box two">24px 0</div>
  <div class="box three">24px 0 8px</div>
  <div class="box four">24px 0 24px 0</div>
  <div class="box tab">16px 16px 0 0</div>
  <div class="box bubble">20px 20px 20px 0</div>
</body>
</html>

You should see a fully rounded box, two "leaf" shapes with opposite corners rounded, a tab rounded only on top, and a chat bubble with one sharp corner. Notice that 24px 0 and 24px 0 24px 0 look the same, because two values already mean "repeat across the diagonal".

Percentages, circles and pills

A percentage radius is measured against the box's own size. The horizontal part of the curve uses the box width, and the vertical part uses the box height.

  • On a square, border-radius: 50% makes a perfect circle, because width and height are equal.
  • On a rectangle, 50% makes an ellipse (an oval), because the curve is wider than it is tall.

So what about a pill, a rectangle with round ends, like most "Add to cart" buttons? For that you want each end to be a half circle. The trick is a very large length, such as 999px.

Why does a huge value not break the box? When the curves on one side add up to more than the length of that side, the browser shrinks all the radii by the same factor until they fit. On a 40px tall button, the two curves on the left side ask for 999px + 999px. The browser scales them down until they add up to exactly 40px, so each one becomes 20px, half the height. That gives perfect round ends at any button width.

Step 2: circle, ellipse and pill

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Circles and pills</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: center; }
    .avatar { width: 6rem; height: 6rem; border-radius: 50%; border: 3px solid rgb(234 88 12); }
    .oval { width: 10rem; height: 6rem; border-radius: 50%; background: rgb(254 215 170); display: grid; place-content: center; }
    .btn { border: none; padding: 0.7rem 1.4rem; font: inherit; color: white; background: rgb(234 88 12); }
    .pill { border-radius: 999px; }
    .half { border-radius: 50%; }
    .dot { width: 0.75rem; height: 0.75rem; border-radius: 50%; background: rgb(22 163 74); display: inline-block; }
  </style>
</head>
<body>
  <img class="avatar" src="https://course.ethicscomputer.in/assets/samples/avatar.svg" alt="Photo of Priya">
  <div class="oval">50% on a rectangle</div>
  <button class="btn pill">Order samosa, ₹15</button>
  <button class="btn half">Order samosa, ₹15</button>
  <p><span class="dot"></span> Shop open</p>
</body>
</html>

The square photo becomes a circle and the rectangle becomes an oval. Compare the two buttons: 999px gives clean round ends, while 50% turns the button into an oval with no straight edges. The small green dot is a circle made from an empty square, a common "online" or "open now" indicator.

Elliptical corners with the slash

So far each corner was a quarter of a circle. A corner can also be a quarter of an ellipse, with a different horizontal and vertical radius. You write the horizontal radii, then a slash /, then the vertical radii.

CSS
.stretch { border-radius: 40px / 16px; }          /* wide, shallow curves */
.egg     { border-radius: 50% / 60% 60% 40% 40%; } /* egg shape */

Each side of the slash follows the same one-to-four rule as before. In the egg, all corners are 50% wide, but the top corners are 60% tall and the bottom corners are only 40% tall, so the top is narrower and the bottom is fuller.

Per-corner properties

You can also set one corner at a time with four longhand properties:

  • border-top-left-radius
  • border-top-right-radius
  • border-bottom-right-radius
  • border-bottom-left-radius

Each takes one value for a circular corner, or two values separated by a space (not a slash) for an elliptical one: horizontal first, then vertical.

CSS
.ticket { border-radius: 12px; }
.ticket.featured { border-top-right-radius: 0; }       /* override one corner */
.wave { border-bottom-left-radius: 80px 30px; }        /* elliptical corner */

Longhands are useful for overriding a single corner without repeating the other three. There are also logical versions such as border-start-start-radius, which follow the writing direction of the language. They are covered in Logical Properties.

Clipping children inside a rounded box

Here is a bug every developer meets. You round a card and put an image at its top. The card's background is rounded, but the image corners stay sharp and stick out of the curve.

This happens because border-radius shapes the card's own background and border. Its children are drawn on top, and by default they are allowed to overflow. To clip them to the curve, give the parent overflow: hidden, or the newer overflow: clip (Baseline 2022), which clips the same way but never creates a scroll area. You will study both in CSS Overflow.

Step 3: fix the poking corners

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Clipping children</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; background: rgb(241 245 249); display: flex; flex-wrap: wrap; gap: 1.5rem; }
    .card { width: 15rem; background: white; border-radius: 20px; border: 2px solid rgb(203 213 225); }
    .card img { display: block; width: 100%; }
    .card p { padding: 0 1rem; }
    .fixed { overflow: clip; }
    .outer { background: rgb(30 64 175); padding: 8px; border-radius: 24px; }
    .inner { background: white; padding: 1rem; border-radius: 16px; }
  </style>
</head>
<body>
  <div class="card">
    <img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Hills near Munnar">
    <p>Without clipping: the image corners stick out.</p>
  </div>
  <div class="card fixed">
    <img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Hills near Munnar">
    <p>With overflow: clip, the image follows the curve.</p>
  </div>
  <div class="outer"><div class="inner">Outer 24px minus 8px padding gives inner 16px.</div></div>
</body>
</html>

In the first card the image's top corners poke through the rounded border. In the second, one line of CSS clips them neatly.

The third box shows a professional detail. When you nest one rounded box inside another, matching the radii makes the curves look uneven. For curves that run parallel, the inner radius should be the outer radius minus the gap between them: here 24px minus 8px of padding gives 16px.

Common mistakes

  • Expecting 50% to make a pill. On a rectangle it makes an ellipse. Use a large length like 999px.
  • Expecting a circle from a rectangle. 50% gives a circle only when width and height are equal.
  • Children poking out of the corners. Add overflow: clip or overflow: hidden to the rounded parent.
  • Using a slash in a longhand. border-top-left-radius: 20px / 10px is invalid. Longhands use a space.
  • Same radius on nested boxes. Subtract the gap from the outer radius for the inner one.

Interview questions

1. How are the corners assigned in border-radius: 10px 20px 30px?

With three values, the first is the top-left corner, the second is shared by top-right and bottom-left, and the third is bottom-right. The values always run clockwise from the top-left, and missing corners copy their diagonal opposite.

2. Why does border-radius: 50% make an oval on a rectangle?

A percentage radius uses the box width for the horizontal part of each curve and the height for the vertical part. On a rectangle those differ, so each corner becomes a quarter of an ellipse. Only a square gives a circle.

3. Why does border-radius: 9999px create a pill shape instead of something broken?

When the radii on any side add up to more than that side's length, the browser scales all radii down by the same factor until they fit. The shortest side wins, so on a wide button each radius becomes half the height, giving round ends.

4. What does the slash mean in border-radius: 40px / 20px?

Values before the slash are horizontal radii and values after it are vertical radii. Here every corner is a quarter ellipse 40px wide and 20px tall. Each side of the slash can have one to four values.

5. An image inside a rounded card has sharp corners. How do you fix it?

The card's radius does not clip its children by default. Add overflow: hidden or overflow: clip to the card so its content is clipped to the curve. Alternatively, give the image matching top radii itself.

Key points

  • border-radius rounds the background, border and shadow, even without a border.
  • One to four values run clockwise from the top-left; two values pair the diagonals.
  • Percentages use width for horizontal curves and height for vertical ones: 50% gives circles and ovals.
  • A large length like 999px makes a pill, because oversized radii are scaled down to fit.
  • The slash sets elliptical corners; longhands like border-top-left-radius use a space instead.
  • Rounded parents do not clip children unless you add overflow: clip or overflow: hidden.

Exercise

Design a product card for Farhan's Bakery in Hyderabad. The card needs rounded corners, an image at the top that follows those corners, a circular "Bestseller" badge, and a pill-shaped "Add to cart" button. Then add a speech-bubble review below the card with one sharp bottom-left corner, and give a coupon box elliptical corners using the slash syntax.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Farhan's Bakery</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; background: rgb(255 247 237); }
    .card { width: 16rem; background: white; border-radius: 18px; overflow: clip; border: 1px solid rgb(253 186 116); }
    .card img { display: block; width: 100%; }
    .body { padding: 1rem; }
    .badge { width: 4.5rem; height: 4.5rem; border-radius: 50%; background: rgb(194 65 12); color: white; display: grid; place-content: center; font-size: 0.75rem; }
    .btn { border: none; border-radius: 999px; padding: 0.6rem 1.2rem; background: rgb(194 65 12); color: white; font: inherit; }
    .review { max-width: 16rem; margin-top: 1rem; padding: 0.8rem 1rem; background: rgb(254 215 170); border-radius: 18px 18px 18px 0; }
    .coupon { max-width: 16rem; margin-top: 1rem; padding: 1rem; border: 2px dashed rgb(194 65 12); border-radius: 40px / 16px; text-align: center; }
  </style>
</head>
<body>
  <div class="card">
    <img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Fresh Osmania biscuits">
    <div class="body">
      <div class="badge">Bestseller</div>
      <h2>Osmania Biscuits</h2>
      <p>₹180 for 500 g</p>
      <button class="btn">Add to cart</button>
    </div>
  </div>
  <p class="review">"Best biscuits with Irani chai!" Ananya</p>
  <p class="coupon">Use code CHAI20 for 20% off</p>
</body>
</html>

overflow: clip makes the image follow the card's corners, 50% on a square makes the badge round, and 999px gives the button its pill ends. The review uses four values with 0 for the bottom-left corner, and the coupon uses the slash for wide, shallow curves.

Quick Quiz

1. Which corners get 20px in border-radius: 20px 0;?

2. A button is 200px wide and 40px tall. What does border-radius: 50% produce?

3. What does border-radius: 30px / 10px create?

4. A rounded card's image has sharp corners that stick out. What fixes it?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call