CSS Borders
Learn to add borders around elements with different styles, colors, and effects
CSS Border Properties
CSS borders create visible lines around elements. You can control their width, style, color, and shape.
Border Width
Control thickness of borders
Border Style
Solid, dashed, dotted, and more
Border Color
Any color for your borders
Border Radius
Rounded corners and shapes
🔹 Basic Border Syntax
Use the border shorthand to define width, style, and color simultaneously. Example: border: 2px solid #333. Borders create visual separation, define boundaries, and enhance component design. They improve content structure and readability when used consistently, helping users navigate your content more effectively—which supports better engagement metrics for SEO.
/* Basic border syntax */
.box {
border: 2px solid red;
/* width style color */
padding: 20px;
}
Result:
🔹 Border Styles
CSS offers multiple border styles: solid, dashed, dotted, double, and more. Each style creates different visual effects for separating content, highlighting elements, or decorative purposes. Choose styles that enhance usability without distracting from content. Well-implemented borders improve visual hierarchy and content organization, making pages more scannable and user-friendly for better SEO performance.
.solid { border: 3px solid #333; }
.dashed { border: 3px dashed #333; }
.dotted { border: 3px dotted #333; }
.double { border: 3px double #333; }
.groove { border: 5px groove #333; }
.ridge { border: 5px ridge #333; }
.inset { border: 5px inset #333; }
.outset { border: 5px outset #333; }
Result:
🔹 Individual Border Sides
You can style each side of a border individually.
/* Individual sides */
.custom-borders {
border-top: 3px solid red;
border-right: 5px dashed blue;
border-bottom: 2px dotted green;
border-left: 4px double orange;
padding: 20px;
}
Result:
Individual Properties
/* Separate width, style, color */
.element {
border-width: 2px 4px 2px 4px; /* top right bottom left */
border-style: solid dashed solid dashed;
border-color: red blue red blue;
}
🔹 Border Radius (Rounded Corners)
Create rounded corners with the border-radius property. This softens element edges, creating modern, friendly interfaces. Use specific values for each corner or shorthand for uniform rounding. Rounded corners improve aesthetic appeal and visual flow between elements, contributing to a more polished user experience that encourages longer visit durations and lower bounce rates.
/* Rounded corners */
.rounded-small { border-radius: 5px; }
.rounded-medium { border-radius: 15px; }
.rounded-large { border-radius: 25px; }
.circle { border-radius: 50%; } /* Perfect circle */
/* Individual corners */
.custom-radius {
border-radius: 10px 20px 30px 40px;
/* top-left top-right bottom-right bottom-left */
}
Result:
🔹 Border Colors
Use any CSS color format for borders.
/* Different color formats */
.color-name { border: 3px solid red; }
.color-hex { border: 3px solid #ff6b35; }
.color-rgb { border: 3px solid rgb(255, 107, 53); }
.color-rgba { border: 3px solid rgba(255, 107, 53, 0.7); }
.color-hsl { border: 3px solid hsl(15, 100%, 60%); }
/* Gradient borders (modern browsers) */
.gradient-border {
border: 3px solid;
border-image: linear-gradient(45deg, #ff6b35, #f7931e) 1;
}
Result:
🔹 Border Width
Control border thickness with different units.
/* Different widths */
.thin { border: thin solid #333; } /* Browser default thin */
.medium { border: medium solid #333; } /* Browser default medium */
.thick { border: thick solid #333; } /* Browser default thick */
/* Pixel values */
.width-1 { border: 1px solid #333; }
.width-3 { border: 3px solid #333; }
.width-5 { border: 5px solid #333; }
.width-10 { border: 10px solid #333; }
Result:
🔹 Practical Examples
Card with Border
.card {
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
background: white;
}
Result:
Card Title
This is a card with a subtle border and shadow effect.
Button with Border
.button {
border: 2px solid #007bff;
background: transparent;
color: #007bff;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background: #007bff;
color: white;
}
Result:
Image with Border
.profile-image {
border: 4px solid white;
border-radius: 50%;
box-shadow: 0 0 0 2px #007bff;
width: 100px;
height: 100px;
}
🔹 Border Shorthand
Border Shorthand Order:
border: [width] [style] [color];
/* Longhand */
.element {
border-width: 2px;
border-style: solid;
border-color: red;
}
/* Shorthand */
.element {
border: 2px solid red;
}