热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使一个弯曲的项目浮动正确-Makingaflexitemfloatright

Ihavea我有一个<div><div>Ignorep

I have a

我有一个

Ignore parent?
another child

The parent has

父母有

.parent {
    display: flex;
}

For my first child, I want to simply float the item to the right.

对于我的第一个孩子,我想简单地将项目向右浮动。

And my other divs to follow the flex rule set by the parent.

我的其他divs遵循父母设置的flex规则。

Is this something possible?

这是可能吗?

If not, how do I do a float: right under flex?

如果没有,我如何做浮动:在flex下面?

3 个解决方案

#1


47  

You can't use float inside flex container and the reason is that float property does not apply to flex-level boxes as you can see here Fiddle.

您不能在flex容器中使用float属性,原因是float属性不适用于浮动级别的盒子。

So if you want to position child element to right of parent element you can use margin-left: auto but now child element will also push other div to the right as you can see here Fiddle.

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

What you can do now is change order of elements and set order: 2 on child element so it doesn't affect second div

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

.parent {
  display: flex;
}
.child {
  margin-left: auto;
  order: 2;
}
Ignore parent?
another child

#2


10  

You don't need floats. In fact, they're useless because floats are ignored in flexbox.

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

You also don't need CSS positioning.

你也不需要CSS定位。

There are several flex methods available. auto margins have been mentioned in another answer.

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

Here are two other options:

以下是另外两个选择:

  • Use justify-content: space-between and the order property.
  • 使用rightfy -content:空格-between和order属性。
  • Use justify-content: space-between and reverse the order of the divs.
  • 使用“正当内容”:空格之间和颠倒div的顺序。

.parent {
    display: flex;
    justify-content: space-between;
}

.parent:first-of-type > div:last-child { order: -1; }

p { background-color: #ddd;}

Method 1: Use justify-content: space-between and order-1

Ignore parent?
another child

Method 2: Use justify-content: space-between and reverse the order of divs in the mark-up

another child
Ignore parent?

#3


0  

.parent {
  display: flex;
}
.child {
  margin-left: auto;
  order: 2;
}
Ignore parent?
another child

can we do without using margin-left

我们可以不使用margin-left吗?


推荐阅读
author-avatar
夜-依晨_920
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有