Borders In CSS
CSS border properties are used to design and shape borders of elements on web pages.
The CSS border can specify color, border type, the width of the border, etc. These are the following properties in CSS for border:
Let's look at all these properties with examples.
1. CSS Border Style
The border-style property defines the style of the border. It specifies the type of border to be drawn. It can have the following values:
- solid - Specifies a solid border. This is default
- dotted - Specifies a dotted border
- dashed - Specifies a dashed border
- double - Specifies a double border
- groove - Specifies a 3D grooved border. The effect depends on the border-color value
- ridge - Specifies a 3D ridged border. The effect depends on the border-color value
- inset - Specifies a 3D inset border. The effect depends on the border-color value
- outset - Specifies a 3D outset border. The effect depends on the border-color value
- none - Specifies no border
- hidden - Specifies the same as none, except in border conflict resolution for table elements
Let's see the following example to understand the border-style property.
Example
<p style="border-style:solid">Border created is solid.</p>
<p style="border-style:dashed">Border created is dashed.</p>
<p style="border-style:dotted">Border created is dotted.</p>
<p style="border-style:double">Border created is double.</p>
<p style="border-style:hidden">Border created is hidden.</p>
<p style="border-style:none">Border created is none.</p>
<p style="border-style:groove">Groove defines a 3D grooved border.</p>
<p style="border-style:ridge">ridge defines a 3D ridged border.</p>
<p style="border-style:inset">Inset defines a 3D inset border.</p>
Try it
2. CSS Border Color
Borders can be given color using the border-color property.
The color value of the border-color property can be any valid CSS color value. It can be specified as:
- color name - like red, green, blue, etc.
- HEX value - like #a53427.
- RGB value - like rgb(255, 165, 0).
- HSL value - like hsl(9, 100%, 64%).
Note - Before setting border-color you must set border-style because to set the color to border it must exist first.
Example
.color1 { border-color: red }
.color2 { border-color: #d423bd }
.color3 { border-color: rgb(124,45,20) }
.color4 { border-color: hsl(90,50%,50%) }
Try it
3. CSS Border Width
CSS border-width property controls the width of the border.
The width value of the border-width property can be given on any valid scale.
Using the border-width property we can also individually control the width of each side of the box by using the following property:
- border-top-width - It is the width of the top of the box.
- border-right-width - It is the width of the right of the box.
- border-bottom-width - It is the width of the bottom of the box.
- border-left-width - It is the width of the left of the box.
Note: Before setting border-width you must set border-style because to set width to border it must exist first.
Example
.width1{ border-width:5px; }
.width2{ border-width:10px; }
.width3{ border-top-width:8px; }
.width4{ border-right-width:0; }
Try it
Instead of using CSS property to specify a width for each side of the box, you can also set values like border-width: top right bottom left.
Using this method you can set different values on different sides.
4. CSS Border Radius
CSS border-radius property is used to create rounded corners on an element's border.
The border-radius property can have from one to four values.
- If one value is given, it applies to all four corners.
- If two values are given, the first applies to the top-left and bottom-right corners, and the second applies to the top-right and bottom-left corners.
- If three values are given, the first applies to the top-left corner, the second applies to the top-right and bottom-left corners, and the third applies to the bottom-right corner.
- If four values are given, they apply to the top-left, top-right, bottom-right, and bottom-left corners, respectively.
Example
.radius1 { border-radius: 10px; }
.radius2 { border-radius: 10px 20px; }
.radius3 { border-radius: 10px 20px 30px; }
.radius4 { border-radius: 10px 20px 30px 40px; }
Try it
CSS border (shorthand)
You have seen how to set the border style, width, and color individually. But you can also set all these properties in one line using the border property.
Using shorthand border property is more convenient, easy to remember, and most importantly, it saves time.
You can set the border style, width, and color in one line in any order.
Example
.border1 { border: 1px solid red; }
.border2 { border: 1px red solid; }
.border3 { border: solid 1px red; }
.border4 { border: solid red 1px; }
.border5 { border: red solid 1px; }
.border6 { border: red 1px solid; }
Try it
When you use the border property, you can omit the border color or the border width. If you omit the border color, the default value is black. If you omit the border width, the default value is 3px.
In the next article, you will learn about CSS Margins.