Open In App

How to Set Border Width in CSS?

The CSS border-width property is used to set the border of an element. This property allows you to specify the width of the border for all four sides of an element or individually for each side. Here, we will cover all possible approaches with detailed explanations and code examples.

Setting Border Width for All Sides

You can set the border width for all sides of an element using the border-width property. This property accepts one, two, three, or four values to specify the width of the border for the top, right, bottom, and left sides, respectively.

Syntax:

/* Set border width for all sides */
.example {
border: 2px; /* 2 pixels for all sides */
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>Border Width</title>

    <style>
        /* Set border width for all sides */
        .GFG {
            
            /* 2 pixels width for all sides */
            border: 2px solid black;
            padding: 10px;
        }
    </style>
</head>

<body>
    <div class="GFG">
        Welcome to GeeksforGeeks
    </div>
</body>

</html>

Output:

border-width

Setting Border Width for Individual Sides

You can also set the border width for individual sides of an element using the border-top-width, border-right-width, border-bottom-width, and border-left-width properties.

/* Set border width for individual sides */
.example {
border-top: 2px; /* 2 pixels for top side */
border-right: 4px; /* 4 pixels for right side */
border-bottom: 6px; /* 6 pixels for bottom side */
border-left: 8px; /* 8 pixels for left side */
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>Border Width</title>

    <style>
        .GFG {
            
            /* 2 pixels for top side */
            border-top: 2px solid black;;
            
            /* 4 pixels for right side */
            border-right: 4px solid black;;
            
            /* 6 pixels for bottom side */
            border-bottom: 6px solid black;;
            
            /* 8 pixels for left side */
            border-left: 8px solid black;
            padding: 10px;
        }
    </style>
</head>

<body>
    <div class="GFG">
        Welcome to GeeksforGeeks
    </div>
</body>

</html>

Output:

border-width-2

Setting Border Width using Shorthand

The border-width property can also be used as a shorthand to set the border width for all sides in a single declaration. You can specify one, two, three, or four values to set the width for the top, right, bottom, and left sides, respectively.

/* Set border width using shorthand */
.example {
border-width: 2px 4px 6px 8px; /* Top, right, bottom, left */
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>Border Width</title>

    <style>
        .GFG {
            
            /* Top, right, bottom, left */
            border-width: 2px 4px 6px 8px;
            
            /* Required for border-width shorthand */
            border-style: solid;
            border-color: black;
            
            padding: 10px;
        }
    </style>
</head>

<body>
    <div class="GFG">
        Welcome to GeeksforGeeks
    </div>
</body>

</html>

Output:

border-width-2

Article Tags :