gpt4 book ai didi

css - Flexbox 对齐到 div 的末尾

转载 作者:行者123 更新时间:2023-11-28 09:24:57 25 4
gpt4 key购买 nike

如何使用 flexbox 将 .right 对齐到 div 的末尾?

我的 CSS:

div {
display: flex;
border: 1px dotted black;
}
.right {

}

HTML:

<div>
<span>Left</span>
<span class="right">Right</span>
</div>

Codepen Link

谢谢。

最佳答案

三种方式:

  1. 在容器上使用justify-content: space-between

    div {
    display: flex;
    border: 1px dotted black;
    justify-content: space-between; /* new */
    }

    Revised Codepen


  1. 在第一个 flex 元素上使用 auto 边距。

    div > span:first-child { margin-right: auto; }

    Revised Codepen


  1. 在第二个 flex 元素上使用 auto 边距。

    .right { margin-left: auto; }

    Revised Codepen


有关justify-contentauto margins,连同示例和插图,请参阅这篇文章:Methods for Aligning Flex Items along the Main Axis

关于css - Flexbox 对齐到 div 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com