- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Border
CSS Box Model
CSS Border
Build borders with width, style, color, radius, logical sides, and shorthands while accounting for their effect on box dimensions.
A border is the line around an element's box. It needs three things: a width, a style and a color. Leave out the style and nothing appears, which is the most common reason a border "does not work".
Borders sit between the padding and the margin. They are related to CSS Outline, which draws outside the box without taking space, and to CSS Shadows for softer edges.
Syntax
css
selector {
border: width style color;
}Example
css
p {
border: 2px solid red;
}That draws a 2px solid red line around every paragraph.
The parts of a border
| Property | What it sets | Example |
|---|---|---|
| border | Width, style and color at once | border: 1px solid black; |
| border-width | Thickness | border-width: 2px; |
| border-style | The line type: solid, dashed, dotted, double, none | border-style: dashed; |
| border-color | Color; if missing, the text color is used | border-color: red; |
| border-radius | Rounded corners | border-radius: 10px; |
border-style has no visible default; it starts as none. That is why border: 2px red; shows nothing, while border: 2px solid; shows a line in the current text color.
One side at a time
Use border-top, border-right, border-bottom or border-left to style just one side:
A line under headings only
css
h2 {
border-bottom: 2px solid #e2e8f0;
padding-bottom: 0.5rem;
}For pages that may be shown in right-to-left languages, logical properties such as border-inline-start follow the text direction.
Borders add to the size
With the default box-sizing: content-box, a 200px wide box with a 2px border is really 204px wide, and that extra width can break a layout. Most sites set box-sizing: border-box on everything, so the border fits inside the width you set.
Example
Complete CSS Border Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Border Example</title>
<style>
div {
border: 2px solid blue;
padding: 10px;
}
p {
border: 1px dashed red;
padding: 10px;
}
h1 {
border: 3px solid black;
border-radius: 10px;
padding: 10px;
}
</style>
</head>
<body>
<h1>Border Example</h1>
<div>This is a div with solid border.</div>
<p>This paragraph has dashed border.</p>
</body>
</html>You will see three boxes: a blue solid border around the div, a red dashed one around the paragraph, and a thick black one with rounded corners around the heading.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| border | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- A transparent border still takes up space. That is handy for keeping a box the same size when a colored border appears on hover.
- Rounded corners work even without a visible border;
border-radiusalso rounds the background. - Do not rely on a border change alone to show keyboard focus. Make the focus state clearly different, usually with an outline.
CSS Border exercises
A solid border
Give the box a border of 2px solid black.
The HTML your code works on
<div class="box">Box</div>
<img class="avatar" src="https://picsum.photos/id/237/200/200.jpg" alt="Dog">Result
