我需要制作一个包含两个元素的面板标题。第一个获得大部分空间,而第二个获得他需要的空间。
结构如下:
.flex-child> display: flex
.child-1 > flex-grow: 1
.child-2 > align-self: center
出于样式原因,第一个元素的文本为white-space: nowrap
,以显示省略号,如果大于父级。但display: flex
的父级不会将元素限制在其可用空间内。
下面的代码片段显示了 1)元素在第一个元素有大量文本时的元素; 2)第一个元素有合理的文本量;和 3)我希望元素如何显示,但我只能使用幻数作为max-width
。使用相同代码的小提琴: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>
答案 0 :(得分:3)
只需在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;
}
&#13;
<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>
&#13;
答案 1 :(得分:1)
不确定跨浏览器兼容性,但我注意到overflow:hidden
(text-overflow: ellipsis;
工作所必需的)在flex元素上的行为不同于块元素。鉴于此,您可以将它添加到一个没有声明宽度的元素中,并且仍然可以隐藏&#34;溢出&#34;在一定条件下。同样,不确定兼容性,但尝试将overflow:hidden
添加到.child-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;
overflow: hidden; /* added here */
}
.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;
}
&#13;
<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>
&#13;