CSS Border & Outline
Explore the world of CSS border and outline properties to add stylish visual boundaries and highlights to your elements, elevating the aesthetics of your web designs
Maintaining borders between HTML elements is as crucial as between countries. Borders show the element's area and prevent content from overlapping. Outlines, in turn, help elements stand out so that users know what to focus on.
Wait, aren't borders and outlines the same thing — the line around the element's box? Actually, they're different and use different CSS properties for setting the width, color, and line styling.
An outline is a line wrapping up an element outside its borders. Its goal is to make the element prominent and capture users' attention. Unlike borders, the outline can take any size you want.
In outline
Pro Tip: Outlines should not necessarily be rectangular, but usually, they're of this shape.
Don't confuse the element's outline and border! The border's goal boils down to specifying the element's existence and marking up its
For setting up borders, web developers use the border
The outline
is a shorthand
Outline-width
Outline-color
Outline-style
The outline-width
property defines the thickness of a line surrounding the element that can be thin, medium, and thick.
Unlike the outline dotted
, dashed
, solid
, double
, etc. To set an element's border or outline style, you should either use a shorthand border
or outline
property or specify the style using individual property — border-style
or outline-style
.
Make a note that the border-style property is a shorthand for the following properties:
• border-bottom-style
• border-left-style
• border-right-style
• border-top-style
You can specify a style for each side individually or list them all inside the border-style
property in a clockwise order starting from the top.
Pro Tip: None of the CSS properties applied to the border/outline will work if you forget to specify the style property.
In contrast to the outline, the element's borders can each be of different widths. The border width can take a predefined value — thin
, medium
, or thick
— or a more specific one using pixels (px), points (pt), centimeters (cm), or ems, like in the example.
You can specify the property in 3 ways:
- Using the
border
shorthandproperty and setting the same width for all sides - Using the
border-width
shorthand property and setting the same or various widths for each side - Using individual properties for each side (
border-bottom-width
,border-left-width
,border-right-width
, andborder-top-width
)
For modifying the outline outline
outline-color
property.
Since border sides can all be different, you can use the border-color
property and, in clockwise order, specify the color for each side.
What value can the color property take?
- Color name, e.g.,
yellow
- HEX code, e.g.
#FDC500
- RGB code, e.g.
rgb(253,197,0)
- HSL - specify a HSL value, like
hsl(47, 100%, 50%)
- Transparent
Rounded borders are pretty common for UI elements, like buttons or cards. They're visually more appealing than sharp-cornered borders and more convenient for users' eyes.
The border-radius
border-radius: 5px 20px 2px 10px;
The outline-offset
The offset can take a negative value and place the outline inside the element. In turn, a value of 0
sets the outline over the border so that there is no space between them.
Shorthand properties like border
and outline
simplify your actions and shorten the code. With one declaration, you can specify all style rules like width, style, and style
In addition to that, if you want to style only one border side, you can use border-left
, border-right
, border-top
, or border-bottom
properties. With one declaration, you can specify weight, style, and color just for one side, e.g.:
{
border-top: 5px solid black;
}
The correct answer for the border
dashed
value. Other types of border style include dotted, solid, double, groove, ridge, etc. If you decide to make each side of a different style, you should use the border-style
shorthand property and specify each value in a clockwise order starting from the top, e.g.: border-style: dotted dashed solid double;
The border-left
, border-right
, border-top
, and border-bottom
border-width
and border-style
when you need to specify values only for one side. The
The border-radius
is an inclusive