The border property sets or returns up to three separate border properties, in a shorthand form.
With this property, you can set/return one or more of the following (in any order):
Return the border property: object.style.border Set the border property: object.style.border = "width style color|initial|inherit"
Property Values
Parameter | Description |
---|---|
width | Sets the width of the borders |
style | Sets the style of the borders |
color | Sets the color of the borders |
initial | Sets this property to its default value. |
inherit | Inherits this property from its parent element. |
Border-Width
The borderWidth property sets or returns the width of an element's border.
This property can take from one to four values:
<style> #Div { border-style: solid; } </style> </head> <body> <div id="Div">Am div element.</div> <br> <button type="button" onclick="draw()">Change width of the four borders</button> <script> function draw() { document.getElementById("Div").style.borderWidth = "thick"; } </script>
In above example when i click button it will change the width of the four borders of a <div> element:
Border-style
The borderColor property sets or returns the color of an element's border.
This property can take from one to four values:
<style> #Div { border: thick solid blue; } </style> <div id="Div">This is a div.</div> <br> <button type="button" onclick="draw()">Change color of the four borders</button> <script> function draw() { document.getElementById("Div").style.borderColor = "red"; } </script>
The borderColor property sets or returns the color of an element's border.
This property can take from one to four values:
<style> #Div { border: thick solid blue; } </style> </head> <body> <div id="Div">This is a div.</div> <br> <button type="button" onclick="draw()">Change color of the four borders</button> <script> function draw() { document.getElementById("Div").style.borderColor = "red"; } </script>
In above example when you click button it will change the color of the four borders of a <div> element to red
<!DOCTYPE html> <html> <head> <title>Use CSS Style Border Properties In JavaScript</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <style> h1{ color: red; } #DIV1 { border-style: solid; } #DIV2 { border: thick solid blue; } #DIV3 { border-style: solid; } </style> <body> <div class="container"> <br> <div class="text-center"> <h1>Use CSS Style Border Properties In JavaScript</h1> </div> <br> <h3>Change color of the four borders</h3> <div class="col-md-4"> <div id="DIV1"> <h2>Hello</h2> </div> <br> <button class="btn btn-primary" type="button" onclick="draw1()">Draw</button> </div> <div class="col-md-4"> <div id="DIV2"> <h2>Hello</h2> </div> <br> <button class="btn btn-primary" type="button" onclick="draw2()">Draw</button> </div> <div class="col-md-4"> <div id="DIV3"> <h2>Hello</h2> </div> <br> <button class="btn btn-primary" type="button" onclick="draw3()">Draw</button> </div> </body> </html> <script> function draw1() { document.getElementById("DIV1").style.borderWidth = "thick"; } function draw2() { document.getElementById("DIV2").style.borderColor = ""; } function draw3() { document.getElementById("DIV3").style.borderColor = "pink"; } </script>