CSS Borders
Learn how to style element borders using CSS border properties
What are CSS Borders?
CSS borders create lines around elements, between the padding and margin. Borders can be styled with different colors, widths, and styles, and can have rounded corners.
Border Properties:
- border - Shorthand for all border properties
- border-width - Sets the border thickness
- border-style - Sets the border style (solid, dashed, etc.)
- border-color - Sets the border color
- border-radius - Rounds the corners of an element's border
- border-top, border-right, border-bottom, border-left - Individual side borders
- border-image - Uses an image as the border
- border-collapse - Controls table border behavior
- border-spacing - Sets distance between table borders
Borders in Action
Example Code
/* Border Properties */ .element { /* Shorthand border property */ border: 2px solid black; /* Individual border properties */ border-width: 2px; border-style: solid; border-color: black; /* Border sides */ border-top: 3px dashed red; border-right: 2px dotted blue; border-bottom: 4px double green; border-left: 1px groove #ccc; /* Border radius */ border-radius: 10px; border-top-left-radius: 5px; border-top-right-radius: 15px; border-bottom-right-radius: 20px; border-bottom-left-radius: 25px; } /* Border styles */ .styles { border-style: solid; /* Solid line */ border-style: dashed; /* Dashed line */ border-style: dotted; /* Dotted line */ border-style: double; /* Double line */ border-style: groove; /* 3D grooved */ border-style: ridge; /* 3D ridged */ border-style: inset; /* 3D inset */ border-style: outset; /* 3D outset */ border-style: none; /* No border */ border-style: hidden; /* Hidden border */ } /* Advanced border examples */ .advanced { /* Multiple borders with box-shadow */ box-shadow: 0 0 0 2px red, 0 0 0 4px blue; /* Outline (different from border) */ outline: 3px solid orange; outline-offset: 5px; }
Border Properties in Detail
Border Width
The border-width
property sets the thickness of the border. It can be specified using various units.
Border Color
The border-color
property sets the color of the border. It accepts all CSS color values.
Border Shorthand
The border
shorthand property combines width, style, and color in one declaration.
Border Styles
Common Border Styles
3D Border Styles
Special Border Values
Border Radius
The border-radius
property allows you to round the corners of an element. You can specify different radii for each corner, and use various units including pixels, percentages, and more.
Uniform Border Radius
Different Radii per Corner
Creating Circles and Ovals
You can create perfect circles and ovals using border-radius with percentage values.
Individual Border Sides
CSS allows you to style each border side individually, giving you precise control over your element's appearance.
Individual Properties
Logical Properties
For internationalization and responsive design, use logical properties that adapt to text direction.
Outline vs Border
Borders
- Part of the element's box model
- Affects layout and element dimensions
- Can have rounded corners with border-radius
- Can be applied to individual sides
- Participates in box-sizing calculation
Outlines
- Drawn outside the element's border
- Does not affect layout or dimensions
- Cannot have rounded corners
- Always drawn around all four sides
- Often used for accessibility focus indicators
Border Image
The border-image
property allows you to use an image as the border of an element, replacing the conventional border styles.
Border Image Properties
Border Image Slice
The border-image-slice
property divides the border image into nine regions: four corners, four edges, and the middle.
Table Borders
Border Collapse
The border-collapse
property determines whether table borders are collapsed into a single border or separated.
Border Spacing
The border-spacing
property sets the distance between the borders of adjacent table cells (when border-collapse is separate).
Practical Border Examples
Card Components
Card Title
This is an example card with a subtle border.