CSS

高度 | height

height

height 指定元素内容区高度。内容区 content area 在元素padding, border, and margin 的里面。

/* Keyword value */ height: auto; /* <length> values */ height: 120px; height: 10em; /* <percentage> value */ height: 75%; /* Global values */ height: inherit; height: initial; height: unset;

min-height 与  max-height覆盖height.

初始值auto
适用元素all elements but non-replaced inline elements, table columns, and column groups
是否是继承属性no
PercentagesThe percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto. A percentage height on the root element is relative to the initial containing block.
适用媒体visual
计算值a percentage or auto or the absolute length
Animation typea length, percentage or calc(
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

<length>将高度定义为绝对值。<percentage>将高度定义为包含块高度的百分比。border-box 如果存在,前面的<length>或者<percentage>应用于元素的边框。content-box如果存在,前面的<length>或<percentage>应用于元素的内容框。auto浏览器将计算并为指定的元素选择一个高度。填充使用fill-available内嵌大小或fill-available块大小,以适应写入模式。max-content内在的首选高度。min-content内在的最小高度。available包含块高度减去垂直边距,边框和填充。fit-content较大的:

  • 内在的最小高度

  • 内部偏好高度和可用高度越小

正式语法

[ <length> | <percentage> ] && [ border-box | content-box ]? | available | min-content | max-content | fit-content | auto

示例

HTML

<div id="red"> <span>I'm 50 pixels tall.</span> </div> <div id="green"> <span>I'm 25 pixels tall.</span> </div> <div id="parent"> <div id="child"> <span>I'm half the height of my parent.</span> </div> </div>

CSS

div { width: 250px; margin-bottom: 5px; border: 3px solid #999999; } #red { height: 50px; } #green { height: 25px; } #parent { height: 100px; } #child { height: 50%; width: 75%; }

规范

SpecificationStatusComment
CSS Basic Box ModelThe definition of 'height' in that specification.Working DraftAdded the max-content, min-content, available, fit-content, border-box, content-box keywords.
CSS TransitionsThe definition of 'height' in that specification.Working DraftLists height as animatable.
CSS Level 2 (Revision 1)The definition of 'height' in that specification.RecommendationAdds support for the <length> values and precises on which element it applies to.
CSS Level 1The definition of 'height' in that specification.RecommendationInitial definition
CSS Intrinsic & Extrinsic Sizing Module Level 3The definition of 'width' in that specification.Working DraftAdds new sizing keywords for width and height.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0(Yes)1.0 (1.7 or earlier)4.07.01.0
fill, fit-content, min-content, max-content46.0?

FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
Basic support1.0(Yes)(Yes)1.0 (1)6.06.01.0(Yes)
fill, fit-content, min-content, max-content?46.0?46.0