我有一个用flexbox创建的两列布局。
在右列中,我有两行,第一行包含标题,第二行包含页面内容。
在标题中,我有三列,一个按钮,一些文本和一个按钮。
我希望按钮坐在左边和右边的列与文字占用任何额外的空间。
最后,我希望文本具有white-space:nowrap
和text-overflow:ellipsis
属性,以截断长标题。
我的问题是:我无法让文本换行在嵌套在另一个flexbox中的flexbox中正确工作,如下面的fiddle所示:
http://jsfiddle.net/maxmumford/rb4sk3mz/3/
.container {
display: flex;
height: 100vh;
}
.left {
width: 200px;
background-color: yellow;
}
.right {
flex: 1;
background-color: blue;
color: white;
}
.header {
background: red;
color: white;
display: flex;
flex-direction: row;
}
.header .content {
white-space: nowrap;
flex: 0 1 auto;
text-overflow: ellipsis;
overflow: hidden;
min-width: 0;
}
.header .buttons {
background: green;
flex: 1 0 auto;
}
.header .content:hover {
white-space: normal;
}
个字符
然而,当header没有嵌套在flex box中时,完全相同的代码也可以工作:
http://jsfiddle.net/maxmumford/p7115f2v/1/的
.header {
background: red;
color: white;
display: flex;
flex-direction: row;
}
.header .content {
white-space: nowrap;
flex: 0 1 auto;
text-overflow: ellipsis;
overflow: hidden;
min-width: 0;
}
.header .buttons {
background: green;
flex: 1 0 auto;
}
<div class="header">
<div class="buttons">buttons</div>
<div class="content">
This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long
and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly...
</div>
<div class="buttons">buttons</div>
</div>
的字符串
我怎样才能实现我的第一目标呢?
谢啦,谢啦
4条答案
按热度按时间hmtdttj41#
您的代码中存在两个问题,导致省略号无法工作:
div.right
包含div.header
,后者又包含按钮和文本。div.right
是主容器(.container
)中的一个flex项。默认情况下为a flex item cannot be smaller than the size of its content。Flex项目的初始设置为
min-width: auto
。这意味着文本的长度(不换行)将为父Flex项设置最小大小。一种使flex项缩小到其内容之外的方法是将flex项设置为
min-width: 0
。您已经为
.content
flex项完成了这一操作,但是还需要在.right
项上进行设置。1.您已经将
.content
元素设置为flex: 0 1 auto
。这告诉flex项使用内容的大小(
flex-basis: auto
)。文本设置大小。相反,将宽度设置为0,并根据需要让Flex容器分配空间。您可以使用
flex: 1 1 0
执行此操作,它与flex: 1
相同。个字符
revised fiddle的
eulz3vhy2#
的值:
字符串
只有当元素的内容超过其宽度时才有效。
您可以设置
.header .content
的宽度:型
它会像你期望的那样工作:
http://jsfiddle.net/t82pqks4/
dphi5xsq3#
上面的解决方案对我来说在一个非常嵌套的Flex布局中不起作用。它一直使我的
h3
标记比父标记宽,因为字符串
但我意外地发现,将
display: inline-grid;
添加到父级解决了这个问题,并正确地应用了省略号。我不知道这对你有没有帮助,但对我有帮助。
p5fdfcr14#
我希望这个解决方案能帮助到一些人,因为我发现没有其他方法可以工作,因为我的页面是一个巨大的flexbox嵌套组合。所以min-width 0不起作用,除了这个,我什么都没试过。
在包含要换行的副本的flex列中,您需要执行此操作。
个字符