The borderWidth
property sets or gets the width of an element's border.
border-width
can have one to four values.
border-width:thin medium thick 10px;
is equal to
border-width:thin medium thick;
is equal to
border-width:thin medium;
is equal to
border-width:thin;
is equal to all four borders are thin.
borderWidth |
Yes | Yes | Yes | Yes | Yes |
Return the borderWidth property:
var v = objectstyle.borderWidth
Set the borderWidth property:
object.style.borderWidth='thin|medium|thick|length|initial|inherit'
Value | Description |
---|---|
thin | thin border |
medium | medium border. Default |
thick | thick border |
length | Set the thickness of the border |
inherit | Inherit the border width from the parent element |
Default Value: | medium |
---|---|
Return Value: | A string representing the width of an element's border |
CSS Version | CSS1 |
The following code shows how to change the width of the four borders of a <div> element
<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {<!-- www .ja v a 2 s . co m-->
border-style: solid;
}
</style>
</head>
<body>
<div id="myDiv">This is a div element.</div>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myDiv").style.borderWidth = "thick";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the width of the top and bottom border to thick, and the left and right border to thin of a <div> element.
<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {<!-- ww w. j a v a 2 s .co m-->
border-style: solid;
}
</style>
</head>
<body>
<div id="myDiv">This is a div element.</div>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myDiv").style.borderWidth = "thick thin";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the width of the four borders of a <div> element, using the length value.
<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {<!-- ww w .ja v a2s .c o m-->
border-style: solid;
}
</style>
</head>
<body>
<div id="myDiv">This is a div element.</div>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myDiv").style.borderWidth = "1px 5px 10px 20px";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the width of the border of a <div> element.
<!DOCTYPE html>
<html>
<body>
<!--from ww w .j ava 2 s . co m-->
<div id="myDiv" style="border:1px solid black">This is a div element.</div>
<br>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
console.log(document.getElementById("myDiv").style.borderWidth);
}
</script>
</body>
</html>
The code above is rendered as follows: