Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

CSS Padding

CSS Padding

CSS padding properties are utilized to produce space around an element’s content, within any specified borders.

With CSS, complete control over padding is afforded. Properties exist for setting padding individually for each side of an element (top, right, bottom, and left).

Padding – Individual Sides

CSS includes properties to specify padding for each side of an element:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

These padding properties can be assigned the following values:

  • length: Specifies padding in pixels, points, centimeters, etc.
  • Percentage (%): Specifies padding as a percentage of the width of the containing element.
  • Inherit: Specifies that the padding should inherit from the parent element.

Note: Disallowing negative values.

Example

Assign varied padding to each of the four sides of a <div> element.

div {
  padding-top: 50px;
  padding-right: 30px;
  padding-bottom: 50px;
  padding-left: 80px;
}

Padding – Shorthand Property

To streamline the code, you can consolidate all padding properties into one using shorthand.

The padding property serves as a shorthand for the following individual padding properties:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

Here’s how it operates:

When the padding property contains four values:

  • padding: 25px 50px 75px 100px; 
    • top padding is 25px
    • right padding is 50px
    • bottom padding is 75px
    • left padding is 100px

Example

Employ the padding shorthand property using four specified values.

div {
  padding: 25px 50px 75px 100px;
}

When the padding property contains three values:

  • padding: 25px 50px 75px;
    • top padding is 25px
    • right and left paddings are 50px
    • bottom padding is 75px

Example

Utilize the padding shorthand property with three specified values.

div {
  padding: 25px 50px 75px;
}
 
When the padding property contains two values:
  • padding: 25px 50px;
    • top and bottom paddings are 25px
    • right and left paddings are 50px

Example

Apply the padding shorthand property using two specified values.

div {
  padding: 25px 50px;
}

When the padding property is set with a single value.

  • padding: 25px;
    • all four paddings are 25px

Example

Utilize the padding shorthand property using a single specified value:

div {
  padding: 25px;
}

Padding and Element Width

The CSS width property defines the width of an element’s content area, which excludes padding, border, and margin (as per the box model).

Therefore, if an element’s width is specified, any padding applied to it will increase the total width of the element. This outcome is frequently considered undesirable.

Example

In this scenario, the <div> element is assigned a width of 300px. Nevertheless, the effective width of the <div> element will be 350px, comprising 300px width plus 25px of left padding and 25px of right padding.

div {
  width: 300px;
  padding: 25px;
}

To ensure that the width remains fixed at 300px regardless of the padding applied, you can utilize the box-sizing property. This property maintains the element’s intrinsic width, adjusting the available content space when padding is increased.

Example

Apply the box-sizing property to maintain a width of 300px, regardless of the padding amount.

div {
  width: 300px;
  padding: 25px;
  box-sizing: border-box;
}

All CSS Padding Properties

Property

Description

padding

A single declaration shorthand for defining all padding properties.

padding-bottom

Specifies the padding at the bottom of an element.

padding-left

Specifies the padding at the left of an element

padding-right

Specifies the padding at the right of an element.

padding-top

Specifies the padding at the top of an element