CSS3 Borders
A border is the line drawn around an element's padding, between the padding and the margin. CSS lets you control its width, style and colour on every side, round its corners with border-radius, and even paint it with an image. Borders are used for cards, input fields, dividers, buttons and avatars, so they show up on almost every page you build.
The three parts of a border
Each border has a width, a style and a colour. You can set them separately with border-width, border-style and border-color, or together with the border shorthand in any order.
- Width: a length like
1pxor0.25rem, or the keywordsthin,medium,thick. - Style:
solid,dashed,dotted,double,groove,ridge,inset,outset,noneorhidden. - Colour: any CSS colour. If omitted, it uses the element's text colour (
currentColor).
Individual sides have their own properties: border-top, border-right, border-bottom, border-left, and longhands like border-left-color. A common pattern is a thick coloured left border to mark a note or quote.
Rounding corners with border-radius
border-radius curves the outer corners. One value rounds all four; up to four values go clockwise from top-left. A value of 50% on a square element turns it into a circle, which is how round profile pictures are made. You can create elliptical corners with a slash, such as border-radius: 40px / 20px, where the first value is the horizontal radius and the second the vertical one. The radius also clips the element's background, even if no border is drawn.
.card {
border: 1px solid #d0d7de;
border-radius: 12px;
padding: 16px;
}
.note {
border-left: 4px solid #2563eb;
padding-left: 12px;
}
.avatar {
width: 64px;
height: 64px;
border: 3px solid #fff;
border-radius: 50%;
object-fit: cover;
}
.pill {
border: 2px dashed currentColor;
border-radius: 999px; /* fully rounded ends at any width */
padding: 4px 14px;
}
.frame {
border: 12px solid transparent;
border-image: linear-gradient(45deg, #f97316, #db2777) 1;
}
Border versus outline
outline looks similar but behaves differently. An outline is drawn outside the border and does not take up any space, so adding one never moves surrounding elements. Outlines cannot have different styles per side and, in modern browsers, follow border-radius. That is why browsers use outlines for focus rings. box-shadow with no blur, like 0 0 0 2px #2563eb, is another way to draw a ring without affecting layout.
Gotchas that trip up beginners
The most common one: setting border-width and border-color but no style. The default style is none, so nothing appears. Always include a style, which the border shorthand makes easy.
Next, borders add to an element's size under the default box-sizing: content-box. A width: 200px box with a 2px border is actually 204px wide. Setting box-sizing: border-box includes the border inside the declared width.
Lastly, adding a border only on :hover makes the element grow by the border width and nudges nearby content. Give the element a transparent border of the same width from the start and change only its colour on hover.
Takeaway
Every visible border needs a style. Use the shorthand for speed, side properties for accents, border-radius for rounded shapes, and an outline or shadow when you need a ring that does not change layout.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.