CSS

填充 | padding

padding

padding属性设置一个元素的内边距,padding 区域指一个元素的内容和其边界之间的空间,该属性不能为负值。

/* 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 1em; /* Global values */ padding: inherit; padding: initial; padding: unset;

初始值as each of the properties of the shorthand: padding-bottom: 0 padding-left: 0 padding-right: 0 padding-top: 0
适用元素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.
是否是继承属性no
Percentagesrefer to the width of the containing block
适用媒体visual
计算值as each of the properties of the shorthand: padding-bottom: the percentage as specified or the absolute length padding-left: the percentage as specified or the absolute length padding-right: the percentage as specified or the absolute length padding-top: the percentage as specified or the absolute length
Animation typea length
正规顺序the unique non-ambiguous order defined by the formal grammar

  • padding-bottom0

  • padding-left0

  • padding-right0

  • padding-top0

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`](::first-letter). [Inherited](inheritance) no Percentages refer to the width of the containing block Media visual [Computed value](computed_value) as each of the properties of the shorthand:

  • padding-bottom:指定的百分比或绝对长度

  • padding-left:指定的百分比或绝对长度

  • padding-right:指定的百分比或绝对长度

  • padding-top:指定的百分比或绝对长度

Animation type a [length](length#Interpolation) Canonical order the unique non-ambiguous order defined by the formal grammar

语法

该padding属性可以使用一个,两个,三个或四个值来指定。每个值是一个<length>或一个<percentage>。

  • 当指定一个值时,它将在所有四条边上应用相同的填充。

  • 当指定了两个值时,第一个填充用于顶部和底部,第二个填充用于左侧和右侧

  • 当指定三个值时,第一个填充适用于顶部,第二个填充适用于顶部,第二个填充适用于左侧和三个底部

  • 当指定四个值时,填充顺序应用于顶部右侧底部左侧(顺时针)。

<length>可指定非负的固定宽度. See<length>for details. <percentage>相对于包含块的宽度

正式语法

[ <length> | <percentage> ]{1,4}

示例

简单例子

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; }

更多的例子

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 */

规范

SpecificationStatusComment
CSS Basic Box ModelThe definition of 'padding' in that specification.Working DraftNo change.
CSS Level 2 (Revision 1)The definition of 'padding' in that specification.RecommendationNo change.
CSS Level 1The definition of 'padding' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support1.0(Yes)1.0 (1.0)4.03.51.0 (85)

FeatureAndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support?(Yes)????