【问题标题】:flexbox positioning, one item at top one at bottom [duplicate]flexbox定位,一项在顶部一项在底部[重复]
【发布时间】:2017-03-04 05:00:39
【问题描述】:

我有一个装有 2 件物品的容器。 我使用 flexbox 将它们居中,但我希望其中一个在中间,另一个在底部,但他也必须居中。

<section class="primary__section">
    <img class=" primary__section__logo " src=../img/logo_png.png alt=" ">
    <img class=" primary__section__scroll " src=../img/scroll.png alt=" ">
</section>

.primary__section {
    background: url("../img/photo_up.png") no-repeat;
    background-size: cover;
    background-position: 50%;
    min-height: 100vh;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

&__logo {
    width: 260px;
}

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    你可以使用边距:

    .primary__section {
        background: url("../img/photo_up.png") no-repeat;
        background-size: cover;
        background-position: 50%;
        min-height: 100vh;
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-direction: column;
    }
    [alt="middle"]{
      margin:auto;
    }
    [alt=" bottom"] {
      margin:0 auto 0;
    }
    /* let's test and see where the middle stands */
    body {
      margin:0;
      background:linear-gradient(to left, transparent 50%, rgba(0,0,0,0.2) 50%),
        linear-gradient(to top, transparent 50%, rgba(0,0,0,0.2) 50%)
    }
    <section class="primary__section">
        <img class=" primary__section__logo " src=../img/logo_png.png alt="middle">
        <img class=" primary__section__scroll " src=../img/scroll.png alt=" bottom">
    </section>

    http://codepen.io/gc-nomade/pen/VKqGQm一起测试和玩,看看是否有效或妥协。

    【讨论】:

    • 效果很好,谢谢! :)
    • 就像@Michael_B 在我几分钟前删除的评论中指出的那样。此解决方案并不真正居中第一个元素。可以看到here
    • 那么,有什么方法可以正确居中吗?
    • @PatrykDąbrowski 底部的绝对定位 codepen.io/gc-nomade/pen/JRwaQb 但我通常不喜欢绝对定位 ;)
    • 所以我会像你之前向我展示的那样保留它,谢谢大家的帮助:)
    猜你喜欢
    • 2017-12-25
    • 2015-09-15
    • 2023-04-07
    • 1970-01-01
    • 2023-03-20
    • 2019-07-02
    • 2016-12-14
    • 2018-01-24
    • 2018-07-30
    相关资源
    最近更新 更多