用flex从溢流包装中删除额外的水平宽度
本质上,我试图删除/防止当块元素的水平行包裹着任何一个块元素时,创建的额外水平宽度。也就是说,我希望包含溢出元素的div
的宽度仅为非包装元素的大小。
以下代码可用于查看所讨论的问题:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="example.css">
</head>
<body>
<div class="card">
<div class="card-content">
<div>
<div class="row">
<p class="note-title">This is a relatively lengthy example title for example purposes.</p>
<div>
<div class="row">
<div class="note-tags-list">
<p id="blank-tag">.</p>
<p class="tag note-tag">crunchy</p>
<p class="tag note-tag">recipe</p>
<p class="tag note-tag">carrot</p>
</div>
<p id="text-that-should-stay">I stay</p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
/* example.css */
@import "https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css";
#text-that-should-stay {
white-space: nowrap;
color: white;
}
#blank-tag {
width: 30px;
min-width: 30px;
}
.note-title {
white-space: nowrap;
color: white;
overflow: hidden;
text-overflow: ellipsis;
}
.card {
background-color: black;
}
.row {
display: flex;
justify-content: space-between;
}
.note-tag {
width: 175px;
min-width: 0px;
}
.note-tags-list {
display: flex;
flex-flow: row wrap;
overflow: hidden;
height: 24px;
justify-content: flex-end;
}
为了轻松,可以在以下JSFiddle中看到上面的代码,尽管以上代码在本地运行上述代码将产生相同的结果: https://jsfiddle.net/5l7u2gkp/1/ 。
这是一个显示问题的GIF,然后在视觉上是理想的:
Essentially, I am trying to remove/prevent the extra horizontal width created when a horizontal row of block elements has any of the block elements wrapped. That is, I want the width of the div
containing the overflow elements to be just the size of the nonwrapped elements.
The following code can be used to see the issue in question:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="example.css">
</head>
<body>
<div class="card">
<div class="card-content">
<div>
<div class="row">
<p class="note-title">This is a relatively lengthy example title for example purposes.</p>
<div>
<div class="row">
<div class="note-tags-list">
<p id="blank-tag">.</p>
<p class="tag note-tag">crunchy</p>
<p class="tag note-tag">recipe</p>
<p class="tag note-tag">carrot</p>
</div>
<p id="text-that-should-stay">I stay</p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
/* example.css */
@import "https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css";
#text-that-should-stay {
white-space: nowrap;
color: white;
}
#blank-tag {
width: 30px;
min-width: 30px;
}
.note-title {
white-space: nowrap;
color: white;
overflow: hidden;
text-overflow: ellipsis;
}
.card {
background-color: black;
}
.row {
display: flex;
justify-content: space-between;
}
.note-tag {
width: 175px;
min-width: 0px;
}
.note-tags-list {
display: flex;
flex-flow: row wrap;
overflow: hidden;
height: 24px;
justify-content: flex-end;
}
For ease, the code above can be seen in the following JSFiddle, though running the above code locally will produce the same result: https://jsfiddle.net/5L7u2gkp/1/.
Here is a gif that shows the issue and then what would be ideal visually:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第二次编辑
我可能不会让您正确,但是删除空白标签时没有额外的宽度。
如果您在 gap 之间谈论
node-title
和note-tags-list
,它将源自<代码> Flex-GROW:0; ,这是Flex项目的默认值。您可以添加到.tag
flex-Grow
属性为flex-grow:1;
,它将使您的标签生长到它们可以获得的最大空间。编辑 - 根据您的
so actually, the excess width is coming from the `#blank-tag`. You set its width to `30px`. If you remove it / change its width to 0, it will solve your problem. Otherwise, there will always be a 30px gap.
评论
与您的元素宽度无关;当您使用
flexbox
时,Chrome DevTools表示元素的位置。您可以(并且应该!)阅读更多在这里。您可以通过更改DevTool中的
div
的宽度来自行检查它(如您添加的图片所示),并且您会发现显示器不会发生变化。SECOND EDIT
I might not get you right, but there is no extra width when you remove the blank tag.
If you are talking about the gap between
node-title
andnote-tags-list
, it derives fromflex-grow: 0;
, which is the default for flex items. You can add to.tag
theflex-grow
property asflex-grow: 1;
and it will make your tags grow to the maximum space they can get.EDIT - based on your comments
so actually, the excess width is coming from the `#blank-tag`. You set its width to `30px`. If you remove it / change its width to 0, it will solve your problem. Otherwise, there will always be a 30px gap.
the
It is not related to the width of your elements; it's how chrome devtools represent the position of your elements when you use
flexbox
. You can (and should!) read more here.You can check it yourself by changing the width of the
div
in the devtools to 202px (as seen in the picture you added), and you'll see there will be no change in the display.它是因为
flex-flow:row wrap;
要澄清, flex-flow wrap wrap是
.note-tags-list
,当该空间不足以显示其中的项目(酥脆|食谱|胡萝卜),垂直上,垂直放置的项目不可见,因为其父母的高度固定高。答案是删除
flex-flow:row wrap;
请参阅这个小提琴fork
Its because of
flex-flow: row wrap;
To clarify, The flex-flow wrap's the content inside
.note-tags-list
,When the space is not enough to display items inside it(crunchy|recipe|carrot),Vertically,The items that are placed vertically are not visible,as there is fixed height to one of its parent.The answer is to remove
flex-flow: row wrap;
See This fiddle-fork
您如何看待这个答案?我只稍微更改了HTML代码。
我更改
的
最小宽度
。现在...
正在工作
what do you think about this answer? i only slightly changed the HTML codes.
i change the
min-width
of.note-tag
and set aflex-grow
for it.and now ...
is working