【问题标题】:Align an element at the bottom of a container inside a flexbox在 flexbox 内的容器底部对齐元素
【发布时间】:2023-03-19 07:44:01
【问题描述】:

Fiddle here.

我正在尝试将白色插入符号 (^) 粘贴到 .section-1 的底部,水平居中但位于最底部。如您所见,插入符号本身位于 Bootstrap container-fluid 内,而 Bootstrap container-fluid 本身位于 flex-box 内(通过 vertical-center 类应用)。

我尝试过使用边距和bottom: 0; 等进行传统定位,但无济于事。同样,flex-end 属性也没有像我想象的那样工作。欢迎所有解决方案和建议,只要他们继续使用 flexbox。

【问题讨论】:

    标签: html twitter-bootstrap css flexbox


    【解决方案1】:

    您可以使用嵌套的弹性容器来实现这一点。

    HTML

    <!-- one adjustment; add span -->
    <div class="align-me-center"><span>Here is some centered text.</span></div>
    

    CSS

    /* ADJUSTMENT TO RULE */
    
    .section-1 {
        min-height: 500px;
        background-color: #0099cc;
        height: 500px; /* necessary for child div with percentage height to work;
                          min-height overrides height, so no worries */
    }
    
    /* NEW RULES */
    
    .container-fluid { 
        display: flex; /* establish nested flex container */
        flex-direction: column;
        height: 100%; /* use full height of container */
        justify-content: center; /* position flex items at opposite end of container */
    }
    
    .align-me-center { 
        display: flex; /* nested flex container to center flex item (span) */
        align-items: center;
        justify-content: center; 
        height: 100%;
    }
    

    演示:http://jsfiddle.net/b26j5m6t/3/

    【讨论】:

    • 这很接近,但我仍然希望插入符号上方的文本垂直居中。
    • 这正是我想要做的。完美!
    猜你喜欢
    • 2016-04-20
    • 2018-09-24
    • 1970-01-01
    • 2015-09-09
    • 2020-01-28
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多