CSS Borders
1. Introduction to CSS Borders
A border surrounds an HTML element. You can control its width, style, color, and shape using different CSS properties.
Main Border Properties
- border-width
- border-style
- border-color
- border-radius
- border-image
div{
border:2px solid black;
}
2. Border Width
The border-width property specifies the thickness of a border.
.box{
border-style:solid;
border-width:5px;
}
| Value | Description |
|---|---|
| thin | Thin border. |
| medium | Medium border (default). |
| thick | Thick border. |
| 5px | Custom border width. |
3. Border Style
The border-style property defines the appearance of a border.
.box{
border-style:dashed;
}
| Style | Result |
|---|---|
| solid | โโโโโโโโ |
| dashed | - - - - - - |
| dotted | ยทยทยทยทยทยทยทยทยทยท |
| double | โโโโโโโ |
| groove | 3D Groove effect |
| ridge | 3D Ridge effect |
| inset | Inset effect |
| outset | Outset effect |
| none | No border |
4. Border Color
The border-color property specifies the color of the border.
.box{
border-style:solid;
border-width:3px;
border-color:blue;
}
Another Example
.card{
border:2px solid #28a745;
}
| Supported Values |
|---|
| Color Name |
| HEX Value |
| RGB |
| RGBA |
| HSL |
| HSLA |
5. Border Shorthand Property
The border property combines border width, style, and color into a single declaration.
.box{
border:4px solid red;
}
Syntax
border: width style color;
Examples
border:2px dashed blue;
border:5px double green;
border:1px solid black;
6. Individual Borders (Top, Right, Bottom, Left)
CSS allows you to apply different borders to each side of an element independently.
.box{
border-top:4px solid red;
border-right:4px dashed blue;
border-bottom:4px double green;
border-left:4px dotted orange;
}
| Property | Description |
|---|---|
| border-top | Sets the top border. |
| border-right | Sets the right border. |
| border-bottom | Sets the bottom border. |
| border-left | Sets the left border. |
7. Rounded Borders (border-radius)
The border-radius property creates rounded corners on HTML elements.
.box{
border:2px solid blue;
border-radius:15px;
}
Circle Example
.circle{
width:150px;
height:150px;
border-radius:50%;
border:3px solid red;
}
| Value | Effect |
|---|---|
| 5px | Slightly rounded corners. |
| 15px | More rounded corners. |
| 50% | Creates a perfect circle (square element). |
8. Border Images
The border-image property allows an image to be used instead of a normal border.
.box{
border:20px solid transparent;
border-image:url("border.png") 30 round;
}
Border Image Properties
- border-image-source
- border-image-slice
- border-image-repeat
- border-image-width
9. Outline vs Border
Borders and outlines look similar but behave differently.
| Border | Outline |
|---|---|
| Takes up space. | Does not take up space. |
| Can be applied to each side separately. | Applies to the entire element. |
| Supports border-radius. | Does not follow border-radius in the same way. |
.box{
border:2px solid blue;
outline:3px dashed red;
}
10. Practical Border Examples
Example 1: Card Border
.card{
border:1px solid #ddd;
border-radius:10px;
}
Example 2: Button Border
button{
border:2px solid green;
border-radius:5px;
}
Example 3: Profile Image
img{
border:4px solid blue;
border-radius:50%;
}
11. CSS Border Best Practices
Borders should enhance the appearance of a webpage without making the design look cluttered.
- Use thin borders for a clean and modern design.
- Maintain consistent border styles throughout the website.
- Use border-radius for modern UI elements.
- Choose border colors that match your theme.
- Avoid using too many different border styles.
- Use subtle borders for cards and forms.
- Highlight important sections using thicker borders.
- Test borders on desktop and mobile devices.
12. Common Border Mistakes
| Mistake | Correct Practice |
|---|---|
| Forgetting border-style | Always specify a border style. |
| Using very thick borders | Use appropriate border widths. |
| Using too many border styles | Maintain consistency. |
| Poor border color contrast | Choose colors that match the design. |
| Ignoring border-radius | Use rounded corners for modern layouts. |
โ Incorrect
.box{
border-width:5px;
border-color:red;
}
โ Correct
.box{
border:5px solid red;
}
13. Browser Support
CSS border properties are supported by all modern browsers.
| Browser | Support |
|---|---|
| Google Chrome | โ Supported |
| Mozilla Firefox | โ Supported |
| Microsoft Edge | โ Supported |
| Safari | โ Supported |
| Opera | โ Supported |
14. CSS Border Interview Questions
- What is the purpose of CSS borders?
- Which property specifies the border style?
- What is the shorthand property for borders?
- Name different border styles available in CSS.
- What does border-radius do?
- How do you create a circular image using CSS?
- What is a border image?
- What is the difference between a border and an outline?
- Can different borders be applied to each side?
- Why is border-style important?
15. Lesson Summary
In this lesson, you learned:
- โ Introduction to CSS Borders.
- โ Border Width.
- โ Border Style.
- โ Border Color.
- โ Border Shorthand Property.
- โ Individual Borders.
- โ Rounded Borders.
- โ Border Images.
- โ Outline vs Border.
- โ Practical Border Examples.
- โ Best Practices and Common Mistakes.
Quick Quiz
Which CSS property creates rounded corners?