CSS Borders


You can use CSS border properties to choose how an element's border looks, how thick it is, and what color it has.


Borders on all sides.


Red bottom border.


Rounded borders.


Blue left border.


CSS Border Style

The border-style property tells us what type of border to show.

The following values are allowed:

  • dotted - Specifies a dotted border
  • dashed - Specifies a dashed border
  • solid - Specifies a solid border
  • double - Specifies a double border
  • groove - Specifies a 3D grooved border. The effect depends on the border-color you specify.
  • ridge - Specifies a 3D ridged border. The effect depends on the border-color you specify.
  • inset - Specifies a 3D inset border. The effect depends on the border-color you specify.
  • outset - Specifies a 3D outset border. The effect depends on the border-color you specify.
  • none - Specifies no border
  • hidden - Specifies a hidden border

The border-style propertycan be set with one to four values. These values control how the borders look on the top, right, bottom, and left sides of an element.

Example

p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}

Result:

A dotted border.

A dashed border.

A solid border.

A double border.

A groove border. The effect depends on the border-color you specify..

A ridge border. The effect depends on the border-color you specify..

An inset border. The effect depends on the border-color you specify..

An outset border. The effect depends on the border-color you specify..

No border.

A hidden border.

A mixed border.

Try it Yourself »

Note: You need to remember that the border-style property must be set for any of the other CSS border properties (which you'll learn about later) to work.


CSS Border Width

The border-width property tells us how wide the four borders are.

You can choose how wide something is by picking from three options: thin, medium, or thick(in px, pt, cm, em, etc).

Example

p.one {
  border-style: solid;
  border-width: 5px;
}

p.two {
  border-style: solid;
  border-width: medium;
}

p.three {
  border-style: dotted;
  border-width: 2px;
}

p.four {
  border-style: dotted;
  border-width: thick;
}

Result:

5px border-width
medium border-width
2px border-width
thick border-width
Try it Yourself »

CSS Border Color

The border-color property is used to set the color of the four borders.

The color can be set by:

  • name - Mention a color, such as "red."
  • HEX - you can choose a HEX color by using a code like "#ff0000".
  • RGB - you can choose an RGB color by using the format 'rgb(255, 0, 0)'.
  • HSL - you can choose an HSL value, such as "hsl(0, 100%, 50%)".
  • transparent

Note: If you don't choose a border-color it takes on the same color as the element.

Example

p.one {
  border-style: solid;
  border-color: red;
}

p.two {
  border-style: solid;
  border-color: green;
}

p.three {
  border-style: dotted;
  border-color: blue;
}

Result:

Red border
Green border
Blue border
Try it Yourself »

Specific Side Colors

The border-color property can hold between one and four values. These values control the color of the top, right, bottom, and left borders.

Example

p.one {
  border-style: solid;
  border-color: red green blue yellow; /* red top, green right, blue bottom and yellow left */
}
Try it Yourself »

HEX Values

You can choose a border color by using a HEX code, which is a combination of numbers and letters.

Example

p.one {
  border-style: solid;
  border-color: #ff0000; /* red */
}
Try it Yourself »

CSS Border - Individual Sides

You've already seen in the earlier examples that you can choose separate borders for each side.

In CSS, you can use properties to describe each of the borders individually. These borders are the ones on the top, right, bottom, and left sides.

Example

p {
  border-top-style: dotted;
  border-right-style: solid;
  border-bottom-style: dotted;
  border-left-style: solid;
}

Result:

Different Border Styles
Try it Yourself »

So, here is how it works:

When the border-style property has four values:

  • border-style: dotted solid double dashed;
    • dotted top border
    • solid right border
    • double bottom border
    • dashed left border

When border-style property has three values:

  • border-style: dotted solid double;
    • dotted top border
    • solid right and left borders
    • double bottom border

When border-style property has two values:

  • border-style: dotted solid;
    • dotted top and bottom borders
    • solid right and left borders

When border-style property has one value:

  • border-style: dotted;
    • all four sides borders are dotted

Example

/* Four values */
p {
  border-style: dotted solid double dashed;
}

/* Three values */
p {
  border-style: dotted solid double;
}

/* Two values */
p {
  border-style: dotted solid;
}

/* One value */
p {
  border-style: dotted;
}
Try it Yourself »

CSS Border - Shorthand Property

As you read earlier, there's a lot to think about when it comes to borders and their properties.

You can make the code shorter by putting all the different border settings into one property.

The border property is a way to set multiple border properties at once. It's like a shortcut for setting these properties separately:

  • border-width
  • border-style (required)
  • border-color

Example

p {
  border: 5px solid red;
}

Result:

Some text

Try it Yourself »

You can describe all the separate border characteristics for only one side:

Left Border

p {
  border-left: 6px solid red;
}

Result:

Some text

Try it Yourself »

CSS Rounded Borders

The border-radius property helps make an element's borders look rounded:

Round border

Rounder border

Roundest border

Example

p {
  border: 2px solid red;
  border-radius: 5px;
}
Try it Yourself »