Using borders, you can add a box around text, and set or change the border to nearly any color. A border in your HTML page helps bring attention to a section of text or surround any other HTML elements. Borders are added to HTML using CSS.

Border CSS

Border CSS has properties for line type (solid, dotted, etc.), line width, and line color. The following table elaborates on the details of each and which values are acceptable to make them work properly.

  • Border CSS
  • Add a border using the style attribute.
  • Add a border using a CSS class.

Add a border using the style attribute

HTML tags can be formatted using a style attribute. To add a border using the style attribute, add the border CSS inside the quotes after style=.

In the example below, we surrounded a paragraph (

) with a solid red border that is 3 pixels wide.

First example with text surrounded by a red border. This example also has multiple lines.

To create the example above, the code below is used.

First example with text surrounded by a red border.
This example also has multiple lines.

In the code above, the CSS is defining the border size (“px” short for pixel), style type, and border color. The style of the border is how the border appears on the screen. In our example, we used the “solid” border style. The border color defines the color you want to use for the border. In the example above, the color code #FF0000 is used, which is the color code for red.

Placing the CSS formatting inside the style attribute can be applied to other HTML tags, such as the div tag or span tag. In the example below, a border is added to a single word using the span tag.

A border can also be applied to only one side. For example, with the heading of this page, we have a grey underline. This line is actually a border, achieved with the CSS code border-bottom: 1px solid #93B0D2;.

Here is a second example with a bordered word, with different styles applied to the top, bottom, left, and right.

To create the example above, the following code is used.

word

Add a border using a CSS class

The appearance of elements on a web page may also be defined with inline CSS. Inline CSS is defined in your HTML document, in theelement. Or, you can define the CSS in an external file with the .css extension. Then, you can link to this file from any HTML page, and elements in that document can use the CSS styles. For example, with the CSS code below, a new class named “borderexample” is created that can be applied to any HTML tag.

Using the code above, if you want to apply this border style to an HTML paragraph or word, you can type something similar to the example below.

Here is an example of a border created using CSS.

The class can also be used on the span tag.

On your web page, the code above would look like the example below.

Here is an example of a border created using CSS.

The class can also be used on the span tag.

  • Additional help and information on HTML tables.
  • See our border definition for additional information related pages.
  • CSS and HTML color help and support.