为了账号安全,请及时绑定邮箱和手机立即绑定

在flexbox中使div填充剩余*水平*空间

在flexbox中使div填充剩余*水平*空间

潇湘沐 2019-07-30 16:47:20
在flexbox中使div填充剩余*水平*空间我在flexbox中有两个div并排。右手应该总是相同的宽度,我希望左手一个只抓住剩余的空间。但除非我专门设定宽度,否则不会。所以目前,它设置为96%,看起来没问题,直到你真的挤压屏幕 - 然后右手div有点缺乏它所需的空间。我想我可以保留它,但它感觉不对 - 就像必须有一种说法:合适的人总是一样的; 你在左边 - 你得到了剩下的一切.ar-course-nav {   cursor: pointer;   padding: 8px 12px 8px 12px;   border-radius: 8px;}.ar-course-nav:hover {   background-color: rgba(0, 0, 0, 0.1);}<br/><br/><div class="ar-course-nav" style="display:flex; justify-content:space-between;">   <div style="width:96%;">     <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">       <strong title="Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!">                 Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!            </strong>     </div>     <div style="width:100%; display:flex; justify-content:space-between;">       <div style="color:#555555; margin-right:8px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;" title="A really really really really really really really really really really really long department name">         A really really really really really really really really really really really long department name      </div>       <div style="color:#555555; text-align:right; white-space:nowrap;">         Created: 21 September 2016      </div>     </div>   </div>   <div style="margin-left:8px;">     <strong>&gt;</strong>   </div></div>
查看完整描述

2 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

使用该flex-grow属性可使Flex项目占用主轴上的可用空间。

此属性将尽可能扩展项目,将长度调整为动态环境,例如屏幕重新调整大小或添加/删除其他项目。

一个常见的例子是flex-grow: 1或使用速记属性flex: 1

因此,不要width: 96%使用你的div,而是使用flex: 1


你写了:

所以目前,它设置为96%,看起来没问题,直到你真的挤压屏幕 - 然后右手div有点缺乏它所需的空间。

压缩固定宽度div与另一个flex属性有关: flex-shrink

默认情况下,将flex项设置为flex-shrink: 1允许它们缩小以防止容器溢出。

要禁用此功能,请使用flex-shrink: 0

有关详细信息,请参阅答案中flex-shrink因子部分:


了解有关沿主轴的柔性对齐的更多信息:

在此处了解有关沿交叉轴的柔性对齐的更多信息:


查看完整回答
反对 回复 2019-07-30
?
繁星coding

TA贡献1797条经验 获得超4个赞

基本上我试图让我的代码在'行'上有一个中间部分来自动调整两边的内容(在我的例子中,是一个虚线分隔符)。就像@Michael_B建议的那样,关键是display:flex在行容器上使用,并至少确保行上的中间容器的flex-grow值至少为1(如果外部容器没有flex-grow应用任何属性)。

这是我试图做的图片以及我如何解决它的示例代码。

编辑:虚线底部边框可能看起来很奇怪,具体取决于浏览器的显示方式。我个人建议使用黑色圆圈SVG作为重复的背景图像,尺寸合适并定位在中间容器的底部。当我有时间时,会添加这个替代解决方案。

.row {
  background: lightgray;
  height: 30px;
  width: 100%;
  display: flex;
  align-items:flex-end;
  margin-top:5px;}.left {
  background:lightblue;}.separator{
  flex-grow:1;
  border-bottom:dotted 2px black;}.right {
  background:coral;}
<div class="row">
  <div class="left">Left</div>
  <div class="separator"></div>
  <div class="right">Right With Text</div></div><div class="row">
  <div class="left">Left With More Text</div>
  <div class="separator"></div>
  <div class="right">Right</div></div><div class="row">
  <div class="left">Left With Text</div>
  <div class="separator"></div>
  <div class="right">Right With More Text</div></div>


查看完整回答
反对 回复 2019-07-30
  • 2 回答
  • 0 关注
  • 1878 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信