CSS PADDING
- The CSS padding properties are used to generate space around an element's content, inside of any defined borders.
- With CSS, you have full control over the padding. There are properties for setting the padding for each side of an
element (top, right, bottom, and left), and a shorthand property for setting all the padding properties in one
declaration.
PADDING – INDIVIDUAL SIDES
CSS has properties for specifying the padding for each side of an element:
• padding-top - sets the top padding of an element
• padding-right - sets the right padding of an element
• padding-bottom - sets the bottom padding of an element
• padding-left - sets the left padding of an element
All the padding properties can have the following values:
• length - specifies a padding in px, pt, cm, etc.
• % - specifies a padding in % of the width of the containing element
• inherit - specifies that the padding should be inherited from the parent element
Note: Negative values are not allowed.
PADDING – SHORTHAND PROPERTY
To shorten the code, it is possible to specify all the padding properties in one declaration.
The padding property is a shorthand property for the following individual padding properties:
• padding-top
• padding-right
• padding-bottom
• padding-left
Here is how it works:
If the padding property has four values:
• padding: 25px 50px 75px 100px;
o top padding is 25px
o right padding is 50px
o bottom padding is 75px
o left padding is 100px
If the padding property has three values:
• padding: 25px 50px 75px;
o top padding is 25px
o right and left paddings are 50px
o bottom padding is 75px
If the padding property has two values:
• padding: 25px 50px;
o top and bottom paddings are 25px
o right and left paddings are 50px
If the padding property has one value:
• padding: 25px;
o all four paddings are 25px
PADDING AND ELEMENT WIDTH
The CSS width property specifies the width of the element's content area. The content area is the portion
inside the padding, border, and margin of an element (the box model).
So, if an element has a specified width, the padding added to that element will be added to the total width
of the element. This is often an undesirable result.
PADDING AND BOX-SIZING
The box-sizing property defines how the width and height of an element are calculated: should they
include padding and borders, or not.
The box-sizing property can have the following values:
• content-box - This is default. The width and height properties includes only the content (border
and padding are not included)
• border-box - The width and height properties includes content, padding and border
So, to keep the width at 300px, no matter the amount of padding, you can use the box-sizing: border-
box;. This causes the element to maintain its actual width; if you increase the padding, the available
content space will decrease.