--- title: height slug: Web/CSS/height tags: - CSS - CSS Property - Reference translation_of: Web/CSS/height ---
height
CSS 属性指定了一个元素的高度。默认情况下,这个属性决定的是内容区( content area)的高度,但是,如果将 {{cssxref("box-sizing")}} 设置为 border-box
, 这个属性决定的将是边框区域(border area)的高度。/* Keyword value */ height: auto; /* <length> values */ height: 120px; height: 10em; /* <percentage> value */ height: 75%; /* Global values */ height: inherit; height: initial; height: unset;
border-box
{{experimental_inline}}content-box
{{experimental_inline}}auto
fill
{{experimental_inline}}fill-available
作为行大小或者块大小。max-content
{{experimental_inline}}min-content
{{experimental_inline}}available
{{experimental_inline}}fit-content
{{experimental_inline}}{{csssyntax}}
<div id="taller">I'm 50 pixels tall.</div> <div id="shorter">I'm 25 pixels tall.</div> <div id="parent"> <div id="child"> I'm half the height of my parent. </div> </div>
div { width: 250px; margin-bottom: 5px; border: 2px solid blue; } #taller { height: 50px; } #shorter { height: 25px; } #parent { height: 100px; } #child { height: 50%; width: 75%; }
{{EmbedLiveSample('示例', 'auto', 240)}}
确保设定了 height
的元素在显示上不会被截断,或者当页面放大时增大的字体大小不会遮挡其他内容。
Specification | Status | Comment |
---|---|---|
{{SpecName('CSS3 Box', '#the-width-and-height-properties', 'height')}} | {{Spec2('CSS3 Box')}} | Added the max-content , min-content , available , fit-content , border-box , content-box keywords. |
{{SpecName('CSS3 Transitions', '#animatable-css', 'height')}} | {{Spec2('CSS3 Transitions')}} | Lists height as animatable. |
{{SpecName('CSS2.1', 'visudet.html#the-height-property', 'height')}} | {{Spec2('CSS2.1')}} | Adds support for the {{cssxref("<length>")}} values and precises on which element it applies to. |
{{SpecName('CSS1', '#height', 'height')}} | {{Spec2('CSS1')}} | Initial definition. |
{{SpecName('CSS3 Sizing', '#width-height-keywords', 'width')}} | {{Spec2('CSS3 Sizing')}} | Adds new sizing keywords for width and height. |
{{cssinfo}}
{{Compat("css.properties.height")}}