Skip to main content

How to Round the Corners of an Element with border-radius in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

border-radius rounds the corners of an element. A small value softens them, a bigger value makes a pill, and 50% on a square makes a circle.

Syntax​

Syntax
<selector> {
border-radius: <radius>;
}
PartWhat it does
<selector>The element to round.
<radius>How round the corners are, such as 4px. Four values set the corners one by one, starting at the top left and going clockwise.

MDN's border-radius page shows every form of the value, including elliptical corners.

The Example​

The example is three blue bars: one with square corners, one with border-radius: 4px and one with border-radius: 16px.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element giving every paragraph a 0f6cbd background, white text and 8px of padding, a class small with border-radius 4px and a class large with border-radius 16px, and three paragraphs, the second small and the third large.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Border radius</title>
<style>
p {
background: #0f6cbd;
color: #ffffff;
padding: 8px;
}

.small {
border-radius: 4px;
}

.large {
border-radius: 16px;
}
</style>
</head>
<body>
<p>Square corners</p>
<p class="small">border-radius: 4px</p>
<p class="large">border-radius: 16px</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: three blue bars, Square corners with sharp corners, border-radius: 4px with slightly rounded corners, and border-radius: 16px with fully rounded ends.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The corners have radii of 0px, 4px and 16px. The first bar has no rule, and 0 is the default.
  • The 16-pixel corners look like round ends, because the bar is about as tall as two radii.
  • The background follows the rounded corners, so the blue is cut off at each curve.

Common Mistakes​

MistakeWhat happensDo this instead
Rounding an element with no background or borderNothing visible changes: a paragraph with border-radius: 16px and no background looked exactly the sameGive it a background or a border
Expecting 50% to make a circle on any elementOn a box that isn't square, it makes an oval: a 160 by 60 box became an ellipseMake the box square first

Troubleshooting​

SymptomLikely causeFix
The corners look squareThe element has no background or border to show the curveAdd a background or a border
A circle comes out as an ovalThe width and height differGive it the same width and height

FAQ​

How do I make a circle?​

Give a square box border-radius: 50%. A 60 by 60 box with a blue background became a blue circle.

How do I round only one corner?​

Use four values, top left first, going clockwise: border-radius: 16px 0 0 0 rounded only the top-left corner, and left the other three at 0px.

Conclusion​

Set border-radius to round an element's corners: a few pixels for soft corners, half the height for round ends, and 50% on a square for a circle.

Additional Resources​