CSS Borders
A border is the line drawn around an element's padding and content. Borders frame cards, separate sections, underline headings and draw attention to buttons. Every border has three ingredients: a width, a style and a color.
Border style
border-style is the one ingredient you cannot skip — its default is none, so without it nothing appears no matter what width or color you set.
| Value | Look |
|---|---|
solid |
A single continuous line |
dashed |
Short dashes |
dotted |
Round dots |
double |
Two parallel lines |
groove / ridge |
Carved-in / raised 3D effect |
inset / outset |
Sunken / embossed 3D effect |
none / hidden |
No border |
<!DOCTYPE html>
<html>
<head>
<style>
body { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; font: 14px sans-serif; }
div { border-width: 6px; border-color: #264de4; padding: 14px 4px; text-align: center; }
</style>
</head>
<body>
<div style="border-style: solid">solid</div>
<div style="border-style: dashed">dashed</div>
<div style="border-style: dotted">dotted</div>
<div style="border-style: double">double</div>
<div style="border-style: groove">groove</div>
<div style="border-style: ridge">ridge</div>
<div style="border-style: inset">inset</div>
<div style="border-style: outset">outset</div>
</body>
</html>Width and color
border-width takes a length (2px, 0.25em) or one of the keywords thin, medium (the default) or thick. border-color takes any color; if you omit it, the border uses the element's text color (currentColor).
One value per side
border-style, border-width and border-color all accept one to four values, which map to the sides in clockwise order starting at the top:
| Values | Meaning |
|---|---|
a |
all four sides |
a b |
top/bottom = a, left/right = b |
a b c |
top = a, left/right = b, bottom = c |
a b c d |
top, right, bottom, left |
<!DOCTYPE html>
<html>
<head>
<style>
body { font: 16px sans-serif; }
.box {
width: 220px; padding: 20px; margin: 20px auto; text-align: center;
border-style: solid;
border-width: 4px 12px 20px 2px;
border-color: crimson orange seagreen royalblue;
}
</style>
</head>
<body>
<div class="box">top / right / bottom / left</div>
</body>
</html>The border shorthand
Most of the time you write all three ingredients at once. The order does not matter, but width style color is the convention:
.card { border: 2px solid #ddd; }To style just one side, use border-top, border-right, border-bottom or border-left. A popular trick is a colored accent bar on one side:
<!DOCTYPE html>
<html>
<head>
<style>
body { font: 16px sans-serif; padding: 10px; }
.alert {
border: 1px solid #ccc;
border-left: 6px solid #e67e22;
background: #fff8ef;
padding: 12px 16px;
margin-bottom: 12px;
}
h2 {
border-bottom: 3px dotted #264de4;
padding-bottom: 4px;
}
.none { border: 1px solid #ccc; border-top: none; padding: 10px; }
</style>
</head>
<body>
<h2>Dotted underline heading</h2>
<div class="alert">Heads up! This alert has a thick left accent.</div>
<div class="none">This box has every border except the top.</div>
</body>
</html>Notice the order: in .alert, border sets all four sides first and then border-left overrides just one. If you swapped those lines, the shorthand would overwrite the accent.
Rounded borders
border-radius rounds the corners. It works with or without a visible border, so it is also used to round backgrounds and images. A radius of 50% on a square makes a circle.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: flex; gap: 16px; align-items: center; justify-content: center; height: 90vh; font: 14px sans-serif; }
div { width: 100px; height: 100px; border: 4px solid #264de4; display: grid; place-items: center; background: #eef2ff; }
.a { border-radius: 8px; }
.b { border-radius: 30px; }
.c { border-radius: 50%; }
.d { border-radius: 40px 0; }
</style>
</head>
<body>
<div class="a">8px</div>
<div class="b">30px</div>
<div class="c">50%</div>
<div class="d">40px 0</div>
</body>
</html>You will explore radii further in the Rounded Corners lesson.
Tip: Borders add to an element's size. A 200px-wide box with a 5px border is 210px wide in total — unless you use
box-sizing: border-box, covered in the Box Sizing lesson.
Test yourself
Why does border-width: 3px; border-color: red; show no border?
- Borders need a radius
border-styledefaults tonone- 3px is too thin
- Color must be in hex
In border-width: 1px 2px 3px 4px;, which side gets 2px?
- Top
- Right
- Bottom
- Left
Which declaration draws a line only under an element?
border: bottom 2px solid;border-bottom: 2px solid black;border-style: underline;border: 0 0 2px;