CSS Borders

CSS borders are used to define the edges of HTML elements. They allow you to set border width, style, color, radius, and even create fancy effects like gradients and hover transitions.

Border Width

The border-width property sets the thickness of a border.

div {
  border-width: 2px;
}

Border Style

The border-style property sets the type of border. Common values include solid, dashed, dotted, double, and none.

div {
  border-style: solid;
}

Border Color

The border-color property sets the color of the border.

div {
  border-color: #333333;
}

Border Shorthand Property

You can set width, style, and color in a single line using the border shorthand property.

div {
  border: 2px solid #007BFF;
}

Border Radius

The border-radius property allows you to create rounded corners.

div {
  border-radius: 12px;
}

Circle Borders

By using border-radius: 50%;, you can make elements perfectly round.

img.avatar {
  border-radius: 50%;
  border: 2px solid #333;
}

Dashed and Dotted Borders

CSS allows dashed or dotted border styles for decorative purposes.

div.dashed {
  border: 2px dashed #28A745;
}

div.dotted {
  border: 2px dotted #FFC107;
}

Hover Effects on Borders

You can create interactive hover effects using border-color changes and shadows.

div.hover-border {
  border: 2px solid #6c757d;
  transition: all 0.3s ease;
}

div.hover-border:hover {
  border-color: #DC3545;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

Gradient Borders

CSS gradients can be applied to borders using border-image.

div.gradient-border {
  border: 3px solid;
  border-image-slice: 1;
  border-image-source: linear-gradient(45deg, #FF6B6B, #556270);
  border-radius: 10px;
}

Best Practices for Borders

Common Mistakes with Borders

Tip: Use border-radius and border shorthand together for clean and maintainable code.