padding - CSS: Cascading Style Sheets 编辑
The padding
CSS shorthand property sets the padding area on all four sides of an element at once.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.An element's padding area is the space between its content and its border.
Note: Padding creates extra space within an element. In contrast, margin
creates extra space around an element.
Constituent properties
This property is a shorthand for the following CSS properties:
Syntax
/* Apply to all four sides */
padding: 1em;
/* vertical | horizontal */
padding: 5% 10%;
/* top | horizontal | bottom */
padding: 1em 2em 2em;
/* top | right | bottom | left */
padding: 5px 1em 0 2em;
/* Global values */
padding: inherit;
padding: initial;
padding: unset;
The padding
property may be specified using one, two, three, or four values. Each value is a <length>
or a <percentage>
. Negative values are invalid.
- When one value is specified, it applies the same padding to all four sides.
- When two values are specified, the first padding applies to the top and bottom, the second to the left and right.
- When three values are specified, the first padding applies to the top, the second to the right and left, the third to the bottom.
- When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise).
Values
<length>
- The size of the padding as a fixed value.
<percentage>
- The size of the padding as a percentage, relative to the width of the containing block.
Formal definition
Initial value | as each of the properties of the shorthand: |
---|---|
Applies to | all elements, except table-row-group , table-header-group , table-footer-group , table-row , table-column-group and table-column . It also applies to ::first-letter and ::first-line . |
Inherited | no |
Percentages | refer to the width of the containing block |
Computed value | as each of the properties of the shorthand:
|
Animation type | a length |
Formal syntax
[ <length> | <percentage> ]{1,4}
Examples
Setting padding with pixels
HTML
<h4>This element has moderate padding.</h4>
<h3>The padding is huge in this element!</h3>
CSS
h4 {
background-color: lime;
padding: 20px 50px;
}
h3 {
background-color: cyan;
padding: 110px 50px 50px 110px;
}
Result
Setting padding with pixels and percentages
padding: 5%; /* All sides: 5% padding */
padding: 10px; /* All sides: 10px padding */
padding: 10px 20px; /* top and bottom: 10px padding */
/* left and right: 20px padding */
padding: 10px 3% 20px; /* top: 10px padding */
/* left and right: 3% padding */
/* bottom: 20px padding */
padding: 1em 3px 30px 5px; /* top: 1em padding */
/* right: 3px padding */
/* bottom: 30px padding */
/* left: 5px padding */
Specifications
Specification | Status | Comment |
---|---|---|
CSS Basic Box Model The definition of 'padding' in that specification. | Working Draft | No change. |
CSS Level 2 (Revision 1) The definition of 'padding' in that specification. | Recommendation | No change. |
CSS Level 1 The definition of 'padding' in that specification. | Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
- Introduction to the CSS basic box model
padding-top
,padding-right
,padding-bottom
, andpadding-left
.- The mapped logical properties:
padding-block-start
,padding-block-end
,padding-inline-start
, andpadding-inline-end
and the shorthandspadding-block
andpadding-inline
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论