小编BoJ*_*ack的帖子

使用css flexbox时如何防止按钮占据全宽?

当我使用 Flexbox 将内容居中时,我总是遇到这个问题,其中我的按钮最终跨越了 div 的整个宽度。我希望它只占用它需要的空间。

代码:

HTML:

                    <div class="who__text-content">
                        <h2 class="u-margin-bottom-2">Who</h2>
                        <h2 class="u-margin-left-2 u-margin-bottom-2">Lorem ipsum</h2>
                        <p class="u-margin-left-2 u-margin-bottom-6">Lorem ipsum</p>
                        <a href="#" class="btn u-margin-left-2">Meet The Team</a>
                    </div>
Run Code Online (Sandbox Code Playgroud)

萨斯:

.btn {
    padding: 1.3rem 3.7rem;
    width: auto;
}

.who {
    &__text-content {
        padding-bottom: 1.3rem;
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        align-content: center;
        margin: auto;
        height: 100%;
        <!-- I tried using the code bellow instead of the code above with position: relative; on the parent element. It ensures the button doesn't span the fullwidth …
Run Code Online (Sandbox Code Playgroud)

css flexbox

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

css ×1

flexbox ×1