CSS Padding




Inherited: No

Padding is the distance between the border of an (X)HTML element and the content within it.

Most of the rules for margins also apply to padding, except there is no “auto” value, and negative values cannot be declared for padding.

[css autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”true” title=”<code>CSS CODE</code>”] length percentage; padding-left: <span class="paths">length percentage</span>; padding-right: <span class="paths">length percentage</span>; padding-bottom: <span class="paths">length percentage</span>; [/css]

 

As you can also see in the above example you have 2 choices of values for the padding property

  • length
  • percentage

You can also declare all the padding of an element in a single property as follows:

[css autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”true” title=”CSS CODE”]
  padding: 10px 10px 10px 10px; [/css]

 

If you declare all 4 values as I have above, the order is as follows:

  1. top
  2. right
  3. bottom
  4. left

If only one value is declared, it sets the padding on all sides. (see below)

  padding: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side. (see below)

[css autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”true” title=”CSS CODE”]
padding: 10px 10px; <span class="paths">/* 2 values */</span>
padding: 10px 10px 10px; <span class="paths">/* 3 values */</span>

[/css]

 

If you do not declare the padding value of an element, the padding is 0 (zero).

Note: You do not have to add px (pixels) or whatever units you use, if the value is 0 (zero).

You can see in the example below, the main container for this site has 30px (pixels) of padding between the border and the text.

[css autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”true” title=”CSS CODE”]
#container{
width: 70%;
margin: auto;
<span class="paths">padding: 30px;</span>
border: 1px solid #666;
background: #ffffff;
}
[/css]







Next CSS Text Properties