CSS Border Properties

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

Border Style

The border-style property specifies what kind of border to display.
Remark None of the border properties will have ANY effect unless the border-style property is set!

border-style values:

none: Defines no border
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 are the same as the border-width value
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 yourself:
Example

<html>
<head>
<style type="text/css">
p.none {border-style:none;}
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.hidden {border-style:hidden;}
</style>
</head>


<body>
<p class="none">No border.</p>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
<p class="double">A double border.</p>
<p class="groove">A groove border.</p>
<p class="ridge">A ridge border.</p>
<p class="inset">An inset border.</p>
<p class="outset">An outset border.</p>
<p class="hidden">A hidden border.</p>
</body>


</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Border Width

The border-width property is used to set the width of the border.
The width is set in pixels, or by using one of the three pre-defined values: thin, medium, or thick.
Note: The "border-width" property does not work if it is used alone. Use the "border-style" property to set the borders first.

Example

<html>
<head>
<style type="text/css">
p.one 
{
border-style:solid;
border-width:5px;
}
p.two 
{
border-style:solid;
border-width:medium;
}
p.three
{
border-style:solid;
border-width:1px;
}
</style>
</head>

<body>
<p class="one">Some text.</p>
<p class="two">Some text.</p>
<p class="three">Some text.</p>
<p><b>Note:</b> The "border-width" property does not work if it is used alone. Use the "border-style" property to set the borders first.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

Some text.
Some text.
Some text.
Note: The "border-width" property does not work if it is used alone. Use the "border-style" property to set the borders first.


Border Color

The border-color property is used to set the color of the border. The color can be set by:
  • name - specify a color name, like "red"
  • RGB - specify a RGB value, like "rgb(255,0,0)"
  • Hex - specify a hex value, like "#ff0000"
You can also set the border color to "transparent".
Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.

Example

<html>
<head>
<style type="text/css">
p.one
{
border-style:solid;
border-color:red;
}
p.two
{
border-style:solid;
border-color:#98bf21;
</style>
</head>

<body>
<p class="one">A solid red border</p>
<p class="two">A solid green border</p>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

A solid red border
A solid green border
Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.


Border - Individual sides

In CSS it is possible to specify different borders for different sides:

Example

<html>
<head>
<style type="text/css">
p
{
border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:solid;
}
</style>
</head>

<body>
<p>2 different border styles.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
The example above can also be set with a single property:

Example

<html>
<head>
<style type="text/css">
p
{
border-style:dotted solid;
}
</style>
</head>

<body>
<p>2 different border styles.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
The border-style property can have from one to four values.
  • border-style:dotted solid double dashed;
    • top border is dotted
    • right border is solid
    • bottom border is double
    • left border is dashed
  • border-style:dotted solid double;
    • top border is dotted
    • right and left borders are solid
    • bottom border is double
  • border-style:dotted solid;
    • top and bottom borders are dotted
    • right and left borders are solid
  • border-style:dotted;
    • all four borders are dotted
The border-style property is used in the example above. However, it also works with border-width and border-color.

Border - Shorthand property

As you can see from the examples above, there are many properties to consider when dealing with borders.
To shorten the code, it is also possible to specify all the border properties in one property. This is called a shorthand property.
The shorthand property for the border properties is "border":

Example

<html>
<head>
<style type="text/css">
p
{
border:5px solid red;
}
</style>
</head>

<body>
<p>This is some text in a paragraph.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
When using the border property, the order of the values are:
  • border-width
  • border-style
  • border-color
It does not matter if one of the values above are missing (although, border-style is required), as long as the rest are in the specified order.

More Examples

All the top border properties in one declarationThis example demonstrates a shorthand property for setting all of the properties for the top border in one declaration.
Example

<html>
<head>
<style type="text/css">

{
border-style:solid;
border-top:thick double #ff0000;
}
</style>
</head>


<body>
<p>This is some text in a paragraph.</p>
</body>


</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Set the style of the bottom borderThis example demonstrates how to set the style of the bottom border.

Example

<html>
<head>
<style type="text/css">
p {border-style:solid;}
p.none {border-bottom-style:none;}
p.dotted {border-bottom-style:dotted;}
p.dashed {border-bottom-style:dashed;}
p.solid {border-bottom-style:solid;}
p.double {border-bottom-style:double;}
p.groove {border-bottom-style:groove;}
p.ridge {border-bottom-style:ridge;}
p.inset {border-bottom-style:inset;}
p.outset {border-bottom-style:outset;}
</style>
</head>


<body>
<p class="none">No bottom border.</p>
<p class="dotted">A dotted bottom border.</p>
<p class="dashed">A dashed bottom border.</p>
<p class="solid">A solid bottom border.</p>
<p class="double">A double bottom border.</p>
<p class="groove">A groove bottom border.</p>
<p class="ridge">A ridge bottom border.</p>
<p class="inset">An inset bottom border.</p>
<p class="outset">An outset bottom border.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Result:
No bottom border.
A dotted bottom border.
A dashed bottom border.
A solid bottom border.
A double bottom border.
A groove bottom border.
A ridge bottom border.
An inset bottom border.
An outset bottom border.


Set the width of the left borderThis example demonstrates how to set the width of the left border.

Example

<html>
<head>
<style type="text/css">

{
border-style:solid;
border-left-width:15px;
}
</style>
</head>


<body>
<p><b>Note:</b> The "border-left-width" property does not work if it is used alone. Use the "border-style" property to set the borders first.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.


Set the color of the four bordersThis example demonstrates how to set the color of the four borders. It can have from one to four colors.
Example

<head>
<style type="text/css">
p.one
{
border-style:solid;
border-color:#0000ff;
}
p.two
{
border-style:solid;
border-color:#ff0000 #0000ff;
}
p.three
{
border-style:solid;
border-color:#ff0000 #00ff00 #0000ff;
}
p.four
{
border-style:solid;
border-color:#ff0000 #00ff00 #0000ff rgb(250,0,255);
}
</style>
</head>


<body>
<p class="one">One-colored border!</p>
<p class="two">Two-colored border!</p>
<p class="three">Three-colored border!</p>
<p class="four">Four-colored border!</p>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Result:
One-colored border!
Two-colored border!
Three-colored border!
Four-colored border!
Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.


Set the color of the right border
This example demonstrates how to set the color of the right border.
Example

<html>
<head>
<style type="text/css">

{
border-style:solid;
border-right-color:#ff0000;
}
</style>
</head>


<body>
<p>This is some text in a paragraph.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.



All CSS Border Properties

PropertyDescription
borderSets all the border properties in one declaration
border-bottomSets all the bottom border properties in one declaration
border-bottom-colorSets the color of the bottom border
border-bottom-styleSets the style of the bottom border
border-bottom-widthSets the width of the bottom border
border-colorSets the color of the four borders
border-leftSets all the left border properties in one declaration
border-left-colorSets the color of the left border
border-left-styleSets the style of the left border
border-left-widthSets the width of the left border
border-rightSets all the right border properties in one declaration
border-right-colorSets the color of the right border
border-right-styleSets the style of the right border
border-right-widthSets the width of the right border
border-styleSets the style of the four borders
border-topSets all the top border properties in one declaration
border-top-colorSets the color of the top border
border-top-styleSets the style of the top border
border-top-widthSets the width of the top border
border-widthSets the width of the four borders

Leave a Reply