CSS Borders Explained
Master CSS borders with complete control over width, style, and color. Learn the border shorthand property, all 9 border-style values, border-radius for rounded corners, and individual side borders. Build polished, professional UIs with advanced border techniques.
- What is a Border?
- Border Width
- Border Style
- Border Color
- Border Shorthand
- Individual Sides
- Border Radius
- Working Examples
- Try It
- FAQ
What is a Border?
A border is a line that appears around the edge of an HTML element. It sits between the element's padding and margin (the CSS box model). Borders are used to:
- Separate content — visually define where one element ends
- Draw attention — highlight important content with colored borders
- Create rounded corners — using border-radius
- Polish the design — make layouts look modern and professional
Width
Controls how thick the border is — px, em, rem, or thin/thick keywords.
Style
The border pattern — solid, dashed, dotted, double, groove, ridge, etc.
Color
The border color — any CSS color: keywords, hex, rgb, hsl, or transparent.
Radius
Rounds the corners — from subtle curves to perfect circles.
Border Width
The border-width property sets how thick the border is. You can use pixels, ems, rems, or the keywords thin, medium, and thick.
div { border-width: 3px; /* pixels */ } p { border-width: 0.2em; /* em units */ } .box1 { border-width: thin; /* keyword */ } .box2 { border-width: medium; } .box3 { border-width: thick; }
Border Style
The border-style property defines the appearance of the border. You must set a style or the border won't show at all — even if you've set width and color.
All 9 border-style values:
.s-none { border: 3px none #2563eb; } /* no border */ .s-solid { border: 3px solid #2563eb; } /* solid line */ .s-dashed { border: 3px dashed #2563eb; } /* dashes */ .s-dotted { border: 3px dotted #2563eb; } /* dots */ .s-double { border: 3px double #2563eb; } /* double line */ .s-groove { border: 3px groove #2563eb; } /* carved in */ .s-ridge { border: 3px ridge #2563eb; } /* raised out */ .s-inset { border: 3px inset #2563eb; } /* pressed in */ .s-outset { border: 3px outset #2563eb; } /* pushed out */
Visual examples:
Border Color
The border-color property sets the border color. Any CSS color format works: keywords, hex, rgb, rgba, hsl, or the special keyword transparent.
.c1 { border-color: red; } /* keyword */ .c2 { border-color: #2563eb; } /* hex */ .c3 { border-color: rgb(37, 99, 235); } /* rgb */ .c4 { border-color: rgba(37, 99, 235, 0.5); } /* semi-transparent */ .c5 { border-color: hsl(217, 95%, 54%); } /* hsl */ .c6 { border-color: transparent; } /* invisible */
Border Shorthand
Instead of writing three separate properties, use the border shorthand to set width, style, and color in one line. This is the most common way to write borders in practice.
.box { /* border: width style color; */ border: 2px solid #2563eb; } .card { border: 3px dashed #7c3aed; } .input { border: 1px solid #cbd5e1; }
Width → Style → Color
The order is fixed: border: 2px solid blue; Order doesn't matter between the values, but this is the convention.
Style is Mandatory
You must include the style or the border won't display. Width and color are optional if a default applies.
All Sides Equally
The border shorthand applies the same border to all four sides. Use individual properties for different sides.
Individual Border Sides
You can style each side (top, right, bottom, left) separately using border-top, border-right, border-bottom, and border-left.
.box { border-top: 4px solid #2563eb; border-right: 4px dashed #7c3aed; border-bottom: 4px dotted #06b6d4; border-left: 4px solid #059669; }
Border Radius
The border-radius property rounds the corners of an element. It's what makes modern, friendly-looking designs possible. Set a radius value (px, em, rem, %) and corners curve smoothly.
.r1 { border-radius: 8px; /* all corners equally */ border: 2px solid #2563eb; padding: 1rem; } .r2 { border-radius: 20px; /* more rounded */ border: 2px solid #7c3aed; } .r3 { border-radius: 50%; /* perfect circle */ width: 100px; height: 100px; border: 3px solid #06b6d4; } .r4 { /* different radius per corner */ border-radius: 10px 20px 30px 40px; /* TL TR BR BL */ border: 2px solid #059669; }
Individual Corner Radius
Use specific properties to round individual corners: border-top-left-radius, border-top-right-radius, border-bottom-right-radius, border-bottom-left-radius.
Or use the shorthand with four values: border-radius: 10px 20px 30px 40px; applies to top-left, top-right, bottom-right, bottom-left in that order.
Working Examples
Example 1: Simple Card with Border
<!DOCTYPE html> <html lang="en"> <head> <style> .card { border: 2px solid #2563eb; border-radius: 12px; padding: 1.5rem; max-width: 300px; background: #f8fafc; } </style> </head> <body> <div class="card"> <h3>My Card</h3> <p>This is a styled card with a border and rounded corners.</p> </div> </body> </html>
Example 2: Input Field with Focus Border
<!DOCTYPE html> <html lang="en"> <head> <style> input { border: 1px solid #cbd5e1; border-radius: 6px; padding: 8px 12px; font-size: 16px; } input:focus { border: 2px solid #2563eb; outline: none; } </style> </head> <body> <input type="text" placeholder="Click me to see focus border"> </body> </html>
Example 3: Colored Top Border
<!DOCTYPE html> <html lang="en"> <head> <style> .alert { border-top: 4px solid #dc2626; background: #fef2f2; padding: 1rem; border-radius: 4px; } </style> </head> <body> <div class="alert"> <p>This is an alert! Notice the colored top border.</p> </div> </body> </html>
CSS Borders Questions
Common questions about borders: