CSS Borders

CSS Borders

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

Example Border

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

Example Border 2

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

Example Border
Example Border

Rounded Borders (border-radius)

The border-radius property is used to add rounded corners to an element.

  • Values: px, %, or em.
  • 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

Example Border
Example Border

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

Example Border

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

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;
        }

Output

Example Border

Summary Table: All CSS Border Properties

All the css border properties are listed in the table below:

PropertyDescriptionExample
bordershorthand property for setting all the border properties in one declarationborder: 1px solid #000;
border-colorshorthand property for setting border color of an element.border-color: blue;
border-styleshorthand property for setting style (solid / dashed) of border of an elementborder-style: dashed;
border-widthshorthand property for setting border width of an element.border-width: medium;
border-bottomshorthand property for setting bottom border of an element.border-bottom: 3px solid blue;
border-bottom-colorSets the color of bottom border of an elementborder-bottom-color: green;
border-bottom-widthSets the width of bottom border of an element.border-bottom-width: 4px;
border-bottom-styleSets the style of bottom border of an element.border-bottom-style: dotted;
border-leftshorthand property for setting left border of an element.border-left: 6px double purple;
border-left-colorSets the color of left border of an element.border-left-color: orange;
border-left-widthSets the width of left border of an element.border-left-width: 8px;
border-left-styleSets the style of left border of an element.border-left-style: groove;
border-rightshorthand property for setting right border of an element.border-right: 5px ridge brown;
border-right-colorSets the color of right border of an element.border-right-color: pink;
border-right-widthSets the width of right border of an element.border-right-width: 7px;
border-right-styleSets the style of right border of an element.border-right-style: inset;
border-topshorthand property for setting top border of an element.border-top: 4px outset teal;
border-top-colorSets the color of top border of an element.border-top-color: navy;
border-top-widthSets the width of top border of an element.border-top-width: 6px;
border-top-styleSets the style of top border of an element.border-top-style: solid;
border-imageshorthand property for setting border image.border: 10px solid transparent; border-image: url(border.png) 30 round;
border-image-outsetSets 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-repeatThis 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-sourceSets 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-sliceThis property shows how to slice up an image in a border.border: 10px solid transparent; border-image: url(border.png) 30 fill;
border-image-widthSets 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-radiusRounds the corners of an element’s borderborder: 2px solid black; border-radius: 15px;
border-inlineSets the border on the inline start and end sides (logical left/right depending on writing mode).border-inline: 4px solid red;
border-blockSets the border on the block start and end sides (logical top/bottom depending on writing mode).border-block: 3px solid green;
border-block-colorSets the color of the block‑start and block‑end borders (logical top and bottom).border-block-color: blue;
border-block-styleDefines the style (solid, dashed, dotted, etc.) of the block‑start and block‑end borders.border-block-style: dashed;
border-block-widthSets the thickness of the block‑start and block‑end borders (logical top and bottom).border-block-width: 5px;
border-inline-colorDefines the color of the inline start and end borders (logical left and right).border-inline-color: purple;
border-inline-styleSpecifies the style (solid, dashed, dotted, etc.) of the inline start and end borders.border-inline-style: dotted;
border-inline-widthControls 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;