CSS: CSS Borders & Border Radius

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: Gray🏷 Category: CSS Tutorial

Borders and border-radius are important properties for giving elements "shape." Borders add outline lines to elements, while border-radius softens sharp corners.

Border Properties Quick Reference

Property Common Values Purpose
border-width 1px, 2px, medium, thick Border width
border-style solid, dashed, dotted, double, none Border style
border-color #333, red, transparent Border color
border 2px solid #333 Shorthand (width+style+color)
border-top/right/bottom/left 1px dashed red Individual side borders
border-radius 4px, 50%, 10px 20px Corner radius
border-collapse collapse, separate Table border collapse mode

Properties in this section: border-width border-style border-color border border-radius outline-offset

1. Border Property

(1) Three Elements of a Border

Every border has three elements: width, style, and color.

▶ Example

▶ Try it Yourself

(2) Border Style Values

▶ Example

▶ Try it Yourself

2. Border Radius

border-radius rounds element corners:

▶ Example

▶ Try it Yourself

(1) Common Uses

Round avatar:

HTML
<!-- Style tag -->
<!-- Style tag -->
<!-- Style tag -->
<!-- Style tag -->
<style>
.avatar {
  border-radius: 50%;
  width: 100px;
  height: 100px;
  overflow: hidden;
}
</style>
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<div class="avatar">Avatar</div>

Capsule button:

HTML
<!-- Style tag -->
<!-- Style tag -->
<!-- Style tag -->
<!-- Style tag -->
<style>
.pill {
  border-radius: 999px;
  padding: 8px 24px;
  background: #3498db;
  color: white;
}
</style>
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<div class="pill">Capsule button</div>

Properties in this section: border-width border-style border-color border border-radius outline-offset

❓ FAQ

Q How do I make a circular avatar with border-radius?
A Pair border-radius: 50% with a square size and add object-fit: cover to prevent distortion: .avatar { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; }. The object-fit is mandatory when the source image isn't square, otherwise it gets squashed.
Q What's the difference between border-radius: 50% and 999px?
A 50% is relative to element dimensions — square becomes circle, rectangle becomes ellipse. 999px is a large fixed value — as long as it exceeds half the dimensions, all corners become a fully rounded "capsule" shape. Same effect for squares, different for rectangles.
Q How do I make a pill button (fully rounded ends)?
A Just give the radius a number big enough: border-radius: 999px and the element becomes a capsule automatically. Browsers clamp radii that exceed the available half-height, so no exact math needed. Material Design buttons work exactly this way.
Q How do I make a gradient border? Border doesn't accept gradient.
A border-color can't take a gradient. The standard trick is dual-area background: border: 2px solid transparent; background: linear-gradient(#fff, #fff) padding-box, linear-gradient(red, blue) border-box; — inner layer fills the content, outer layer fills the gradient, and the border shows the gradient. border-image: linear-gradient(red, blue) 1; is shorter but ignores border-radius, so for rounded corners use the background approach.
Q Can I adjust the spacing of a dashed border? Mine is too dense.
A border: 1px dashed spacing is not adjustable — the browser decides. Two workarounds: SVG stroke with stroke-dasharray for exact control of dash length and gaps, or fake it with a repeating-linear-gradient background. Switching solid to dashed is just border-style: dashed, but fine-tuning the rhythm is beyond what border offers.
Q border-radius is set but the image still shows sharp corners — why?
A Most common cause: the radius is on the outer div but the img overflows it — add overflow: hidden to the div, or put the radius directly on the img. Also check in DevTools whether a higher-specificity rule overrode the radius to 0.
Q How to set radius for individual corners?
A Use border-top-left-radius, border-top-right-radius, border-bottom-right-radius, border-bottom-left-radius for individual corners. Or shorthand: border-radius: top-left top-right bottom-right bottom-left; — e.g. rounded top, square bottom: border-radius: 12px 12px 0 0;.
Q Can outline and border be used together?
A Yes. outline doesn't occupy space (doesn't affect element size), border does. outline is commonly used with :focus state to provide focus indicators for keyboard users. Use outline-offset to control distance between outline and element boundary.
Q Can I set border width, color, and style separately? How?
A Yes — border: 1px solid #333 expands to border-width: 1px; border-style: solid; border-color: #333;. Separating them lets you change one dimension at a time; a hover color change only needs transition: border-color .2s. Note border-style must have a value (default none), otherwise width and color render nothing.

📖 Summary

📝 Exercises

  1. Create a card with border and border-radius to look like a "business card" — with border, rounded corners, and padding.
  2. Use border-radius: 50% to create a circular avatar frame.
  3. Use different border combinations to create a menu item with "underline" (only bottom border).
  4. Try dashed and dotted border styles to see their visual differences.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏