The CSS padding properties define the space between the element border and the element content.


Padding

The padding clears an area around the content (inside the border) of an element. The padding is affected by the background color of the element.
The top, right, bottom, and left padding can be changed independently using separate properties. A shorthand padding property can also be used, to change all paddings at once.

Possible Values

ValueDescription
lengthDefines a fixed padding (in pixels, pt, em, etc.)
%Defines a padding in % of the containing element


Padding - Individual sides

In CSS, it is possible to specify different padding for different sides:

Example

<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.padding
{
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
}
</style>
</head>

<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
This is a paragraph with no specified padding.
This is a paragraph with specified paddings.

Padding - Shorthand property

To shorten the code, it is possible to specify all the padding properties in one property. This is called a shorthand property.
The shorthand property for all the padding properties is "padding":

Example

<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.padding
{
padding:25px 50px;
}
</style>
</head>

<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
The padding property can have from one to four values.
  • padding:25px 50px 75px 100px;
    • top padding is 25px
    • right padding is 50px
    • bottom padding is 75px
    • left padding is 100px
  • padding:25px 50px 75px;
    • top padding is 25px
    • right and left paddings are 50px
    • bottom padding is 75px
  • padding:25px 50px;
    • top and bottom paddings are 25px
    • right and left paddings are 50px
  • padding:25px;
    • all four paddings are 25px

More Examples

All the padding properties in one declarationThis example demonstrates a shorthand property for setting all of the padding properties in one declaration, can have from one to four values.
Example
<html>
<head>
<style type="text/css">
p.ex1 {padding:2cm;}
p.ex2 {padding:0.5cm 3cm;}
</style>
</head>


<body>
<p class="ex1">This text has equal padding on each side. The padding on each side is 2cm.</p>
<p class="ex2">This text has a top and bottom padding of 0.5cm and a left and right padding of 3cm.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Set the left padding
This example demonstrates how to set the left padding of a p element.
Example
<html>
<head>
<style type="text/css">
p.padding {padding-left:2cm;}
p.padding2 {padding-left:50%;}
</style>
</head>


<body>
<p>This is a text with no left padding.</p>
<p class="padding">This text has a left padding of 2 cm.</p>
<p class="padding2">This text has a left padding of 50%.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Set the right padding
This example demonstrates how to set the right padding of a p element.
Example
<html>
<head>
<style type="text/css">
p.padding {padding-right:2cm;}
p.padding2 {padding-right:50%;}
</style>
</head>


<body>
<p>This is a text with no right padding. This is a text with no right padding. This is a text with no right padding.</p>
<p class="padding">This text has a right padding of 2 cm. This text has a right padding of 2 cm. This text has a right padding of 2 cm.</p>
<p class="padding2">This text has a right padding of 50%. This text has a right padding of 50%. This text has a right padding of 50%.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Set the top padding
This example demonstrates how to set the top padding of a p element.
Example
<html>
<head>
<style type="text/css">
p.padding {padding-top:2cm;}
p.padding2 {padding-top:50%;}
</style>
</head>


<body>
<p>This is a text with no top padding. This is a text with no top padding. This is a text with no top padding.</p>
<p class="padding">This text has a top padding of 2 cm. This text has a top padding of 2 cm. This text has a top padding of 2 cm.</p>
<p class="padding2">This text has a top padding of 50%. This text has a top padding of 50%. This text has a top padding of 50%.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.


Set the bottom padding
This example demonstrates how to set the bottom padding of a p element.
Example
<html>
<head>
<style type="text/css">
p.padding {padding-bottom:2cm;}
p.padding2 {padding-bottom:50%;}
</style>
</head>


<body>
<p>This is a text with no bottom padding. This is a text with no bottom padding. This is a text with no bottom padding.</p>
<p class="padding">This text has a bottom padding of 2 cm. This text has a bottom padding of 2 cm. This text has a bottom padding of 2 cm.</p>
<p class="padding2">This text has a bottom padding of 50%. This text has a bottom padding of 50%. This text has a bottom padding of 50%.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.


All CSS Padding Properties

PropertyDescription
paddingA shorthand property for setting all the padding properties in one declaration
padding-bottomSets the bottom padding of an element
padding-leftSets the left padding of an element
padding-rightSets the right padding of an element
padding-topSets the top padding of an element

Leave a Reply