Hello guys! morning and welcome to this section of my tutorial on CSS. I want to also use this medium to wish you all a happy new month. In my last tutorial i talked about CSS Tables and i believe that you guys understand every thing about CSS Tables by now.
Now in this tutorial post i want to talk about CSS borders. The border property allows you to specify how the border of the box representing an element should look. There are three properties of a border you can change:
- The border-color specifies the color of a border.
- The border-style specifies whether a border should be solid, dashed line, double line, or one of the other possible values.
- The border-width specifies the width of a border.
Now let's see how to use these properties with examples.
The border-color Property
The border-color property allows you to change the color of the border surrounding an element. You can individually change the color of the bottom, left, top and right sides of an element's border using these properties below:
- border-bottom-color changes the color of bottom border.
- border-top-color changes the color of top border.
- border-left-color changes the color of left border.
- border-right-color changes the color of right border.
The following example below shows the effect of all these properties.
<html>
<head>
<style type="text/css">
p.example1 {
border: 2px solid;
border-bottom-color: #009900; /* Green */
border-top-color: #0000CC; /* Blue */
border-left-color: #FF0000; /* Red */
border-right-color: #000000; /* Black */
}
p.example2 {
border: 2px solid;
border-color: #000000; /* Black */
}
</style>
</head>
<body>
<p class="example1">
This example is showing all borders in different colors.
</P>
<p class="example2">
This example is showing all borders in black color only.
</P>
</body>
</html>
<head>
<style type="text/css">
p.example1 {
border: 2px solid;
border-bottom-color: #009900; /* Green */
border-top-color: #0000CC; /* Blue */
border-left-color: #FF0000; /* Red */
border-right-color: #000000; /* Black */
}
p.example2 {
border: 2px solid;
border-color: #000000; /* Black */
}
</style>
</head>
<body>
<p class="example1">
This example is showing all borders in different colors.
</P>
<p class="example2">
This example is showing all borders in black color only.
</P>
</body>
</html>
You can also read our tutorial post on: Html Formating
The border-style Property
The border-style property allows you to select one of the following styles of a border below:
- none: No border. (Equivalent of border-width: 0;)
- solid: Border is a single solid line.
- dotted: Border is a series of dots.
- dashed: Border is a series of short lines.
- double: Border is two solid lines.
- groove: Border looks as though it is carved into the page.
- ridge: Border looks the opposite of groove.
- inset: Border makes the box look like it is embedded in the page.
- outset: Border makes the box look like it is coming out of the canvas.
- hidden: Same as none, except in terms of border-conflict resolution for table elements.
You can also individually change the style of the bottom, left, top, and right borders of an element using the following properties below:
- border-bottom-style changes the style of the bottom border.
- border-top-style changes the style of the top border.
- border-left-style changes the style of the left border.
- border-right-style changes the style of the right border.
The following example shows all these border styles:
<html>
<head>
</head>
<body>
<p style="border-width: 3px; border-style: none;">
This is a border with none width.
</p>
<p style="border-width: 3px; border-style: solid;">
This is a solid border.
</p>
<p style="border-width: 3px; border-style: dashed;">
This is a dashed border.
</p>
<p style="border-width: 3px; border-style: double;">
This is a double border.
</p>
<p style="border-width: 3px; border-style: groove;">
This is a groove border.
</p>
<p style="border-width: 3px; border-style: ridge;">
This is a ridge border.
</p>
<p style="border-width: 3px; border-style: inset;">
This is an inset border.
</p>
<p style="border-width: 3px; border-style: outset;">
This is an outset border.
</p>
<p style="border-width: 3px; border-style: hidden;">
This is a hidden border.
</p>
<p style="border-width: 3px;
border-top-style: dashed;
border-bottom-style: double;
border-left-style: solid;
border-right-style: groove;">
This is a border with four different styles.
</p>
</body>
</html>
<head>
</head>
<body>
<p style="border-width: 3px; border-style: none;">
This is a border with none width.
</p>
<p style="border-width: 3px; border-style: solid;">
This is a solid border.
</p>
<p style="border-width: 3px; border-style: dashed;">
This is a dashed border.
</p>
<p style="border-width: 3px; border-style: double;">
This is a double border.
</p>
<p style="border-width: 3px; border-style: groove;">
This is a groove border.
</p>
<p style="border-width: 3px; border-style: ridge;">
This is a ridge border.
</p>
<p style="border-width: 3px; border-style: inset;">
This is an inset border.
</p>
<p style="border-width: 3px; border-style: outset;">
This is an outset border.
</p>
<p style="border-width: 3px; border-style: hidden;">
This is a hidden border.
</p>
<p style="border-width: 3px;
border-top-style: dashed;
border-bottom-style: double;
border-left-style: solid;
border-right-style: groove;">
This is a border with four different styles.
</p>
</body>
</html>
NOTE: In the above example we made use of inline CSS.
You can also read our tutorial post on: Html Attributes 2
The border-width Property
The border-width property allows you to set the width of an element borders. The value of this property could be either a length in px, pt, or cm, or it should be set to thin, medium, or thick.
You can also individually change the width of the bottom, top, left, and right borders of an element using the following properties:
- border-bottom-width changes the width of the bottom border.
- border-top-width changes the width of the top border.
- border-left-width changes the width of the left border.
- border-right-width changes the width of the right border.
The following example shows these border width:
<html>
<head>
</head>
<body>
<p style="border-width: 3px; border-style: solid;">
This is a solid border whose width is 3px.
</p>
<p style="border-width: 3pt; border-style: solid;">
This is a solid border whose width is 3pt.
</p>
<p style="border-width: thin; border-style: solid;">
This is a solid border whose width is thin.
</p>
<p style="border-width: medium; border-style: solid;">
This is a solid border whose width is medium.
</p>
<p style="border-width: thick; border-style: solid;">
This is a solid border whose width is thick.
</p>
<p style="border-bottom-width: 3px;
border-top-width: 5px;
border-left-width: 1px;
border-right-width: 10px;
border-style: solid;">
This is a border with four different width.
</p>
</body>
</html>
<head>
</head>
<body>
<p style="border-width: 3px; border-style: solid;">
This is a solid border whose width is 3px.
</p>
<p style="border-width: 3pt; border-style: solid;">
This is a solid border whose width is 3pt.
</p>
<p style="border-width: thin; border-style: solid;">
This is a solid border whose width is thin.
</p>
<p style="border-width: medium; border-style: solid;">
This is a solid border whose width is medium.
</p>
<p style="border-width: thick; border-style: solid;">
This is a solid border whose width is thick.
</p>
<p style="border-bottom-width: 3px;
border-top-width: 5px;
border-left-width: 1px;
border-right-width: 10px;
border-style: solid;">
This is a border with four different width.
</p>
</body>
</html>
You can also read our tutorial post on: Html Attributes
Border Properties Using Shorthand
The border property allows you to specify color, style, and width of lines in one property. The following example shows how to use all these three properties into a single property. This is the most frequently used property to set border around any element.
<html>
<head>
</head>
<body>
<p style="border: 3px solid red;">
This example is showing shorthand property for border.
</p>
</body>
</html>
<head>
</head>
<body>
<p style="border: 3px solid red;">
This example is showing shorthand property for border.
</p>
</body>
</html>
Alright guys! we have come to the end of this tutorial on CSS border. Feel free to drop your questions in the comment box below, they will be adttended to as soon as possible.
Don't forget to like our facebook page, subcribe with us.