A photo on the wall looks neater in a frame. On a web page, an element can have a frame too. That frame is called a border: a line around the edge of the element.
A border has three parts: its width, its line style (straight, dashed or dotted) and its colour. The part people forget most often is the line style, and without a line style no border shows.
A border in one line
This rule gives the heading a blue border:
h1 {
border: 3px solid blue;
}
h1is the selector: the rule is for every<h1>.borderis a shorthand property, one property that sets three properties at once. It takes three values, separated by spaces with no commas:3pxis the width (border-width).solidis the line style (border-style): one unbroken line.blueis the colour (border-color).
The result: the heading gets a blue frame, 3 pixels wide, on all four sides. The usual order is width, style, colour. That one border line gives the same result as these three separate declarations:
h1 {
border-width: 3px;
border-style: solid;
border-color: blue;
}
Both ways are fine. If the h1 { ... } rule is already there, as in the first exercise, add the declarations inside the same curly braces. If you don't write a colour, the line uses the element's text colour.
No line style, no border
The default border-style is none, which means no line. So border-width and border-color alone draw nothing: the width and colour are set, but the line style is not. That is why border: 2px blue; shows no line at all. Add a line style, such as solid, and the line appears right away.
The border-style values: solid (a straight line), dashed, dotted, double (two lines), plus groove, ridge, inset, outset (four styles that look raised or pressed in) and none.
⚠️ The line doesn't show, or the box gets wider
- The browser ignores
border: 1px, solid, black;, because there are commas between the values. Separate the values with spaces. border: 1 solid black;is ignored too, because the number has no unit. Write1px.- The browser doesn't know
dashanddot. The correct style names aredashedanddotted. - A border adds to the box's size. A box with
width: 200px(200 pixels wide) andborder: 10px solidis 220px wide on screen. That is 200, plus 10 for the line on the left and 10 for the line on the right. The box stays 200px wide if you usebox-sizing: border-box, which the next lesson covers.
TL;DR: A border is a line around the edge of an element. border: 3px solid blue; writes its three parts in one line: width, line style and colour, separated by spaces. It gives the same result as writing border-width, border-style and border-color separately. The line style has to be written, because its default is none (no line). Common styles: solid (straight), dashed, dotted, double (two lines).
⚡ Width, style, colour
Write:
h1 {
border: 3px solid blue;
}
Meaning: border puts a line around the edge of an element, here the <h1> heading. This property sets three things at once: the width (3px), the line style (solid, one unbroken line) and the colour (blue). The values are separated by spaces with no commas, and the number has a unit.
⚡ The line style must be there
Guess first: what line does border: 2px blue; draw?
No line at all. The line style is not written, and the default line style is none (no line).
Meaning: A width and a colour alone draw nothing. Add a line style, such as solid (straight), dashed or dotted, and the line appears right away.
⚡ A border adds width
Guess first: a box with width: 200px gets border: 10px solid. How wide is it on screen?
220px. The 10px line is added outside that 200px width, on the left and on the right.
Meaning: width sets the width of the box without counting the border, unless you use box-sizing: border-box. With that value, width includes the border.