CSS Borders
In this tutorial you will learn about Cascading Style Sheets (CSS) Borders, Border width, Border style, Border color and Using the shortcut
Borders in CSS are not just the table borders as in HTML, with CSS any HTML element can have borders, CSS adds many effects to be applied to these borders.
Border width
To set the width of a border, use the property “border-width”, the value of this property can be one of the following values:
thin, medium, thick, or an absolute value as the table “border” attribute in HTML.
Example:
table
{
border-width: 2px;
}
This sets the width of the border of the < table > element to be 2 pixels.
Border style
To set the style of a border, use the property “border-style”, the value of this property can be one of the following values:
none, dashed, dotted, double, groove, hidden, inset, outset, ridge, or solid.
Example:
Table
{
border-width: 1px;
border-style: dotted;
}
This sets the border style of the < table > element to be dotted, and its width to 1 pixel.
Border color
To set the color of a border, use the property “border-color”, the value of this property can be any HTML color.
Example:
table
{
border-width: 1px;
border-style: dotted;
border-color: blue;
}
This sets the style of the border of the < table > element to dotted, width to 1 pixel, and color to blue.
Note: All the previous properties can take up to 4 values, in the previous example if color is defined as:
border-color: blue green;
The top and bottom borders will be colored as blue and the right and left borders will be colored as green.
If the rule is declared as:
border-color: blue green red;
The top border will be colored as blue, the right border will be colored as green, the bottom border will be colored as red, and the left border will be colored as the right border.
If the rule is declared as:
border-color: blue green red yellow;
The top border will be colored as blue, the right border will be colored as green, the bottom border will be colored as red, and the left border will be colored as yellow.
The rule is:
1. The first value is for the top border
2. If there is no other value, then all borders color will be set to the only declared value.
3. Else, the second value is for the right border.
4. If there is no other value, then the bottom border value will be the same as the top border, and the left border value will be the same as the right border.
5. Else, the third value is for the bottom value.
6. If there is no other value, then the left border value will be the same as the right border.
7. Else, the last value will be for the left border.
This applies all border properties.
Using the shortcut
You can set all the properties in one declaration, to do so use the property “border”, and the order is:
border-width, then border-style, and finally border-color.
Example:
Table
{
border: 1px dotted blue;
}
This sets the border width of the < table > element to 1 pixel, the style to dotted, and the color to blue.
There are other border properties to set the border values for only one of the borders, they have the same values, they are:
border-top-width, border-top-style, border-top-color, border-top, border-right-width, border-right-style, border-right-color, border-right, border-bottom-width, border-bottom-style, border-bottom-color, border-bottom, border-left-width, border-left-style, border-left-color, and border-left.
The only difference between these properties and the previously explained properties is that they can’t take more than one value to be set for that single border.