CSS Padding

CSS Padding (CSS内边距)

The CSS padding property is used for creating space around the content of an element. CSS provides different properties, with the help of which you can set the padding for an element’s each side (right, left, top and bottom). (CSS padding属性用于在元素的内容周围创建空间。CSS提供了不同的属性,借助这些属性,您可以为元素的每一侧(右侧、左侧、顶部和底部)设置填充。)

The individual sides

The individual sides (单边)

With the help of the following properties you can set the padding for each side of an element:

  • padding-top (顶部填充)

  • padding-bottom (填充-底部)

  • padding-left (填充-左)

  • padding-right (填充-右)

As you can guess, for the top, we use padding-top, for bottom padding-bottom, for left side padding-left and right padding-right. (您可以猜到,对于顶部,我们使用padding-top ,对于底部padding-bottom ,对于左侧padding-left和右侧padding-right。)

All the padding properties can have the values below:

  • length, which is used for specifying a padding in px, pt, cm, etc, (- length ,用于指定px、pt、cm等单位的填充,)

  • %, which specifies a padding in % of the width of the containing element, (- % ,以包含元素宽度的%指定填充,)

  • inherit , which specifies that the padding must be inherited from its parent element. (- inherit ,指定必须从其父元素继承填充。)

Take into account, that CSS padding property cannot accept negative values. Its default value for all padding properties is 0. (请注意, CSS padding属性不能接受负值。其所有填充属性的默认值为0。)

Example of the individual padding properties:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
   <style>
     div {
       background-color: yellow;
       padding-top: 50px;
       padding-right: 30px;
       padding-bottom: 50px;
       padding-left: 80px;
     }
   </style>
 </head>
 <body>
   <h2>Individual padding properties</h2>
   <div>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua..
(Latin&#10;)
   </div>
 </body>
</html>

Padding as a shorthand property

Padding as a shorthand property (作为速记属性的填充)

The CSS padding property is a shorthand property for the individual padding properties below:

  • padding-top (顶部填充)

  • padding-bottom (填充-底部)

  • padding-left (填充-左)

  • padding-right (填充-右)

In the cases, when the padding property has only 1 value, for example padding: 35px, all the four paddings are 35px.

Example of the padding shorthand property with one value:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
   <style>
     div {
       background-color: green;
       padding: 35px;
     }
   </style>
 </head>
 <body>
   <h2>Individual padding properties</h2>
   <div>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
(Latin&#10;)
   </div>
 </body>
</html>

The padding property may have 2 values, for example padding: 20px 40px, where top and bottom paddings are 20px, right and left paddings are 40px.

p {
 padding: 20px 40px;
}

This is the same with the code above. (这与上面的代码相同。)

p {
 padding-top: 20px;
 padding-right: 40px;
 padding-bottom: 20px;
 padding-left: 40px;
}

Example of the padding shorthand property with two values:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
   <style>
     p {
       background-color: #1c87c9;
       color: white;
       padding: 20px 40px;
     }
   </style>
 </head>
 <body>
   <p>Paragraph with background-color, color and padding properties.</p>
 </body>
</html>

Result

The padding property may have 3 values, for example, padding: 20px 15px 35px;, where top padding is 20px, right and left paddings are 15px and bottom padding is 35px.

p {
 padding: 20px 15px 35px;
}

This is the same with the code above. (这与上面的代码相同。)

p {
 padding-top: 20px;
 padding-right: 15px;
 padding-bottom: 35px;
 padding-left: 15px;
}

Example of the padding shorthand property with three values:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
   <style>
     div {
       background-color: lightblue;
       padding: 20px 15px 35px;
     }
   </style>
 </head>
 <body>
   <h2>Example of the padding shorthand property</h2>
   <div>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
(Latin&#10;)
   </div>
 </body>
</html>

And finally, the padding property can have four values, for example padding: 25px 50px 75px 100px;, where top padding is 25px, right padding is 50px, bottom padding is 75px and left padding is 100px.

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

Example of the padding shorthand property with four values:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
   <style>
     div {
       background-color: #95e5f7;
       padding: 25px 50px 75px 100px;
     }
   </style>
 </head>
 <body>
   <h2>Example of the padding shorthand property</h2>
   <div>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
(Latin&#10;)
   </div>
 </body>
</html>


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部