【发布时间】:2016-02-04 18:47:19
【问题描述】:
我有一个响应式设计,它连续包含 3 个项目。如果屏幕变小,第一个将被删除。所以这 3 个项目是用 flexbox 和justify-content:space-between 分配的,这使得外部项目绑定到侧面,中间项目居中。现在如果第一个项目被删除,我想成为右侧的右侧项目,左侧的项目以左侧的空间为中心。
这是我的 3 项代码:
<div style="display:flex;flex-flow:row;justify-content:space-between;align-items:center">
<span style="width:70px;text-align:right">To the left</span>
<div style="height:40px;width:100px;background-color:red">Center</div>
<span style="width:70px;text-align:right">To the right</span>
</div>
这两个项目:
<div style="display:flex;flex-flow:row;justify-content:space-between;align-items:center">
<div style="height:40px;width:100px;background-color:red">Center</div>
<span style="width:70px;text-align:right">To the right</span>
</div>
我为你准备了一些小提琴:https://jsfiddle.net/msLd7g5j/1/
编辑:请注意,这不是重复的,至少不是您指出的那个,因为我希望左侧项目居中于“剩余”而不是绝对居中的空间。
解决方案:我有一个新的小提琴:https://jsfiddle.net/msLd7g5j/2/
我只是不删除第三个元素,而是删除内容,所以元素看起来像
`<span> </span>`
flexbox 只是完成它的工作并将中间元素居中到相对中间,并设置justify-content:space-between。
【问题讨论】: