2015-11-03 67 views
-1

嘿,我想知道你是否可以有一个HTML表,并使某些列有whitespace: nowrap和其他列有whitespace: normalHTML表格与空白nowrap

例子:

<table> 
    <thead> 
     <tr> 
      <th>No wrap</th> 
      <th>Wrap</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Small text</td> 
      <td>Wrap this longgggg text</td> 
     </tr> 
    </tbody> 
</table> 
+0

这将是很好,如果你能举一个例子.. –

回答

0

CSS white-spacenormal默认情况下,因此,所有你需要的是将其设置为nowrap一些列。一种方法是将一个类添加到他们,因为:

<td class="my_nowrap">this won't wrap</td> 
<style> 
.my_nowrap { 
    white-space: nowrap; 
} 
</style> 

其他的方式是使用CSS3 nth-child selector和(无需设置类)针对某些列,如:

<style> 
td:nth-child(2), 
td:nth-child(3) { 
.my_nowrap { 
    white-space: nowrap; 
} 
</style> 

上面会将第二和第三列设置为nowrap

0

是的,这是可能的。看看下面的例子。

table, th, td { 
 
    border: 1px solid black; 
 
    border-collapse: collapse; 
 
} 
 
th, td { 
 
    padding: 5px; 
 
    text-align: left;  
 
} 
 

 
tr.wrap { 
 
    width: 50px; 
 

 
} 
 

 
th.wrap, td.wrap { 
 
    background: red; 
 
    overflow: hidden; 
 
    white-space: nowrap; 
 
    text-overflow: ellipsis; 
 
    max-width: 10px; 
 
}
<table style="width:100%"> 
 
    <tr> 
 
    <th class='wrap'>Name</th> 
 
    <th colspan="2">Telephone</th> 
 
    </tr> 
 
    <tr> 
 
    <td class='wrap'>Bill Gates</td> 
 
    <td>555 77 854</td> 
 
    <td>555 77 855</td> 
 
    </tr> 
 
</table>