• CSS Faqs
1 June 2008

CSS Padding

With the CSS Padding can change default padding, which appears inside the various HTML elements the paragraphs, tables, and so on. But first, let us ensure that we understand definition of the padding. Padding is a space between the element’s border as well as content inside it.

Please see example below for the visual symbol. Note: Border is made noticeable, for every element; as a result you might more willingly see effects of padding.

CSS Code:
P {padding: 15px; border: 1px solid black; }
h5{padding: 0px; border: 1px solid red;}

Example 1
<html>
<head>
<style>
p { padding-left: 25px; border: 1px solid black; }
h2 { padding-top: 80px; border: 1px solid black; }
</style>
</head>
<body>
<h2>This is a Example Of CSS Padding

<p>The header has a top padding of 25px and this paragraph has a padding-left of 80px. This gives
a nice indendation to the paragraph.</p>
</body>
</html>
jpg

CSS Padding: 1 Value
As you see in the example on top, padding can be consistent inside the element. Stating one value that will make the uniform padding on every side: top, bottom, right, left. And using exact values, you might as well define padding with use of percentages.

CSS Code:
p {padding: 2%; border: 1px solid black; }
h5{padding: 0px; border: 1px solid red;}

CSS Padding: padding- (direction):
Every HTML element in fact has 4 diverse puddings: top, bottom, right, and left. It is likely to name these particular puddings just by adding the direction suffix to padding trait. Case form: padding- (direction). Defining simply one direction that will leave further 3 default paddings intact.

CSS Code:
p { padding-left: 5px; border: 1px solid black; }
h5{
padding-top: 3px;
padding-right: 12px;
padding-bottom: 15px;
padding-left: 23px;
border: 2px solid red;
}

CSS Padding: 2 and 4 Values
You can declare four padding values at once by specifying two or else four values. While using two values, first will describe padding on top as well as bottom, while second will identify padding on left and right.

While using four values padding requirement, corresponding instructions are: top, right, bottom, left. In order to help you keep in mind what order is, just memorize that it begins at top and after that moves clockwise unless it reaches left. The examples below illustrated incomplete (2) and complete (4) padding practice.

CSS Code:
p {
padding: 7px 12px;
border: 1px solid red;
}
h5{
padding: 0px 5px 10px 3px;
border: 1px solid red;
}

Browser Compatibility
According to the CSS specification, when the element has declared width, width is supposed to identify how wide content area of that particular element is. Regrettably for web development group of people, older versions of the Internet Explorer make elements with total width identical what is stated with width property. Correct way to make the element is for width property to classify the content breadth only, and for values of CSS margin, padding, as well as border properties adding up to that width.

Padding is as simple as margin to utilize, and it as well follows same format (A B C and D as seen). If you desire a paragraph to contain 5px (pixels) of padding around it, you can use:

Syntax: padding:
Possible Values: [ | ]{1,4}
Initial Value: 0
Applies to: All elements
Inherited: No

Padding property is shorthand for the padding-top, padding-bottom, padding-right, and padding-left property.

The element’s padding is an amount of space among border and content of an element. Between one as well as four values are agreed, where every value is a length or else a percentage. Percentage principles refer to parent element’s breadth and negative values are not allowable.

If four values are specified, they pertain to top, bottom, right, and left padding, in that order. If one value is set, then it applies to all the sides. If two or else three values are set, missing values are in use from opposite side.

For instance, following rule sets top padding to 2em, right padding to 4em, bottom padding to 5em, left padding to 4em:

If we set off in particulars of padding

Values

<Length>
Specifies the fixed height or else width.
<Percentage>
The percentage with value to height or else width of containing block.

* If there is just one value, it point out all four sides
* While if there are two values then first one specifies top and bottom, and second one specify left and right.
* And when there is three values the first one specify top, the second one specify left and right, and third one specify bottom.
Suppose there is four values then the first one specify top, the second one specifies right, third one specify bottom, fourth specify left.

As we have seen from box model, padding is a space outside content area, and inside a border area. In the box, there are also four sides. As a result, we can state padding up to 4 sides, and the universal padding property:
 Padding-top
 padding-right
 padding-bottom
 padding-left
The fifth property, padding, is also used as the shortcut for above four properties.
paddings can also be specified in 3 different ways: length, percentage, or else auto. Let us take a look at example:
#container {
padding-top:15px;
padding-left:5px;
padding-right:30px;
padding-bottom:40px;
border: 1px solid 000000;
}

The following HTML
<div id=”container”>
This is an example for the padding
</div>

renders following:
This is an example for the padding

Notice padding between box and top, left, bottom, as well as right of light green region is 15px, 5px, 40px, and 30px, in that order.

Padding shortcuts
All of the four paddings can be specified on the single line making use of the “padding” property. Syntax is as follows:
padding: [padding-top] [padding-right] [padding-bottom] [padding-left]

Order is extremely important here. First element is always top padding, second is always right padding, third is always bottom padding, and fourth is always left padding.

Paddings are the part of box formatting the model. Every element planned in this model is one or new rectangular boxes that are representing padding, border as well as margin areas.
Padding Properties decide how much room is to be put in between border and actual content of an element.
jpg

Padding property is the shorthand for padding-top, padding-bottom, padding-right, and padding-left properties in the CSS.

The element’s padding is amount of space among border and content of an element. Between one plus four values are set, where every value is a length or else a percentage. Percentage values pass on to parent element’s breadth and negative values are not allowable.

If four values are set, they pertain to top, right, bottom, as well as left padding, correspondingly. If one value is set, it applies to every side. If two or three values are set, then the missing values are been taken from opposite side.

For instance, following rule sets top padding to 2em, right padding to 4em, bottom padding to 5em, left padding to 4em

CSS Shortcut Notation
Above four CSS padding properties can also be joint in one property as:-

You can do this if top and bottom padding are same, as well as if left and right padding are same. If you test it in browser, it looks precisely the similar as before.

This shortcut note will reduce on typing. And further importantly, it will decrease size (slightly) of CSS file making it faster download.

Shortcut note goes still further. If top, bottom, left, as well as right padding are all same, then you can inscribe.

Tags:

This entry was posted on 1 June 2008 at 10:49 PM and is filed under CSS Faqs. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment