How to create a colored border around text with HTML and CSS

Updated: 07/31/2022 by Computer Hope
Blue colored border around the word Border

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

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.

CSS Border Property Valid Values Example
border (all sides)
border-top (top line only)
border-right (right line only)
border-bottom (bottom line only)
border-left (left line only)
Any valid combination of the properties below. dotted thin black
border-style (all sides) solid
dotted
dashed
double
groove
ridge
inset
outset
none
hidden
solid
border-width (all sides) width, specified in px, pt, cm, em
width, specified as thin, medium, or thick
3 px
border-color (all sides) color, specified as color code
color, specified as named color
#FF00FF

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=.

<HTML-Tag style="myBorder">

In the example below, we surrounded a paragraph (<p></p>) 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.

<p style="border-width:3px; border-style:solid; border-color:#FF0000; padding: 1em;">First example with text surrounded by a red border.<br>This example also has multiple lines.</p>

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.

Tip

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;.

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.

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.

<span style="border-top:thick green solid;border-bottom:thick green double;border-left:4px #2330C4 dotted; border-right:thin #2330C4 dotted;padding-left:2px;padding-right:2px;">word</span>

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 the <head></head> element. 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.

<style>
.borderexample {
 border-width:3px;
 border-style:solid;
 border-color:#287EC7;
}
</style>

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

<p class="borderexample">Here is an example of a border created using CSS.</p>
<p>The class can also be <span class="borderexample">used</span> on the span tag.</p>

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.