The Wayback Machine - https://web.archive.org/web/20241218085928/https://www.geeksforgeeks.org/css-borders/
Open In App

CSS Borders

Last Updated : 27 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A CSS border is a line that surrounds an HTML element, providing a visual separation between the element and its surroundings. Borders can be styled in various ways, including adjusting their width, color, and style. The basic properties used to define borders in CSS are border-width, border-style, and border-color.

Here, We covered CSS borders, including styling options, practical use cases, and best practices.

Syntax

The simplest way to add a border to an element is by using the border shorthand property. Here’s the basic syntax:

element {
border: 1px solid black;
}

In this example, the border is defined as 1 pixel wide, solid in style, and black in color.

CSS Border Properties

CSS provides several properties to control and customize borders:

PropertyDescription
border-styleDetermines the type of border (e.g., solid, dashed, dotted).
border-widthSets the width of the border (in pixels, points, or other units).
border-colorSpecifies the border color.
border-radiusCreates rounded corners for elements.

Ways to Style Border in CSS

The CSS border property enables the styling of an element’s border by setting its width, style, and color, allowing for customizable visual boundaries in web design.

1. Border Style

2. Border Width

3. Border Color

4. Border individual sides

CSS allows you to style each side of a border individually, giving flexibility in design.

5. Border Radius Property

The border-radius property allows you to round the corners of an element, creating smoother edges.

Common Border Styles

The border-style property specifies the type of border. None of the other border properties will work without setting the border style. 

Border StyleDescription
DottedCreates a series of dots.
DashedForms a dashed line.
SolidProduces a continuous line.
DoubleRenders two parallel lines.
GrooveCreates 3D grooved effect.
RidgeCreates 3D ridged effect.
InsetAdds 3D inset border.
OutsetAdds 3D outset border.
NoneRemoves the border.
HiddenHides the border.

Example: Using Different Border Styles

In this example Paragraphs feature varied border styles (dotted, dashed, solid, double) via CSS classes (.dotted, .dashed, .solid, .double). This enhances visual presentation, demonstrating distinct styles for each paragraph.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p.dotted {
            border-style: dotted;
        }

        p.dashed {
            border-style: dashed;
        }

        p.solid {
            border-style: solid;
        }

        p.double {
            border-style: double;
        }
    </style>
</head>

<body>
    <h2>The border-style Property</h2>

    <p>Geeksforgeeks</p>


    <p class="dotted">A dotted border.</p>

    <p class="dashed">A dashed border.</p>

    <p class="solid">A solid border.</p>

    <p class="double">A double border.</p>

</body>

</html>

Output: 

CSS border-style Example Output

CSS Border Width

Border width sets the width of the border. The width of the border can be in px, pt, cm or thin, medium, and thick.

Example:

In this example Two paragraphs have an 8-pixel solid border defined by CSS. This border enhances visual separation, and its thickness can be adjusted using the `border-width` property for a desired effect.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            border-style: solid;
            border-width: 8px;
        }
    </style>
</head>

<body>
    <p>
        Geeksforgeeks
    </p>
    <p>
        Border properties
    </p>
</body>

</html>

Output: 

CSS Border Width Example Output

CSS Border Color

This property is used to set the color of the border. Color can be set using the color name, hex value, or RGB value. If the color is not specified border inherits the color of the element itself.

Example:

In this example Two paragraphs have a solid red border set with the `border-color` property, enhancing visual distinction. The border color can be customized by specifying different color values.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            border-style: solid;
            border-color: red
        }
    </style>
</head>

<body>
    <p>
        Geeksforgeeks
    </p>
    <p>
        Border properties:color
    </p>
</body>

</html>

Output: 

CSS Border Color Example Output

CSS Border individual sides

Using border property, we can provide width, style, and color to all the borders separately for that we have to give some values to all sides of the border.

Syntax: 

border-top-style : dotted;
border-bottom-width: thick;
border-right-color: green;

Example:

In this example, A single <h2> heading features a dotted top border set with the `border-top-style` property. CSS allows various border styles to enhance element appearance.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        h2 {
            border-top-style: dotted;
        }
    </style>
</head>

<body>
    <h2>Welcome to GeeksforGeeks</h2>
</body>

</html>

Output:

CSS Border individual sides Example Output

Border radius property

The CSS border-radius property rounds the corners of an element’s border, creating smoother edges, with values specifying the curvature radius.

Syntax:

border-radius: value;

Example :

In this example A single <h1> heading has a solid border, centered text (using `text-align`), a green background, and rounded corners (using `border-radius`), creating a visually appealing header element.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        h1 {
            border-style: solid;
            text-align: center;
            background: green;
            border-radius: 20px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
</body>

</html>

Output:

Border radius property Example Output

Practical Use Cases of CSS Borders

CSS borders are commonly used in the following scenarios:

  • Styling Buttons : Borders enhance button designs, making them more visually appealing and clickable.
  • Creating Dividers : Borders can act as separators between content sections, providing clear visual breaks.
  • Customizing Images : Apply borders around images to frame them, making thumbnails stand out.
  • Designing Navigation Menus : Borders can define the boundaries of navigation links or items

CSS Borders Supported Browsers

Note: Older versions of Internet Explorer may not fully support advanced features like border-radius or certain border styles (e.g., groove, ridge), so it’s advisable to test your design across different browsers

CSS Borders – FAQs

How do I control borders in CSS?

You can control borders using properties like border-width, border-style, border-color, and border-radius for customization.

Can you add 2 borders in CSS?

Yes, you can add two borders using a combination of outline and border, or by nesting elements with different borders.

How many border styles are in CSS?

CSS offers 10 border styles, including solid, dashed, dotted, double, groove, ridge, inset, outset, none, and hidden.

What is CSS outline?

The outline property in CSS adds a line around an element without affecting its size or layout, often used for accessibility.

How to blur a border in CSS?

You can blur a border using the box-shadow property with a blur radius, such as box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);



Next Article

Similar Reads

three90RightbarBannerImg