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; 
}