高度 | 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 |
Percentages | The 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 type | a 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%;
}
规范
Specification | Status | Comment |
---|---|---|
CSS Basic Box ModelThe definition of 'height' in that specification. | Working Draft | Added the max-content, min-content, available, fit-content, border-box, content-box keywords. |
CSS TransitionsThe definition of 'height' in that specification. | Working Draft | Lists height as animatable. |
CSS Level 2 (Revision 1)The definition of 'height' in that specification. | Recommendation | Adds support for the <length> values and precises on which element it applies to. |
CSS Level 1The definition of 'height' in that specification. | Recommendation | Initial definition |
CSS Intrinsic & Extrinsic Sizing Module Level 3The definition of 'width' in that specification. | Working Draft | Adds new sizing keywords for width and height. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | 4.0 | 7.0 | 1.0 |
fill, fit-content, min-content, max-content | 46.0 | ? | | | | |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | (Yes) | 1.0 (1) | 6.0 | 6.0 | 1.0 | (Yes) |
fill, fit-content, min-content, max-content | ? | 46.0 | ? | | | | | 46.0 |