Reprence Html CSS javascript PHP MySQL Bootsrap

CSS Padding

In this tutorial you will learn how to adjust the padding area of an element using CSS.

CSS Padding Properties

The CSS padding properties allow you to set the spacing between the content of an element and its border (or the edge of the element's box, if it has no defined border).

The padding is affected by the element's background-color. For instance, if you set the background color for an element it will be visible through the padding area.


Define Paddings for Individual Sides

You can specify the paddings for the individual sides of an element such as top, right, bottom, and left sides using the CSS padding-top, padding-right, padding-bottom, and the padding-left properties, respectively. Let's try out an example to understand how it works:

pad1

The padding properties can be specified using the following values:

pad2

Unlike CSS margin, values for the padding properties cannot be negative.


The Padding Shorthand Property

The padding property is a shorthand property to avoid setting padding of each side separately, i.e., padding-top, padding-right, padding-bottom and padding-left.

Let's take a look at the following example to understand how it basically works:

This shorthand notation can take one, two, three, or four whitespace separated values.

pad3

It is recommended to use the shorthand properties, it will help you to save some time by avoiding the extra typing and make your CSS code easier to follow and maintain.


Effect of Padding and Border on Layout

When creating web page layouts, adding a padding or border to the elements sometimes produce unexpected result, because padding and border is added to the width and height of the box generated by the element, as you have learnt in the CSS box model chapter.

For instance, if you set the width of a < div> element to 100% and also apply left right padding or border on it, the horizontal scrollbar will appear. Let's see an example:


To prevent padding and border from changing element's box width and height, you can use the CSS box-sizing property. In the following example the width and height of the < iv> box will remain unchanged, however, its content area will decrease with increasing padding or border.

You will learn about the CSS box sizing feature in detail in the upcoming chapters.