Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptHow to Change Border Width of Div in JavaScript ?

How to Change Border Width of Div in JavaScript ?

Div elements are essential in creating modern web applications, and manipulating their properties dynamically can enhance user experience. In this article, we’ll see how to change the border width of a div element using JavaScript.

The border-width property in CSS is used to specify the width of the border around an HTML element. It takes values in pixels, ems, rems, or percentages, and it can be set independently for each side of the border. To change the border width of a div element using JavaScript, we need to target the div element and set its border width property.

Syntax:

element.style.borderWidth = "px"

Example 1: In the following example, we first get the div element with the class name called card using its ID using the document.getElementById() method. We then set the borderWidth style property of the element to 5px using the style.borderWidth.

HTML




<!DOCTYPE html>
<html lang="en">
   
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>neveropen</title>
    <style>
        .card {
            border: 1px solid green;
            padding: 10px;
            margin: 10px;
            width: 50%;
        }
    </style>
</head>
 
<body>
    <div class="card" id="myCard">
        <div class="card-body">
            <h1 class="card-title">
                Welcome To Geeksforneveropen!!
            </h1>
        </div>
    </div>
   
    <script>
        let myCard = document.getElementById("myCard");
        myCard.style.borderWidth = "5px";
    </script>
</body>
   
</html>


Output:

 

Example 2: In the following example, we first get the div element by its ID. We then set borderTopWidth to 2px, borderTopWidth to 4px, borderRightWidth to 8px, and borderLeftWidth to 12px.

HTML




<!DOCTYPE html>
<html lang="en">
   
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>neveropen</title>
    <style>
        div {
            padding: 10px;
            width: 60%;
            border: 2px solid green;
            border-radius: 10px;
        }
    </style>
</head>
 
<body>
    <div id="gfg">
        <h1>Welcome To Geeksforneveropen!!</h1>
        <p>
            A Computer Science Portal for neveropen.
            It contains well written, well thought
            & well explained computer science & pro-
            gramming articles.
        </p>
    </div>
   
    <script>
        let myDiv = document.getElementById("gfg");
        myDiv.style.borderTopWidth = "2px";
        myDiv.style.borderRightWidth = "4px";
        myDiv.style.borderBottomWidth = "8px";
        myDiv.style.borderLeftWidth = "12px";
    </script>
</body>
   
</html>


Output:

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments