怎么样才能让子元素继承父元素高度?

<div class="father">
    <div class="son"></div>
</div>

father的高度是屏幕高度

son的高度是内容高度
现在想让son继承father的高度

怎么做?

阅读 12k
2 个回答

height: 100%
height: 100vh

或者父元素relative, 子元素absolute像狗皮膏药一样贴上去。

.father { position: relative }

.son {
  postion: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
<style type="text/css">
.father{
    height: 500px;
    width: 500px;
    background: #ccc
}
.son{
    width: 500px;
    background: #f00;
    height: inherit;

}
</style>
<body>

<div class="father">
    <div class="son"></div>
</div>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题