css高度自适应问题 百分比高度

clipboard.png
父级div为1,里面有2个子div

3的高度根据内容自适应
1的高度根据3的高度自动撑开并设置最小高度45
2的高度跟1一样高

阅读 4.2k
3 个回答
<div class="one">
    <div class="two"></div>
    <div class="three"></div>
</div>
.one {
    position: relative;
    min-height: 200px;
}
.two {
    background-color: green;
    position: absolute;
    top: 0px;
    left: 0px;
    right: 50%;
    bottom: 0px;
}
.three {
    height: 300px;
    width: 50%;
    background-color: red;
    margin-left: 50%;
    min-height: 200px;
}

你可以调节.three height的大小查看效果。

思路就是,2设置为绝对定位、height:100%,1给个padding-left的宽度和2的宽度一样

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        *{
            
            box-sizing: border-box;
        }
        .cc-1{
            border: 4px solid #ddd;
            position: relative;
            padding-left: 50%;
        }

        .cc-2{
            position: absolute;
            width: 50%;
            left: 0;
            top: 0;
            height: 100%;
            border: 2px solid #000;
        }

        .cc-3{
            border: 2px solid #ff4200;
            height: 300px;
        }
    </style>
</head>
<body>
    <div class="cc-1">
        <div class="cc-2"></div>
        <div class="cc-3"></div>
    </div>
</body>
</html>

我感觉办不到,内部元素要能和外部元素一样高,需要内外能使用height:100%

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