padding Property - Javascript CSS Style Property

Javascript examples for CSS Style Property:padding

Description

The padding property sets or gets the padding of an element.

This property can take from one to four values:

  • If one value is set, this padding sets all 4 sides.
  • If two values are set, the first value sets top and bottom, the second value sets the right and left side.
  • Three values set the top, horizontal (right and left) and bottom side.
  • Four values set to the top, right, bottom, left side in that order.

Property Values

Value Description
% padding in percentage of the width of the parent element
length padding in length units
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

Technical Details

Item Value
Default Value: 0
Return Value: A String, representing the padding of an element
CSS VersionCSS1

Set the padding of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {//from   www.j ava2s  .c o  m
    border: 1px solid #FF0000;
}
</style>
</head>
<body>

<div id="myDiv" style="padding-top:5cm;">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Test</button>

<script>
function myFunction() {
    document.getElementById("myDiv").style.padding = '10px 20px 30px 40px';
}
</script>

</body>
</html>

Related Tutorials