我有一个

<div class="parent">
    <div class="child" style="float:right"> Ignore parent? </div>
    <div> another child </div>
</div>

父级有

.parent {
    display: flex;
}

对于我的第一个子元素,我只想将项目向右浮动。

和我的其他div遵循父母设置的伸缩规则。

这有可能吗?

如果不是,我怎么做一个浮动:在flex的权利?


你不能在伸缩容器内使用浮动,原因是浮动属性不适用于伸缩级别的盒子,你可以在这里看到提琴。

如果你想把子元素定位到父元素的右边,你可以使用margin-left: auto但是现在子元素也会把其他div推到右边,就像你在这里看到的。

你现在可以做的是改变元素的顺序,并在子元素上设置顺序:2,这样就不会影响第二个div

.parent { 显示:flex; } .child { margin-left:汽车; 秩序:2; } < div class = "父" > <div class="child">忽略父母?< / div > < div >另一个孩子< / div > < / div >


你不需要花车。事实上,它们是无用的,因为浮动在flexbox中被忽略了。

你也不需要CSS定位。

有几种可用的flex方法。在另一个答案中提到了汽车利润率。

这里还有另外两种选择:

使用justify-content: space between和order属性。 使用justify-content: space between和颠倒div的顺序。

.parent { display: flex; justify-content: space-between; } .parent:first-of-type > div:last-child { order: -1; } p { background-color: #ddd;} <p>Method 1: Use <code>justify-content: space-between</code> and <code>order-1</code></p> <div class="parent"> <div class="child" style="float:right"> Ignore parent? </div> <div>another child </div> </div> <hr> <p>Method 2: Use <code>justify-content: space-between</code> and reverse the order of divs in the mark-up</p> <div class="parent"> <div>another child </div> <div class="child" style="float:right"> Ignore parent? </div> </div>


使用justify-content: flex-end;家长:

display: flex;
width: 100%;
flex-wrap: wrap;
justify-content: flex-end;

更多信息


display: flex;
flex-wrap: wrap;
justify-content: space-between;