CSS Borders

CSS Borders are used to create visible lines around HTML elements. Borders help separate content, highlight important sections, and improve the overall appearance of a webpage.

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;

}
Borders improve page structure and make elements easier to identify.

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.
You can specify the border width using pixels, em, rem, or predefined values.

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
Without a border style, the border will not be visible even if width and color are specified.

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
Border colors can be specified using any valid CSS color format.

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;
The shorthand property makes CSS cleaner and reduces the amount of code.

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.
Individual borders are useful for creating stylish cards, menus, and section dividers.

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).
Rounded corners are widely used in modern web design.

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
Border images are useful for decorative frames and creative layouts.

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;

}
Outlines are commonly used to highlight focused form elements.

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%;

}
Borders help create attractive cards, buttons, profile pictures, forms, and navigation menus.

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.
Consistent borders improve the overall user experience and make layouts more professional.

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;

}
A border will not appear unless a valid border-style is specified.

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
Basic border properties work consistently across all modern browsers.

14. CSS Border Interview Questions

  1. What is the purpose of CSS borders?
  2. Which property specifies the border style?
  3. What is the shorthand property for borders?
  4. Name different border styles available in CSS.
  5. What does border-radius do?
  6. How do you create a circular image using CSS?
  7. What is a border image?
  8. What is the difference between a border and an outline?
  9. Can different borders be applied to each side?
  10. Why is border-style important?
These questions are commonly asked in HTML & CSS interviews and practical exams.

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.
Congratulations! You have successfully completed the CSS Borders lesson. You can now create stylish borders using different widths, styles, colors, rounded corners, and border images.

Quick Quiz

Which CSS property creates rounded corners?