Open In App

CSS Borders

CSS borders are essential elements in websites, representing the edges of various components and elements. CSS Borders refer to the lines that surround elements, defining their edges. Borders can be styled, colored, and sized using CSS properties such as border style, border color, border width, and border radius. borders can be styled with the top border, the right border, the bottom border, and the left border. 

In this Borders in CSS article, we will learn about CSS borders, covering styling options, practical use cases, and best practices.

Border Properties

CSS provides several properties to customize borders:

  1. border-style: Determines the type of border (e.g., solid, dashed, dotted).
  2. border-width: Sets the width of the border (in pixels, points, or other units).
  3. border-color: Specifies the border color.
  4. border-radius: Creates 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

5. Border radius property

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. 

Following are the types of borders:

Examples of CSS border Style

In this example we are going to use CSS border-style property.

<!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

Explanation:

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 of Border Width

Here is the basic example of using CSS border width property.

<!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

Explanation:

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 of CSS Border Color

Here we are implementing above explained border color property.

<!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

Explanation:

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.

CSS Border individual sides Syntax: 

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

CSS Border individual sides Example:

In this example, we set border-top-style as dotted in h2.

<!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

Explanation:

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.

Border radius property Syntax:

border-radius: value;

Example of Border radius property

Here is the bsic example of using border-radius property.

<!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

Explanation:

Practical Use Cases

CSS borders find application in various scenarios:

CSS Borders Use Cases:

1. How to define the color of the border using CSS ?

to define the color of border we use the border-color property in CSS to define the color of the border.

2. How to specify the double border using CSS ?

Here we Use the border-style property set to double and adjust the border-width property to control the thickness of the double border.

3.How to Add Border Around Text using CSS ?

Here we use the border property with values for style, width, and color to add a border around text in CSS.

4. How to create a Border around an HTML Element using CSS ?

To create a border around an HTML element using CSS, use the border property to specify style, width, and color.

5.How to create a hidden border using CSS properties ?

To create a hidden border using CSS properties, set the border-style property to hidden or none to hide the border.

CSS Borders Supported Browsers:

Article Tags :