如何仅使一个弹性项目拉伸而其他项目限制以保持其自然高度?

asd*_*asd 4 html css flexbox

我希望除 last 之外的所有 flex 项目只占据它们的正常高度,最后一个 flex 项目占据 flexbox 的剩余高度(拉伸)。但我无法做到这一点。

以下是我一直在尝试的:

  .parent {
        display: flex;
        flex-wrap: wrap;
        /*align-content: start;*/
        border: 1px solid green;
        height: 50vh
    }

    .child-height-auto {
        align-self: start;
        border: 1px solid red;
        width: 100%;
    }

    .child-height-strech {
        align-self: stretch;
        border: 1px solid blue;
        width: 100%;
    }
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
    <div class="child-height-auto">I should occupy my normal height</div>
    <div class="child-height-auto">I should occupy my normal height</div>
    <div class="child-height-strech">I should occupy remaining height of the flexbox</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Art*_*äpp 7

将 flex 的方向设置为column并将 flex-grow 属性添加到最后一个元素。

  .parent {
        display: flex;
        flex-wrap: wrap;
        /*align-content: start;*/
        border: 1px solid green;
        height: 50vh;
        flex-direction: column;
    }

    .child-height-auto {
        align-self: start;
        border: 1px solid red;
        width: 100%;
    }

    .child-height-strech {
        align-self: stretch;
        border: 1px solid blue;
        width: 100%;
        flex-grow: 1;
    }
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
    <div class="child-height-auto">I should occupy my normal height</div>
    <div class="child-height-auto">I should occupy my normal height</div>
    <div class="child-height-strech">I should occupy remaining height of the flexbox</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 不,根据规范,您需要将一个 flex 容器插入另一个 flex 容器,每个容器都有不同的方向。上面的答案是正确的。 (3认同)