Javascript Reference - HTML DOM Style border Property








The border property sets or gets three separate border properties, in a shorthand form.

We can use border property to get and set the following border properties.

  • border-width
  • border-style
  • border-color




Browser Support

border Yes Yes Yes Yes Yes

Syntax

Return the border property:

var v = 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.




Technical Details

Default Value: not specified
Return Value: A String, representing the border width, style and/or color of an element
CSS Version CSS1

Example

The following code shows how to change the border width, style and color for a <div> element


<!DOCTYPE html>
<html>
<body>
<div id="myDiv">This is a div element.</div>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w ww. j  a v  a 2s.  c  o  m-->
    document.getElementById("myDiv").style.border = "thin dotted red";
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 2

The following code shows how to get the border property values from a <div> element.


<!DOCTYPE html>
<html>
<body>
<div id="myDiv" style="border:1px solid red;">This is a div element.</div>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!--  w  w  w.  j  av  a2s  . c om-->
    console.log(document.getElementById("myDiv").style.border);
}
</script>
</body>
</html>

The code above is rendered as follows: