css 文本溢出省略号在嵌套的flexbox中无效

llycmphe  于 2023-08-09  发布在  其他
关注(0)|答案(4)|浏览(178)

我有一个用flexbox创建的两列布局。
在右列中,我有两行,第一行包含标题,第二行包含页面内容。
在标题中,我有三列,一个按钮,一些文本和一个按钮。
我希望按钮坐在左边和右边的列与文字占用任何额外的空间。
最后,我希望文本具有white-space:nowraptext-overflow:ellipsis属性,以截断长标题。
我的问题是:我无法让文本换行在嵌套在另一个flexbox中的flexbox中正确工作,如下面的fiddle所示:
http://jsfiddle.net/maxmumford/rb4sk3mz/3/

  1. .container {
  2. display: flex;
  3. height: 100vh;
  4. }
  5. .left {
  6. width: 200px;
  7. background-color: yellow;
  8. }
  9. .right {
  10. flex: 1;
  11. background-color: blue;
  12. color: white;
  13. }
  14. .header {
  15. background: red;
  16. color: white;
  17. display: flex;
  18. flex-direction: row;
  19. }
  20. .header .content {
  21. white-space: nowrap;
  22. flex: 0 1 auto;
  23. text-overflow: ellipsis;
  24. overflow: hidden;
  25. min-width: 0;
  26. }
  27. .header .buttons {
  28. background: green;
  29. flex: 1 0 auto;
  30. }
  31. .header .content:hover {
  32. white-space: normal;
  33. }

个字符
然而,当header没有嵌套在flex box中时,完全相同的代码也可以工作:
http://jsfiddle.net/maxmumford/p7115f2v/1/

  1. .header {
  2. background: red;
  3. color: white;
  4. display: flex;
  5. flex-direction: row;
  6. }
  7. .header .content {
  8. white-space: nowrap;
  9. flex: 0 1 auto;
  10. text-overflow: ellipsis;
  11. overflow: hidden;
  12. min-width: 0;
  13. }
  14. .header .buttons {
  15. background: green;
  16. flex: 1 0 auto;
  17. }
  1. <div class="header">
  2. <div class="buttons">buttons</div>
  3. <div class="content">
  4. 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
  5. and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly...
  6. </div>
  7. <div class="buttons">buttons</div>
  8. </div>

的字符串
我怎样才能实现我的第一目标呢?
谢啦,谢啦

hmtdttj4

hmtdttj41#

您的代码中存在两个问题,导致省略号无法工作:

  1. 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相同
  1. .container {
  2. display: flex;
  3. height: 100vh;
  4. }
  5. .left {
  6. width: 200px;
  7. background-color: yellow;
  8. }
  9. .right {
  10. flex: 1;
  11. background-color: blue;
  12. color: white;
  13. min-width: 0; /* NEW */
  14. }
  15. .header {
  16. background: red;
  17. color: white;
  18. display: flex;
  19. flex-direction: row;
  20. }
  21. .header .content {
  22. white-space: nowrap;
  23. flex: 1; /* ADJUSTMENT */
  24. text-overflow: ellipsis;
  25. overflow: hidden;
  26. min-width: 0;
  27. }
  28. .header .buttons {
  29. background: green;
  30. flex: 1 0 auto;
  31. }
  32. .header .content:hover {
  33. white-space: normal;
  34. }

个字符
revised fiddle

展开查看全部
eulz3vhy

eulz3vhy2#

的值:

  1. white-space: nowrap;
  2. text-overflow: ellipsis;
  3. overflow: hidden;

字符串
只有当元素的内容超过其宽度时才有效。
您可以设置.header .content的宽度:

  1. .header .content {
  2. width: 50%;
  3. }


它会像你期望的那样工作:
http://jsfiddle.net/t82pqks4/

展开查看全部
dphi5xsq

dphi5xsq3#

上面的解决方案对我来说在一个非常嵌套的Flex布局中不起作用。它一直使我的h3标记比父标记宽,因为

  1. white-space: nowrap;
  2. overflow: hidden;
  3. text-overflow: ellipsis;

字符串
但我意外地发现,将display: inline-grid;添加到父级解决了这个问题,并正确地应用了省略号。
我不知道这对你有没有帮助,但对我有帮助。

p5fdfcr1

p5fdfcr14#

我希望这个解决方案能帮助到一些人,因为我发现没有其他方法可以工作,因为我的页面是一个巨大的flexbox嵌套组合。所以min-width 0不起作用,除了这个,我什么都没试过。
在包含要换行的副本的flex列中,您需要执行此操作。

  1. .row {
  2. display: flex;
  3. }
  4. .col1 {
  5. position: relative;
  6. flex: 1 1 70%;
  7. overflow: hidden;
  8. }
  9. .nested {
  10. width: 100%;
  11. position: absolute;
  12. white-space: nowrap;
  13. overflow: hidden;
  14. text-overflow: ellipsis;
  15. }
  16. .col2 {
  17. flex: 1 1 30%;
  18. padding-left: 1rem;
  19. }

个字符

展开查看全部

相关问题