2013-02-12 57 views
34

我想实现看起来像这样的中间单元的响应表格的宽度text-overflow: ellipsis;文本溢出:省略号表细胞无宽度

| Button 1 | A one-lined text that is too long and has to be... | Button 2 |

整桌应该有width: 100%;到与设备一样大。我不能在Button 1Button 2上设置固定宽度,因为应用程序是多语言的(尽管应该可以使用max-width)。

我可以尝试任何我想要的,...只有当我设置固定宽度时才会出现。如何在没有JavaScript的帮助下告诉中间单元“使用可用空间”?

+2

如果你发布你已经尝试过的东西,这会有所帮助吗?至少有一些基地,我们可以复制粘贴和修改;) – TweeZz 2013-02-12 06:54:35

+0

我只试过一个表格,一行三个单元格... – kraftwer1 2013-02-12 12:07:20

+1

:)我的意思是你试过的CSS,因为我认为硬的部分 – TweeZz 2013-02-13 06:06:51

回答

0

[更新] 我的道歉,似乎不工作“就是这样”:http://jsfiddle.net/kQKfc/1/

会离开它虽然这里,因为它的某些情况下,它的伎俩我们。
[/更新]

我们有这个CSS在我们的项目,给它一个镜头:

.nowr 
{ 
    display: block; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    white-space: nowrap; 
} 
71

这是不是一个真正的干净的解决方案,但它不使用JS和每一个浏览器我的作品经过测试。

我的解决方案在于将单元格的内容包装在table-layout: fixedwidth: 100%的表格中。

查看demo

下面是完整的解决方案:

<table class="main-table"> 
    <tr> 
     <td class="nowrap">Button 1</td> 
     <td> 
      <table class="fixed-table"> 
       <tr> 
        <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus doloremque magni illo reprehenderit consequuntur quia dicta labore veniam distinctio quod iure vitae porro nesciunt. Minus ipsam facilis! Velit sapiente numquam.</td> 
       </tr> 
      </table> 
     </td> 
     <td class="nowrap">Button 2</td> 
    </tr> 
</table> 

.main-table { 
    width: 100%; 
} 

.fixed-table { 
    /* magic */ 
    width: 100%; 
    table-layout: fixed; 

    /*not really necessary, removes extra white space */ 
    border-collapse: collapse; 
    border-spacing: 0; 
    border: 0; 
} 
.fixed-table td { 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
} 

.nowrap { 
    /* used to keep buttons' text in a single line, 
    * remove this class to allow natural line-breaking. 
    */ 
    white-space: nowrap; 
} 

这不是真的清楚什么是对侧键的意图,所以我用white-space: nowrap,让他们在同一线。根据使用情况,您可能更愿意应用min-width并自然让他们换行。

+0

Perfecto,它的工作就像魅力....我给你的​​赏金点,但SO说它不能在23更多小时之前完成... – 2013-10-27 21:09:18

+2

Heheh没有担心,我希望看到比这更清晰的答案所以我们拭目以待吧。实际上,我在生产环境中有这个答案的代码,它并不觉得真的很干净,但是,我花了好几天的努力,这是唯一的解决方案,实际上在每个浏览器中都有效。 – 2013-10-27 21:10:44

+0

@AtulGupta感谢您试图将我的小提琴添加到OP(在编辑审阅队列中看到它),但我想这不是真正的地方。 ';''感谢您提出我的回答,但是我们拭目以待,看看是否有人在接下来的24小时内提出了一个更清洁的解决方案,然后您可以在您喜欢的时候奖励赏金。 ':P' – 2013-10-27 21:20:49

17

在桌子上设置你的宽度为100%,并指定一个fixed表格的布局:

table { 
    width: 100%; 
    table-layout: fixed; 
} 

然后,设置取下列表格单元格应该有省略号:

td:nth-child(2) { 
    overflow: hidden; 
    text-overflow: ellipsis; 
    white-space: nowrap; 
} 

瞧!响应式表省略号溢出!

演示:http://jsfiddle.net/VyxES/

+1

这是一个很好的解决方案,应用'table-layout:fixed'的唯一缺点是它平均分割单元格的宽度(除非手动设置),而不是基于内容。 – 2013-10-27 20:52:22

+0

的确如此,但OP要求的解决方案并未在单个表格单元上指定宽度(并且谢谢!)。 – pete 2013-10-27 20:53:57

+2

同意,大的缺点,OP要第一个和第三个单元格只有它们的内容一样宽,并且中心单元格占用了剩余的宽度。 – 2014-01-29 18:33:41

3

的清洁器的方法是简单地设置对TD一个最大宽度。

基于@法布里西奥磨砂的答复,

http://jsfiddle.net/V83Ae/89/

<table class="main-table"> 
<tr> 
    <td class="nowrap">Button 1</td> 
    <td class="truncate"> 
     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus doloremque magni illo reprehenderit consequuntur quia dicta labore veniam distinctio quod iure vitae porro nesciunt. Minus ipsam facilis! Velit sapiente numquam.</td> 
    <td class="nowrap">Button 2</td> 
</tr> 

而且

body { margin: 0; } 

.main-table { 
    width: 100%; 
} 

.truncate { 
    text-overflow: ellipsis; 
    white-space : nowrap; 
    overflow  : hidden; 
    max-width : 100px; /* Can be any size, just small enough */ 
} 

.nowrap { 
    white-space: nowrap; 
} 

我不知道为什么这个工作,但这样做,因此,如果有人可以计算出为什么应用最大宽度的原因,然后请让我知道,因为我是通过ac做的意外事件。

+0

这是美好的,并按预期工作。真的救了我的一天。 – 2017-09-13 21:29:26

2

经过几个小时的轰动,没有解决方案,终于找到了它。

你想要的基础溢出的元素是最小/最大宽度,其中最大值低于最小宽度。然后,您可以溢出语言或其子项,您可以在其中选择任何方式进行设置:%,px,auto。

我在多个棘手的情况下尝试过它,它在所有方面都有效。

我会尝试用我的例子发布一个幻灯片,但这应该会帮助你去。

/* Apply on base element for proper overflow */ 
max-width: 1px; 
min-width: 10000px; 

/* Apply on self or child, according to need */ 
width: 100%; 
overflow: hidden; 
white-space: nowrap; 
text-overflow: ellipsis; 
+0

这应该被接受为答案,因为它是最干净的工作解决方案。 – 2016-09-06 19:26:29

8

HTML

<tr> 
    <td> 
     <span class="ellipsis"> A very looooooooooooooooooooooooooooooong string</span> 
    </td> 
</tr> 

CSS

td { 
    position: relative; 
} 

.ellipsis { 
    /*Here is the trick:*/ 
    position: absolute; 
    width: 100%; 
    /*End of the trick*/ 

    text-overflow: ellipsis; 
    white-space: nowrap; 
    overflow: hidden; 
} 
+0

这对我来说是最好的解决方案。我看到,span是顶端:默认为10。在elipsis类中添加一个顶级值将是有益的。 – efirat 2016-09-21 13:45:13

0

Here you go

<div class="container"> 
    <div> 
    Button 1 
    </div> 
    <div class="fill"> 
    <div class="lockText"> 
     A one-lined text that is too long and has to be... 
    </div> 
    </div> 
    <div> 
    Button 2 
    </div> 
</div> 

诀窍是建立一个表的布局与第一占用剩余空间中的小区( CSS桌子,介意你!) 。

.container { 
    display: table; 
    width: 100%; 
} 

.container > div { 
    display: table-cell; 
    white-space: nowrap; 
    position : relative; 
    width : auto; 

    /*make it easier to see what's going on*/ 
    padding : 2px; 
    border : 1px solid gray; 
} 

.container > .fill { 
    width : 100%; 
} 

然后添加您的限制自己的文本。

.lockText { 
    position : absolute; 
    left : 0; 
    right : 0; 
    overflow: hidden; 
    text-overflow: ellipsis; 
}