gpt4 book ai didi

css - 填充剩余空间直到最大宽度

转载 作者:行者123 更新时间:2023-12-04 08:30:25 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why don't flex items shrink past content size?

(4 个回答)


5年前关闭。




我需要用两个元素制作一个面板标题。第一个获得了大部分空间,而第二个只获得了他需要的空间。

结构如下:

.flex-child> display: flex
.child-1 > flex-grow: 1
.child-2 > align-self: center

出于样式原因,第一个元素的文本为 white-space: nowrap , 如果大于父级,则显示省略号。但是有 display: flex 的父级不将元素限制在其可用空间内。

下面的代码片段显示了元素在 时的状态。 1) 第一个元素有很多文本; 2) 第一个元素有合理数量的文本;和 3) 我希望元素如何出现,但我只能用一个魔数(Magic Number)来做 max-width .使用相同代码的 fiddle : https://jsfiddle.net/mjtf4ec3/1/ .

.fixed-width-parent {
margin: 1em 2em;
width: 300px;
background: #dedede;
padding: 15px;
}

.unbreakable-text {
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

.flex-child {
display: flex;
}

.child-1 {
flex-grow: 1;
}

.child-2 {
align-self: center;
margin-left: 5px;
}

.fancy-button {
padding: 5px;
background-color: #1a1;
border: 1px solid #080;
border-radius: 5px;
color: #fff;
}

.expectation .child-1 {
min-width: 80%;
}

.unimportant {
color: #bbb;
}

body {
font-family: sans-serif;
font-size: 14px;
}
<div class="fixed-width-parent">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>Very long text NOT OK</strong>
Lorem ipsum dolor sit amet, consectetur
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

<div class="fixed-width-parent">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>Short text OK</strong>
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

<div class="fixed-width-parent expectation">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>EXPECTATION (without <code>max-width</code> as %)</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

最佳答案

只需设置min-width: 0px.child-1 , 向 flexbox 布局表明可以缩小该子项。这就是你真正需要的。下面的演示(只添加了这一行)。

.fixed-width-parent {
margin: 1em 2em;
width: 300px;
background: #dedede;
padding: 15px;
}

.unbreakable-text {
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

.flex-child {
display: flex;
}

.child-1 {
flex-grow: 1;
min-width: 0px; /* added this */
}

.child-2 {
align-self: center;
margin-left: 5px;
}

.fancy-button {
padding: 5px;
background-color: #1a1;
border: 1px solid #080;
border-radius: 5px;
color: #fff;
}

.expectation .child-1 {
min-width: 80%;
}

.unimportant {
color: #bbb;
}

body {
font-family: sans-serif;
font-size: 14px;
}
<div class="fixed-width-parent">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>Very long text NOT OK</strong>
Lorem ipsum dolor sit amet, consectetur
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

<div class="fixed-width-parent">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>Short text OK</strong>
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

<div class="fixed-width-parent expectation">
<div class="flex-child">
<div class="child-1">
<div class="unbreakable-text">
<strong>EXPECTATION (without <code>max-width</code> as %)</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</div>
<div class="unimportant">Unimportant text
<br> Very unimportant</div>
</div>
<div class="child-2">
<div class="fancy-button">
Button
</div>
</div>
</div>
</div>

关于css - 填充剩余空间直到最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com