A box is rarely finished when its content and its spacing are right.
The last layer of polish comes from the edges. There is a border to define the boundary, a shadow to lift the box off the page, and a radius to soften the corners.
All three are simple properties once you know their syntax.
The previous lesson in this module covered "Why Do Vertical Margins Collapse Together?", and the next lesson in this module is about "How Does overflow Control Text and Images That Don't Fit?".
What border styles are there?
A border needs a width, a style, and a colour, and the style is what makes it visible. A border with a width but no style is invisible.
.card {
border: 1px solid #e2e2e2; /* width style colour */
border-top: 2px dashed #999; /* one side only */
border-width: 1px 2px 1px 2px; /* top right bottom left */
border-color: #ddd #ccc; /* vertical then horizontal */
border-style: solid;
}There are eight border styles: solid, dashed, dotted, double, groove, ridge, inset, and outset.
The solid border is the one you will use almost every time.
The dashed and dotted styles are useful for showing that something is provisional, and the double border gives a classic rule look to a box.
The directional shorthands follow the same order everywhere in CSS. It is top, right, bottom, and left.
There is one detail worth knowing. A border takes up space inside the box, and under the default content-box sizing it is added outside the content. Under the border-box it is taken out of the width you set. That is the second reason almost every project sets the border-box.
How does the box-shadow work?
The syntax is an x offset, a y offset, a blur radius, a spread radius, and a colour, in that order.
The first two move the shadow, the blur softens its edge, and the spread grows or shrinks the shape of the shadow before the blur is applied.
.card {
box-shadow:
0 1px 2px rgb(0 0 0 / 0.08), /* a subtle lift */
0 8px 24px rgb(0 0 0 / 0.06); /* a larger, softer shadow */
}Two shadows are separated by a comma, and each one paints behind the previous. That is how the layered look of a raised card is built.
Multiple shadows are worth using for anything that floats. There is a tight shadow for the contact with the surface, and a wide one for the ambient light.
The inset keyword makes the shadow draw inside the box instead of outside, and that is how a pressed or inset effect is created.
.input {
box-shadow: inset 0 1px 2px rgb(0 0 0 / 0.12);
}
.card:hover {
box-shadow: 0 12px 28px rgb(0 0 0 / 0.14);
}Keep the vertical offset non-zero.
A shadow with no offset, only a blur, looks like a glow and not like a shadow, and it does not read as a raised surface.
How does the border-radius work?
It takes one length for all four corners, or up to four of them for the top-left, the top-right, the bottom-right, and the bottom-left.
A percentage is relative to the dimensions of the box itself, and that is what makes elliptical corners possible.
.card { border-radius: 8px; }
.pill { border-radius: 999px; } /* fully rounded ends */
.circle { border-radius: 50%; } /* only square if width equals height */
.asym { border-radius: 12px 4px 12px 4px; }The trick of a large value, the 999px on a pill-shaped element, works because the radius is clamped to half of the smaller of the width and the height. So any value above that produces a full semicircle.
For a circle you use 50 percent, and you make sure the element is a square. Otherwise you get an ellipse.
There is one subtlety with percentage radii on a non-square box. The horizontal and the vertical radii are scaled independently, so the corner of the shape is an ellipse and not a circle.
If you need a true circle, set an equal width and height.
Can a border be a gradient?
Not directly, but there is a well-known technique that fakes it by using a background and an inner element, and it has become much simpler with the addition of multiple backgrounds.
.gradient-border {
border: 2px solid transparent;
background:
linear-gradient(#fff, #fff) padding-box,
linear-gradient(45deg, #800d4d, #ff9ec7) border-box;
border-radius: 12px;
}The two background layers are clipped differently. The first one is clipped to the padding box of the box, and the second one is clipped to the border box, so the gradient shows only in the border area.
It is a good trick, and it is worth understanding rather than copying blindly.
How do you style a border on only one side?
Every border property has a long form that takes four values in a fixed order, which is top, right, bottom, and left. And there is a shorthand that takes one value for all four of them.
To set one side, you use the side-specific long form.
.card {
border-style: solid;
border-width: 1px;
border-color: #d8e0e8;
/* only the top edge, in a different colour and twice as thick */
border-top: 3px solid #185ca5;
}
/* the same thing written as a long hand */
.card {
border-top-width: 3px;
border-top-style: solid;
border-top-color: #185ca5;
}The order of the four values matters, and it is worth memorising, because getting it wrong is a classic source of a border that looks like it is on the wrong side.
It is top, then right, then bottom, then left. That is the same clockwise order as the margin and the padding.
Three values mean the top, both sides, and then the bottom.
For an accent bar on the side of a card, the technique is to set a transparent border on every side, and then give the one side you want a colour.
The border still occupies its space, so the layout does not shift, and the other three edges stay invisible rather than absent.
.notice {
border: 1px solid transparent;
border-inline-start: 4px solid #c9821a;
padding: 0.75rem 1rem;
background: #fdf6ea;
}There is one caveat about the shorthand. border: none resets the width, the style, and the colour of all four sides at once.
That is usually what you want when you remove a border, but it also means that a later rule which sets only the border-color will not bring the border back. The style is still none, and a border with no style is not drawn at all.
Quick reference
- A border needs a style as well as a width, or it is invisible.
- The
box-shadowtakes x, y, blur, spread, and colour, and several shadows stack. - Keep the vertical offset non-zero or it reads as a glow.
- The
insetdraws a shadow inside the box. - The
border-radiustakes one to four values, and 999px on a pill gives fully rounded ends. - Percentage radii are scaled per axis, so a circle needs a square element.
Try it yourself: build a card with a one pixel border, a two-layer shadow, and a radius of twelve pixels. Then hover it and change the shadow to a larger offset. Finally, set the border to a gradient using the padding-box and border-box technique, and check in the developer tools that the border area is the only part that shows the colour.