The Border Box

While you may not realize it, most elements have a border!

Border layer of CSS Box Model

Let’s learn how to make these borders visible.

The way to apply a border to our elements with CSS is… you guessed it! With the border property!

The border property is actually another shorthand that lets us combine the values of multiple properties into one declaration:

/* Both of these are the same */
 
h1 {
  border: 2px solid blue;
}
 
h1 {
  border-width: 2px;
  border-style: solid;
  border-color: blue;
}
  • border-width determines the “thickness” of the border, and is usually set with absolute units (e.g., pixels).
  • border-style includes values like soliddasheddotted.
  • border-color can be set with a named color, an rgb() value, or a hexadecimal.

Border properties (width, style, and color)

We can also style a single border if we want, by using border-topborder-rightborder-bottom, or border-left properties:

h1 {
  border-top: 5px dashed red;
  border-right: 5px dotted purple;
  border-bottom: 5px double yellow;
  border-left: 5px solid green;
}

Minus a few hidden styles, this is what the rendered output looks like:

Four border properties separately applied to each side

# Round Corners

Another common practice for borders is rounding the corners using the border-radius property:

h1 {
  border: 2px solid blue;
  border-radius: 5px;
}

The rendered output will look like this:

Border radius example

You can use both absolute and relative units. The larger the value, the rounder the corner will be!