2010-03-25 83 views

回答

3

嘗試white-space: nowrapoverflow: hidden包裹每個單元格中的內容與其他元素(表單元格不支持overflow :hidden),並減少該元素的font-size,直到它的內容融入它。

示例代碼:

HTML:

<table> 
    <tbody> 
    <tr> 
     <td><span>cell</span></td> 
     <td><span>another cell</span></td> 
     <td><span>yet another cell</span></td> 
    </tr> 
    </tbody> 
</table> 

CSS:

td span { 
    display: block; 
    white-space: nowrap; 
    width: 100px; 
    overflow: hidden; 
    font-size: 100%; 
} 

JS:

$(function() { 
    $('td span').each(function() { 
    var fontSize = 100; 
    while (this.scrollWidth > $(this).width() && fontSize > 0) { 
     // adjust the font-size 5% at a time 
     fontSize -= 5; 
     $(this).css('font-size', fontSize + '%'); 
    } 
    }); 
}); 

Working version of the example (JSBin)

+0

謝謝,這對我有用! – Poru 2011-07-21 17:16:40

相關問題