← Back to Chapters

CSS Borders

? CSS Borders

? Quick Overview

  • border is a shorthand that sets border width, style and color in one line.
  • You can also control each side separately (top, right, bottom, left).
  • border-radius is used to round the corners of the element.
  • Without a border-style, the border will not be visible.

? Key Concepts

  • Border Width – thickness of the border (e.g., 1px, 2px, 5px).
  • Border Style – visual style like solid, dashed, dotted, etc.
  • Border Color – color of the border; can be hex, named color, or rgba.
  • Shorthand vs Longhand – use border to set all at once, or properties like border-width, border-style, border-color individually.
  • Border Radius – controls corner rounding (e.g., 10px, 50% for circles).

? Syntax (Shorthand)

Basic shorthand syntax for setting all border properties at once:

? View Basic Border Syntax
/* Shorthand border syntax: width style color */
.card {
border: 2px solid #333;
}

? Examples

Here are a few common border styles you will use frequently:

? View Code Example
/* Different border styles and rounded corners */
.container {
border: 2px solid #333;
}

.dashed-border {
border: 3px dashed #2563eb;
}

.rounded-border {
border: 5px solid #16a34a;
border-radius: 10px;
}

? Common Border Properties

Property Description Example Value
border Shorthand for setting width, style and color. 2px solid #333
border-width Width of the border. 1px, 2px, 5px
border-style Style of the border line. solid, dashed, dotted
border-color Color of the border. #333, red, rgba(0, 0, 0, 0.2)
border-radius Radius of the element's corners. 10px, 50%

?️ Live Output / Visual Example

Example Box with Solid Border

This box has a 5px solid red border around it.

? View Example Markup
/* Simple bordered box example */
.box {
border: 5px solid red;
padding: 10px;
max-width: 260px;
margin: 12px auto 0;
}

? Tips & Best Practices

  • Use border-radius to create rounded corners and softer UI components.
  • Use the shorthand border property for cleaner, more readable CSS.
  • Always include a border-style (like solid) or your border will not appear.
  • Use lighter colors or rgba() for subtle, modern-looking borders.

? Try It Yourself

  1. Create three boxes with solid, dashed and dotted borders.
  2. Use different border-width values and see how the appearance changes.
  3. Make a circle using border-radius: 50%; on a square element.
  4. Style only one side of an element using border-top, border-right, border-bottom or border-left.