You are here: Reference > JavaScript > client-side > style handling > properties > padding

padding style property

Browser support:
Specifies or returns a shorthand property for setting the top, right, bottom and left spaces between an element's border and its contents, in that order.
The padding property is nearly equivalent to the margin property, both insert space around the element. But while margin inserts the space around, padding inserts it within the border of the element. In other words, the padding property specifies the distance between the border and the element's content.

Syntax:

object.padding;
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: padding

Possible values:

The type of this property is string.
 One of the following values: 
 This value can be used from 1 to 4 times (use the space character to separate them) 
 One of the following values: 
<space in non-negative length>
space in non-negative percentage
inherit

Description of values:

inherit
Takes the value of this property from the computed style of the parent element.
space in non-negative length
The space in length units. For the supported length units, see the length page.
space in non-negative percentage
The space is the specified percentage of the width of the parent element.
Default: this property has no default value.

  • If only one padding value is set, it is used for all four sides.
  • If two padding values are specified, the first value is used for the top and bottom sides, the second one is for the left and right sides.
  • If three padding values are set, the first value is used for the top side, the second one is for the left and right sides and the third one is for the bottom side.
  • If four padding values are specified, the order is top, right, bottom, left (clockwise from top).

Example HTML code 1:

This example illustrates the use of the padding, border and margin properties:
<head>
    <style>
        .outer {
            border: 1px solid #ffffff;
            background-color: #ffb08a;
        }
        .middle {
            margin: 20px;
            padding: 20px;
            border: 10px solid #000000;
            background-color: #ceb379;
        }
        .inner {
            height: 10px;
            border: 1px solid #ffffff;
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <table cellpadding="0px" cellspacing="10px">
        <tr>
            <td style="background-color: #ffb08a;">&nbsp;</td>
            <td>margin</td>
            <td style="background-color: #000000;">&nbsp;</td>
            <td>border</td>
            <td style="background-color: #ceb379;">&nbsp;</td>
            <td>padding</td>
        </tr>
    </table>
    
    <div class="outer">
        <div class="middle">
            <div class="inner"></div>
        </div>
    </div>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the padding property in JavaScript:
<head>
    <style>
        .outer {
            border: 1px solid #ffffff;
            background-color: #ffb08a;
        }
        .middle {
            margin: 20px;
            padding: 20px;
            border: 10px solid #000000;
            background-color: #ceb379;
        }
        .inner {
            height: 10px;
            border: 1px solid #ffffff;
            background-color: #ffffff;
        }
    </style>
    <script type="text/javascript">
        function ChangePadding () {
            var middleDiv = document.getElementById ("middleDiv");
            var paddingInput = document.getElementById ("paddingInput");

            middleDiv.style.padding = paddingInput.value + "px";
        }
    </script>
</head>
<body>
    <table cellpadding="0px" cellspacing="10px">
        <tr>
            <td style="background-color: #ffb08a;">&nbsp;</td>
            <td>margin</td>
            <td style="background-color: #000000;">&nbsp;</td>
            <td>border</td>
            <td style="background-color: #ceb379;">&nbsp;</td>
            <td>padding</td>
        </tr>
    </table>
    <div class="outer">
        <div id="middleDiv" class="middle">
            <div class="inner"></div>
        </div>
    </div>
    <br />
    padding: 
    <input id="paddingInput" type="text" value="10" size="2" /> 
    <button onclick="ChangePadding ();">Change</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content