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.
An element's padding area is the space between its content and its border.
注意:
Padding creates extra space within an element. In contrast,
margin
creates extra space
around
an element.
此特性是下列 CSS 特性的简写:
/* 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;
padding
property may be specified using one, two, three, or four values. Each value is a
<length>
或
<percentage>
. Negative values are invalid.
<length>
The size of the padding as a fixed value.
<percentage>
| 初始值 |
as each of the properties of the shorthand:
|
|---|---|
| 适用于 |
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
.
|
| 继承 | no |
| 百分比 | refer to the width of the containing block |
| 计算值 |
as each of the properties of the shorthand:
|
| 动画类型 | a length |
[ <length> | <percentage> ]{1,4}
<h4>This element has moderate padding.</h4> <h3>The padding is huge in this element!</h3>
h4 {
background-color: lime;
padding: 20px 50px;
}
h3 {
background-color: cyan;
padding: 110px 50px 50px 110px;
}
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 */
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
padding
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
padding-top
,
padding-right
,
padding-bottom
,和
padding-left
.
padding-block-start
,
padding-block-end
,
padding-inline-start
,和
padding-inline-end
and the shorthands
padding-block
and
padding-inline
height
margin-bottom
margin-left
margin-right
margin-top
margin-trim
max-height
max-width
min-height
min-width
overscroll-behavior
overscroll-behavior-block
overscroll-behavior-inline
overscroll-behavior-x
overscroll-behavior-y
padding
padding-bottom
padding-left
padding-right
padding-top
visibility
width