The CSS border properties allow you to specify the style, width, and color of an element’s border. Borders are essential for defining the edges of containers, buttons, and input fields.
Individual Border Properties
A border has three main characteristics that you can control:
Border Style (border-style)
This is the most important property. A border will not show up unless a style is defined.
- Values:
none,hidden,dotted,dashed,solid,double,groove,ridge,inset,outset. - Example:
p { border-style: solid; }
Border Width (border-width)
Sets the thickness of the border.
- Values: Specific sizes (
px,em,rem) or keywords (thin,medium,thick). - Example:
div { border-width: 5px; }
Border Color (border-color)
Sets the color of the border.
- Values: Color names, HEX, RGB, HSL, or
currentcolor. - Example:
button { border-color: #3498db; }
Example
.border-style-example {
border-width: 5px;
border-style: dotted;
border-color: red;
}
Output
Border – Individual Sides
You can specify different borders for each side (Top, Right, Bottom, Left).
Example
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
Output
Border Shorthand Property
To save code, you can define all the individual border properties in one single property: border.
Syntax Order: border: [width] [style] [color];
Example Code:
.bo-box {
border: 2px solid red;
}
/* You can also style just one side using shorthand */
.bo-card {
border-bottom: 5px solid green;
}
Output
Rounded Borders (border-radius)
The border-radius property is used to add rounded corners to an element.
- Values:
px,%, orem. - Circle Tip: To make a square element perfectly circular, set
border-radius: 50%;.
Example Code:
.bo-button {
border: 2px solid blue;
border-radius: 8px; /* Slightly rounded corners */
}
.bo-avatar {
width: 100px;
height: 100px;
border-radius: 50%; /* Perfect circle */
}
Output
Override border Shorthand Property
You can override the border shorthand property by using any individual border property below it. See the example code below.
bo-override {
border: 5px solid gray;
border-bottom-width: 20px;
}
Output
Applying Borders to Inline Elements
Similar to Block elements border can be applied to inline elements.
<p>
Here the border is applied to <span style="border: 5px solid red;>inline elements span</span> inside a paragraph element
</p>
Output
Here the border is applied to border-image Property
To set images as borders for another element like div, span, body, paragraph, etc, you can use the border-image property. First you need to declare the border-style property before providing an image source, else no image will be displayed as the border. Example code Output All the css border properties are listed in the table below:
image-bo{
background-color: #FF0000;
border: 30px solid transparent;
border-image: url(https://www.tutorialmanthra.com/wp-content/uploads/flower-image.jpg) 40;
padding: 20px;
}
Summary Table: All CSS Border Properties
Property Description Example bordershorthand property for setting all the border properties in one declaration border: 1px solid #000; border-color shorthand property for setting border color of an element. border-color: blue; border-style shorthand property for setting style (solid / dashed) of border of an element border-style: dashed; border-width shorthand property for setting border width of an element. border-width: medium; border-bottom shorthand property for setting bottom border of an element. border-bottom: 3px solid blue; border-bottom-color Sets the color of bottom border of an element border-bottom-color: green; border-bottom-width Sets the width of bottom border of an element. border-bottom-width: 4px; border-bottom-style Sets the style of bottom border of an element. border-bottom-style: dotted; border-left shorthand property for setting left border of an element. border-left: 6px double purple; border-left-color Sets the color of left border of an element. border-left-color: orange; border-left-width Sets the width of left border of an element. border-left-width: 8px; border-left-style Sets the style of left border of an element. border-left-style: groove; border-right shorthand property for setting right border of an element. border-right: 5px ridge brown; border-right-color Sets the color of right border of an element. border-right-color: pink; border-right-width Sets the width of right border of an element. border-right-width: 7px; border-right-style Sets the style of right border of an element. border-right-style: inset; border-top shorthand property for setting top border of an element. border-top: 4px outset teal; border-top-color Sets the color of top border of an element. border-top-color: navy; border-top-width Sets the width of top border of an element. border-top-width: 6px; border-top-style Sets the style of top border of an element. border-top-style: solid; border-image shorthand property for setting border image. border: 10px solid transparent; border-image: url(border.png) 30 round; border-image-outset Sets the image outset i.e how much the border image area extends beyond the border box. border: 10px solid transparent; border-image: url(border.png) 30 round; border-image-outset: 2; border-image-repeat This property determines whether the border image should be repeated, rounded, spaced or stretched. border: 10px solid transparent; border-image: url(border.png) 30; border-image-repeat: stretch; border-image-source Sets the source/path of an image to be passed as a border to an element. border: 10px solid transparent; border-image-source: url(border.png); border-image-slice: 30; border-image-slice This property shows how to slice up an image in a border. border: 10px solid transparent; border-image: url(border.png) 30 fill; border-image-width Sets the width of the image to be set as a border. border: 10px solid transparent; border-image: url(border.png) 30; border-image-width: 20px; border-radius Rounds the corners of an element’s border border: 2px solid black; border-radius: 15px; border-inline Sets the border on the inline start and end sides (logical left/right depending on writing mode). border-inline: 4px solid red; border-block Sets the border on the block start and end sides (logical top/bottom depending on writing mode). border-block: 3px solid green; border-block-color Sets the color of the block‑start and block‑end borders (logical top and bottom). border-block-color: blue; border-block-style Defines the style (solid, dashed, dotted, etc.) of the block‑start and block‑end borders. border-block-style: dashed; border-block-width Sets the thickness of the block‑start and block‑end borders (logical top and bottom). border-block-width: 5px; border-inline-color Defines the color of the inline start and end borders (logical left and right). border-inline-color: purple; border-inline-style Specifies the style (solid, dashed, dotted, etc.) of the inline start and end borders. border-inline-style: dotted; border-inline-width Controls the thickness of the inline start and end borders. border-inline-width: 6px; border-collapse (for tables) Controls whether table borders are collapsed into a single border or separated. border-collapse: collapse; border-spacing (for tables) Defines the space between table cell borders when borders are separated. border-spacing: 10px;