【问题标题】:Scrollable div within a container of variable height可变高度容器内的可滚动 div
【发布时间】:2014-01-05 14:00:48
【问题描述】:

我想在一个高度可变的容器内有一个可滚动的 div(可垂直滚动)。我希望 div 占据 100% 的可用高度,如果需要显示滚动条。容器的高度由所有其他元素的高度组成(当然不包括这个可滚动的 div)。如何仅使用 CSS 来完成这项工作?显而易见的解决方案是将可滚动 div 的高度设置为使用 javascript 计算的容器高度。

HTML:

<div class="toolbar">
    <div class="left">
        <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
        </ul>
        <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
        </ul>
    </div>
    <div class="right">
        <ul>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
            <li>List item description</li>
        </ul>        
    </div>
</div>

CSS:

.toolbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 1em;
    background-color: lightgrey;
    border-top: 1px solid grey;
    height: 10em; /* I want to remove this */
}

ul {
    display: inline-block;
}

.left {
    float: left;
}

.right {
    float: right;
    height: 100%; /* I want this to take up only the possible parent height */
    overflow-y: auto;
    white-space: nowrap; /* Is this really needed? */
    padding: 1em;
}

.toolbar:after {
    content: "";
    display: table;
    clear: both;
}

JSFiddle:

http://jsfiddle.net/fTPv5/2/

【问题讨论】:

    标签: css html height scrollable


    【解决方案1】:

    您可以删除height,将max-height 设置为所需的值(我认为它是10em,对吗?)并将overflow-y 设置为auto

    .toolbar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 1em;
        background-color: lightgrey;
        border-top: 1px solid grey;
        max-height:10em;
        overflow:auto;
    }
    

    编辑

    根据您自己的建议,如果您将绝对位置应用于正确的 div,它仍然会尊重其父级的尺寸。 Here's how the code ended

    .right {
        right:1em;
        bottom:1em;
        top:1em;
        overflow-y: auto;
        overflow-x: hidden;
        white-space: nowrap; /* Is this really needed? */
        padding: 1em;
        background-color: #fff;
        position: absolute;
    }
    

    PS:white-space: nowrap; 正在阻止 li 短语折叠,忽略 div 宽度。

    【讨论】:

    • 必须将最大高度设置为“左侧” div 中的列表组成的任何内容 - 所以我不能在那里设置任何最大高度。
    • 哦,在这种情况下,您必须使用 javascript 处理它。它必须采用左侧 div 构成的任何高度,并将其作为最大高度应用于“.toolbar”。
    • 如果我将正确的容器从正常流程中取出(IE. pos absolute) - 是否无法再调整其相对于其父级的大小?
    • 你是绝对正确的(双关语不是故意的)!我已经用解决方案编辑了我的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多