本文共 4176 字,大约阅读时间需要 13 分钟。
本文翻译自:
Is there a more flexbox-ish way to right-align "Contact" than to use position: absolute
? 与使用position: absolute
相比,有没有更多的flexbox-ish方式来使“ Contact”右对齐?
.main { display: flex; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { flex: 1; text-align: center; } .c { position: absolute; right: 0; }
With title
Without title
参考:
A more flex approach would be to use an auto
left margin (flex items treat a bit differently than when used in a block formatting context). 更加灵活的方法是使用auto
左页边距(与在块格式化上下文中使用时相比,灵活项对的处理有些不同)。
.c { margin-left: auto;}
Updated fiddle: 更新的小提琴:
.main { display: flex; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { flex: 1; text-align: center; } .c {margin-left: auto;}
With title
Without title
Problem
Is there a more flexbox-ish way to right align "Contact" than to use position absolute?
If you want to use flexbox for this, you should be able to, by doing this ( display: flex
on the container, flex: 1
on the items, and text-align: right
on .c
): 如果要为此使用flexbox,则应该能够这样做(在容器上display: flex
,在项目上display: flex
flex: 1
,在.c
上显示text-align: right
):
.main { display: flex; }.a, .b, .c { background: #efefef; border: 1px solid #999; flex: 1;}.b { text-align: center; }.c { text-align: right; }
...or alternatively (even simpler), if the items don't need to meet, you can use justify-content: space-between
on the container and remove the text-align
rules completely: ...或更可替代地(甚至更简单),如果不需要满足这些条件,则可以在容器上使用justify-content: space-between
,并完全删除text-align
规则:
.main { display: flex; justify-content: space-between; }.a, .b, .c { background: #efefef; border: 1px solid #999; }
Here's a on Codepen to allow you to quickly try the above. 这是Codepen上的 ,可让您快速尝试上述操作。
Here you go. 干得好。 Set justify-content: space-between
on the flex container. 在flex容器上设置justify-content: space-between
。
.main { display: flex; justify-content: space-between; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { text-align: center; }
With title
Without title
You can also use a filler to fill the remaining space. 您也可以使用填充物填充剩余空间。
.filler{ flex-grow: 1;}
I have updated the solution with 3 different versions. 我已经用3个不同的版本更新了解决方案。 This because of the discussion of the validity of using an additional filler element. 这是因为讨论了使用附加填充元素的有效性。 If you run the code snipped you see that all solutions do different things. 如果您运行了代码片段,则会看到所有解决方案都做不同的事情。 For instance setting the filler class on item b will make this item fill the remaining space. 例如,在项目b上设置填充器类别将使该项目填充剩余空间。 This has the benefit that there is no 'dead' space that is not clickable. 这样做的好处是,没有不可单击的“死”空间。
If you need one item to be left aligned (like a header) but then multiple items right aligned (like 3 images), then you would do something like this: 如果您需要将一项左对齐(例如标题),然后将多项右对齐(例如3张图像),则可以执行以下操作:
h1 { flex-basis: 100%; // forces this element to take up any remaining space}img { margin: 0 5px; // small margin between images height: 50px; // image width will be in relation to height, in case images are large - optional if images are already the proper size}
Here's what that will look like (only relavent CSS was included in snippet above) 这就是它的样子(上面的片段中仅包含了relavent CSS)
转载地址:http://ancnb.baihongyu.com/