Skip to content

height

height 属性定义元素的高度。

语法

css
height: auto | <length-percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>);

常见值

说明示例
auto自动height: auto
<length-percentage>长度或百分比height: 100px
min-content最小内容height: min-content
max-content最大内容height: max-content
fit-content适应内容height: fit-content
fit-content(<length-percentage>)适应内容(带限制)height: fit-content(200px)

示例

基础用法

css
/* 自动 */
.auto {
  height: auto;
}

/* 长度 */
.length {
  height: 100px;
}

/* 百分比 */
.percentage {
  height: 50%;
}

/* 最小内容 */
.min-content {
  height: min-content;
}

/* 最大内容 */
.max-content {
  height: max-content;
}

/* 适应内容 */
.fit-content {
  height: fit-content;
}

/* 适应内容(带限制) */
.fit-content-limit {
  height: fit-content(200px);
}

高级用法

css
/* 容器 */
.container {
  height: auto;
}

/* 静态 */
.static {
  height: auto;
}

/* 相对 */
.relative {
  height: auto;
}

/* 绝对 */
.absolute {
  height: auto;
}

/* 固定 */
.fixed {
  height: auto;
}

/* 粘性 */
.sticky {
  height: auto;
}

/* 响应式布局 */
.responsive {
  height: auto;
}

/* 卡片布局 */
.card {
  height: auto;
}

/* 表单布局 */
.form {
  height: auto;
}

/* 图片画廊 */
.gallery {
  height: auto;
}

/* 混合单位 */
.mixed-units {
  height: auto;
}

/* 带命名的项目 */
.named-items {
  height: auto;
}

/* 悬停效果 */
.card {
  transition: height 0.3s ease-in-out;
}

.card:hover {
  height: 200px;
}

浏览器兼容性

浏览器版本
Chrome1+
Firefox1+
Safari1+
Edge12+
iOS Safari1+
Android Browser1+
Chrome for Android18+

注意事项

  • height 可以用于任何元素
  • height 定义元素的高度
  • auto 是默认值,表示自动高度

相关属性

相关资源