简体   繁体   English

用省略号截断浮动div内容

[英]Truncate floated div content with ellipsis

I'm looking to create a dynamic layout in CSS that truncates text in a floated div using text-overflow: ellipsis in a way that takes into account the size of sibling elements 我正在寻找在CSS中创建一个动态布局,以使用文本溢出功能将浮动div中的文本截断:省略号,同时考虑到同级元素的大小

The layout consists of 3 divs 布局包含3个div

|--------------------A-------------------|
||------B------||-------C-------|        |
|| Hello world || Goodbye world |        |
||-------------||---------------|        |
|----------------------------------------|
  • Div A: Wrapper parent Div A:包装父级
  • Div B: show as much content as possible, and truncate to ellipsis if not enough space B部:显示尽可能多的内容,如果没有足够的空间,则截断为省略号
  • Div C: all of this content should always be visible. Div C:所有这些内容应始终可见。 The width of this content is not fixed (but will always be <50% of the width of div A) . 此内容的宽度不是固定的(但始终小于div A宽度的50%)。 If the content in B is not truncated, this div should directly beside div B. 如果B中的内容未被截断,则该div应该直接在div B旁边。

And in the case there div B should truncate text, I want it to look like: 如果div B应该截断文本,我希望它看起来像:

|--------------------A-------------------|
||------B--------------||-------C-------||
|| Hello world this ...|| Goodbye world ||
||---------------------||---------------||
|----------------------------------------|

I can get the general layout set up, but my problem is I cannot trigger Div B to truncate the content when needed. 我可以设置一般布局,但是我的问题是我无法触发Div B在需要时截断内容。 My code so far: 到目前为止,我的代码:

 #wrapper { max-width: 300px; } #div-b { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; float: left; margin-right: 5px; } #div-c { color: #6a7d80; white-space: nowrap; } 
 <div id="wrapper"> <div id="div-b">This is my really long message hello world hello world hello world </div> <div id="div-c">Goodbye world</div> </div> 

Adding a max-width to div B works, but the problem is that the content in div C does change so I cannot hardcode a max-width value that works in all cases. 将最大宽度添加到div B可以工作,但是问题是div C中的内容确实发生了变化,因此我无法对在所有情况下都可以使用的最大宽度值进行硬编码。

I'm starting to think this isn't possible without javascript. 我开始认为如果没有JavaScript,这是不可能的。 Any ideas? 有任何想法吗?

Just use flexible boxes : 只需使用灵活的盒子

#wrapper {
    display: flex;
}

 .wrapper { display: flex; border: 1px solid red; } .w1 { max-width: 300px; margin-bottom: -1px; } .w2 { max-width: 600px; } .div-b { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; margin-right: 5px; } .div-c { color: #6a7d80; white-space: nowrap; } 
 <div class="wrapper w1"> <div class="div-b">This is my really long message hello world hello world hello world </div> <div class="div-c">Goodbye world</div> </div> <div class="wrapper w2"> <div class="div-b">This is my really long message hello world hello world hello world </div> <div class="div-c">Goodbye world</div> </div> 

That is enough on Firefox. 在Firefox上就足够了。 But for webkit browsers, you may also need 但是对于Webkit浏览器,您可能还需要

#div-b {
    width: 0;               /* Reduce width as much as necessary */
    flex-grow: 1;           /* Grow to fill remaining space */
    max-width: max-content; /* But don't grow too much */
}

Note that max-content has much limited browser support, and currently require vendor extensions. 请注意, max-content对浏览器的支持非常有限,并且当前需要供应商扩展。

 .wrapper { display: flex; border: 1px solid red; } .w1 { max-width: 300px; margin-bottom: -1px; } .w2 { max-width: 600px; } .div-b { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; margin-right: 5px; width: 0; flex-grow: 1; max-width: -moz-max-content; max-width: -webkit-max-content; max-width: max-content; } .div-c { color: #6a7d80; white-space: nowrap; } 
 <div class="wrapper w1"> <div class="div-b">This is my really long message hello world hello world hello world </div> <div class="div-c">Goodbye world</div> </div> <div class="wrapper w2"> <div class="div-b">This is my really long message hello world hello world hello world </div> <div class="div-c">Goodbye world</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM