Padding in CSS

The CSS padding feature is used to create space around the content of the element and within the borders in CSS. Features set up all 4 sides (top, bottom, left, right) for each element.

Padding of each edge

CSS has properties that define padding for each element edge:

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

All properties can have the following values:

  1. length - determines the margin according to px, pt, cm .
  2. % - determines the margin in% of the width of the element
  3. inherit - determine the margin inherited from the parent element

Negative values ​​can be used. The example below sets the padding for 4 sides of the element

:

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

Shortened feature of padding

To shorten the code, you can use the padding, abridged property padding, putting it all in a single property.

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

If the padding property has 4 values

 padding: 25px 50px 75px 100px; 
  1. 25px upper margin
  2. right margin 50px
  3. margin below 75px
  4. left margin 100px
 div { 
padding: 25px 50px 75px 100px;
}

If the padding property has 3 values

 padding: 25px 50px 75px; 
  1. 25px upper margin
  2. left and right margins 50px
  3. margin below 75px
 div { 
padding: 25px 50px 75px;
}

If the padding property has 2 values

 padding: 25px 50px; 
  1. upper margin and below 25px
  2. left and right margins 50px
 div { 
padding: 25px 50px;
}

If the padding property has 1 value

 padding: 25px; 
  1. All margins are 25px
 div { 
padding: 25px;
}

Width of padding and element

The width property of CSS determines the width of the content area within the element, which is the part of the element's padding, border, and margin.

Padding in CSS Picture 1
Illustration of element padding, border, and margin properties

If the element has a certain width, the added padding will add to the total width of the element, which will often cause unexpected results.

In the example below element

300px wide but the width of the element is realistic

This will be 350px (300px + 25px of left padding part + 25px right padding part).

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

To keep the total width of 300px, no matter how wide the padding part is, use the box-sizing to keep the width. If you increase the padding, part, the content inside will decrease to the total width unchanged.

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

Last lesson: CSS margin

Lesson: Height and width in CSS

4 ★ | 2 Vote

May be interested

  • AZ word about eggs in Pokemon GoAZ word about eggs in Pokemon Go
    there are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
  • Things you didn't know about 26 types of Pokéball - Part 1Things you didn't know about 26 types of Pokéball - Part 1
    pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.