Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Border

CSS Borders

CSS borders are properties used to define the border style of an element.

Borders can be applied to any HTML element to enhance visual effects, separate content, or highlight elements.

CSS border properties mainly include border width, border style, border color, and shorthand properties.


CSS Border Properties

CSS border properties allow you to specify the style and color of an element's border.

Borders on all four sides

Red bottom border

Rounded Borders

Left border with width, colored blue


Border Style

The border-style property specifies what kind of border to display.

The border-style property is used to specify the type of border to display.

The allowed values are as follows:

border-style demonstration:

none: No border by default

dotted: Defines a dotted border

dashed: Defines a dashed border

solid: Defines a solid border

double: Defines two borders. The width of the two borders is the same as the value of border-width

groove: Defines a 3D grooved border. The effect depends on the border color value

ridge: Defines a 3D ridged border. The effect depends on the border color value

inset: Defines a 3D inset border. The effect depends on the border color value

outset: Defines a 3D outset border. The effect depends on the border color value

Try it »


Border Width

You can specify the width of a border using the border-width property.

There are two ways to specify the width of a border: you can specify a length value, such as 2px or 0.1em (units such as px, pt, cm, em, etc.), or use one of three keywords: thick, medium (the default), and thin.

**Note:** CSS does not define the specific widths of the three keywords, so one user might set thick, medium, and thin equal to 5px, 3px, and 2px respectively, while another user might set them to 3px, 2px, and 1px respectively.

Example

p

.one

{

border-style:

solid

;

border-width:

5

px

;

}

p

.two

{

border-style:

solid

;

border-width:

medium

;

}

Try it »


Border Colors

The border-color property is used to set the color of the border. Colors that can be set:

You can also set the border color to "transparent".

Note: border-color alone has no effect; you must first use border-style to set the border style.

Example

p

.one

{

border-style:

solid

;

border-color:

red

;

}

p

.two

{

border-style:

solid

;

border-color:

#98bf21

;

}

Try it »


Border - Setting Each Side Individually

In CSS, you can specify different borders for different sides:

Example

p

{

border-top-style:

dotted

;

border-right-style:

solid

;

border-bottom-style:

dotted

;

border-left-style:

solid

;

}

Try it »

The example above can also be set with a single property:

Example

border-style

:dotted

solid

;

Try it »

The border-style property can have 1 to 4 values:

- Top border is dotted - Right border is solid - Bottom border is double - Left border is dashed - Top border is dotted - Left and right borders are solid - Bottom border is double - Top and bottom borders are dotted - Right and left borders are solid - All four borders are dotted

The examples above use border-style. However, it can also be used together with border-width and border-color.


Border - Shorthand Property

The examples above use many properties to set borders.

You can also set borders in a single property.

You can set the following in the "border" property:

Example

border

:5

px

solid

red

;

Try it »


More examples

All border properties in a single declaration
This example demonstrates using the shorthand property to set all four border properties in a single declaration.

Setting the Style of the Bottom Border
This example demonstrates how to set the style of the bottom border.

Setting the Width of the Left Border
This example demonstrates how to set the width of the left border.

Setting the Colors of All Four Borders
This example demonstrates how to set the colors of all four borders. You can set one to four colors.

Setting the Color of the Right Border
This example demonstrates how to set the color of the right border.

Setting a Gradient Border
This example demonstrates how to set a gradient border.


CSS Border Properties

property Description
border Shorthand property for setting the properties for all four sides in a single declaration.
border-style Sets the style of all borders of an element, or sets the border style for each side individually.
border-width Shorthand property for setting the width of all borders of an element, or setting the width of each border individually.
border-color Shorthand property for setting the color of the visible parts of all borders of an element, or setting the color for each of the 4 sides individually.
border-bottom Shorthand property for setting all properties of the bottom border in a single declaration.
border-bottom-color Sets the color of the bottom border of an element.
border-bottom-style Sets the style of the bottom border of an element.
border-bottom-width Sets the width of the bottom border of an element.
border-left Shorthand property for setting all properties of the left border in a single declaration.
border-left-color Sets the color of the left border of an element.
border-left-style Sets the style of the left border of an element.
border-left-width Sets the width of the left border of an element.
border-right Shorthand property for setting all properties of the right border in a single declaration.
border-right-color Sets the color of the right border of an element.
border-right-style Sets the style of the right border of an element.
border-right-width Sets the width of the right border of an element.
border-top Shorthand property for setting all properties of the top border in a single declaration.
border-top-color Sets the color of the top border of an element.
border-top-style Sets the style of the top border of an element.
border-top-width Sets the width of the top border of an element.
border-radius Sets rounded corners on the border.