【问题标题】:How to get flexbox align-items flex-end to work in WebKit/Blink?如何让 flexbox align-items flex-end 在 WebKit/Blink 中工作?
【发布时间】:2014-05-25 01:24:19
【问题描述】:

我正在尝试将一个 flexbox 嵌套在另一个 flexbox 中。外盒是垂直的:

+------+
|      |
+------+
|      |
|      |
|      |
+------+

顶部区域适合内容flex 0 1 auto,下半部分填充剩余空间flex 1 1 auto。

在下半部分,我有另一个水平方向的 flex-box。我希望 2 个内盒在 align-items 的交叉和 flex-end 之间有空格,所以基本上 2 个内盒被固定在左右底角,如下所示:

+------+
|      |
+-+  +-|
|L|  |R|
+------+

设置这个似乎在 Firefox 中有效,但在 Chrome(Blink) 和 Safari 中我得到了这个:

+------+
|      |
|      |
|      |
+-+--+-|
|L|  |R|
+-+  +-+

这是 HTML

<div id="outer">
    <div id="top">
        top
    </div>
    <div id="bottom">
        <div id="bottomcontents">
            <div id="botleft">
                bottom left
            </div>
            <div id="botright">
                bottom right
            </div>
        </div>
    </div>
</div>

还有 CSS

* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;   
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    border: 0px;
}

#outer {
    height: 100%;
    border: 1px solid red;
    display: flex;
    display: -webkit-flex;
    flex-flow: column;
    -webkit-flex-flow: column;
    justify-content: center;
    align-content: center;
    align-items: center;
    -webkit-justify-content: center;
    -webkit-align-content: center;
    -webkit-align-items: center;
    min-height: auto;    
}
#top {
    width: 100%;
    flex: 0 1 auto;
    -webkit-flex: 0 1 auto;
    border: 1px solid blue;
}
#bottom {
    width: 100%;
    height: 100%;
    flex: 1 1 auto;
    -webkit-flex: 1 1 auto;
    border: 1px solid green;
}
#bottomcontents {
    height: 100%;
    width: 100%;
    border: 1px solid pink;
    display: flex;
    display: -webkit-flex;
    flex-flow: row;
    -webkit-flex-flow: row;
    justify-content: space-between;
    align-content: center;
    align-items: flex-end;
    -webkit-justify-content: space-between;
    -webkit-align-content: center;
    -webkit-align-items: flex-end;
    min-height: auto;        
}

#botleft, #botright {
    flex: 0 1 auto;
    -webkit-flex: 0 1 auto;
}
#botleft {
    border: 1px solid purple;
}
#botright {
    border: 1px solid cyan;
}

还有here's a fiddle

它在 Firefox 中按预期工作。我做错什么了吗?这是 Chrome 和 Safari 中的错误吗?有解决办法吗?

【问题讨论】:

    标签: css flexbox


    【解决方案1】:

    基本上,您看到的是“高度:100%;” on #bottomcontents 迫使容器在其周围容器的“外部”。 100% 的高度最终从它的父级获得高度,父级也有 100% 的高度;这意味着为#bottomcontents 指定的高度设置为与#outer 相同的高度。如果添加“溢出:隐藏”,您可以更清楚地看到它超出了应有的范围。到#底部。可以在此处找到对您的问题的更深入讨论:您遇到的问题在此处进行了介绍:Height 100% on flexbox column child

    除非我遗漏了您需要的某些要求,否则您还可以删除#bottomcontents 容器并仅使用#bottom 完成。示例代码(底部的jsfiddle示例):

    HTML:

    <div id="outer">
        <div id="top">
            top
        </div>
        <div id="bottom">
            <div id="bottomcontents">
                <div id="botleft">
                    bottom left
                </div>
                <div id="botright">
                    bottom right
                </div>
            </div>
        </div>
    </div>
    

    CSS:

    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;   
    }
    
    html, body {
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
        border: 0px;
    }
    
    #outer {
        border: 5px solid red;
        display: flex;
        display: -webkit-flex;
        height: 100%;
        width: 100%;
        flex-direction: column;
        -webkit-flex-direction: column;
    }
    
    #top {
        width: 100%;
        border: 5px solid blue;
        flex: 0 1 auto;
        -webkit-flex: 0 1 auto;
    }
    
    #bottom {
        width: 100%;
        display: flex;
        display: -webkit-flex;
        flex-direction: row;
        -webkit-flex-direction: row;
        justify-content: space-between;
        -webkit-justify-content: space-between;
        height: 100%;
        border: 5px solid green;
        align-items: flex-end;
        -webkit-align-items: flex-end;
    }
    
    #left {
        border: 5px solid purple;
        padding: .5em;
        flex: 0 1 auto;
        -webkit-flex: 0 1 auto;
    }
    
    #right {
        border: 5px solid cyan;
        padding: .5em;
        flex: 0 1 auto;
        -webkit-flex: 0 1 auto;
    }
    

    我的小提琴:http://jsfiddle.net/blake770/2br5x/

    【讨论】:

    • 人力资源部。这似乎真的错了(我不是说你错了,我是说我不懂理性)。在我的示例中,#bottom 填充了 #outer 剩余的任何内容,一旦#top 占用了它需要的任何空间。如果我在#bottom 中放入任何东西,并且我希望它填满#bottom 中的空间,那么我需要说height: 100% 来告诉它“100% 是你父母的大小”,即#bottom。您的示例以摆脱 #bottomcontents 为代价,但如果我需要 #bottomconents 怎么办?我读了你链接到的答案,我只是不明白 flex 有什么用处。
    • 您可以尝试一些想法,这里有一个例子:jsfiddle.net/blake770/2br5x/1。我在#top 和#bottom 之间添加了一个“#content” div,并将底部的高度设置为0。这可以让您在页眉/内容/页脚之间提供更多的分隔,而不是在#bottomcontents 中扔东西。将 #bottom 的高度设置为 0 仍然显示左右列,但它允许内容中的内容仍然“到达”底部。您还可以在 #left 和 #right 之间添加一个 div 并将内容放在那里,但这意味着您将在每一侧都有一个边距。
    猜你喜欢
    • 2021-05-09
    • 2022-01-17
    • 2016-02-17
    • 2021-10-26
    • 2018-03-12
    • 1970-01-01
    • 2020-11-06
    • 2018-07-16
    相关资源
    最近更新 更多