Largura Fixa Da Célula Da Tabela
muitas pessoas ainda usam tabelas para os controlos de layout, dados, etc. - um exemplo disto é o popular jqGrid. No entanto, há alguma magia acontecendo que eu não consigo entender (suas mesas, por amor de Deus, quanta magia poderia haver?)
Como é possível definir a largura da coluna de uma tabela e obedecê-la como o jqGrid faz!? Se eu tentar replicar isso, mesmo se eu definir cada {[[0]}, assim que o conteúdo de uma dessas células for maior que 20px, a cela expande-se! Alguma ideia ou ideia?7 answers
Poderá tentar usar a marca <col>
para gerir o estilo da tabela para todas as linhas, mas terá de definir o estilo table-layout:fixed
na classe <table>
ou nas tabelas CSS e definir o estilo overflow
para as células
Http://www.w3schools.com/TAGS/tag_col.asp
<table class="fixed">
<col width="20px" />
<col width="30px" />
<col width="40px" />
<tr>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
</table>
E este é o teu CSS.
table.fixed { table-layout:fixed; }
table.fixed td { overflow: hidden; }
table.fixed {table-layout:fixed; width:90px;}/*Setting the table width is important!*/
table.fixed td {overflow:hidden;}/*Hide text outside the cell.*/
table.fixed td:nth-of-type(1) {width:20px;}/*Setting the width of column 1.*/
table.fixed td:nth-of-type(2) {width:30px;}/*Setting the width of column 2.*/
table.fixed td:nth-of-type(3) {width:40px;}/*Setting the width of column 3.*/
<table class="fixed">
<tr>
<td>Veryverylongtext</td>
<td>Actuallythistextismuchlongeeeeeer</td>
<td>We should use spaces tooooooooooooo</td>
</tr>
</table>
Tens de pôr a mesa mesmo na solução da assombração . Caso contrário, não funciona.
Também uma nova característica CSS3 que Vsync sugeriu é: word-break:break-all;
. Isto irá quebrar as palavras sem espaços nelas para várias linhas também. Apenas modifique o código assim:
table.fixed { table-layout:fixed; width:90px; word-break:break-all;}
Final resultado
table td
{
table-layout:fixed;
width:20px;
overflow:hidden;
word-wrap:break-word;
}
<td><div style='width: 150px;'>Text to break here</div></td>
Você não precisa especificar qualquer tipo de estilo para mesa, TR elementos. Você também pode usar o overflow: hidden; como sugerido por outras respostas, mas isso faz com que o excesso de texto desapareça.
table {
table-layout:fixed; width:200px;
}
table tr {
height: 20px;
}
10x10
Para a tabela de largura do ecrã completo:
A largura da mesa deve ser 100%
Se necessário n colunms, então THs deve ser n+1
Exemplo para 3 colunas:
table.fixed {
table-layout: fixed;
width: 100%;
}
table.fixed td {
overflow: hidden;
}
<table class="fixed">
<col width=20 />
<col width=20 />
<col width=20 />
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>FREE</th>
</tr>
<tr>
<td>text111111111</td>
<td>text222222222</td>
<td>text3333333</td>
</tr>
</table>
table
{
table-layout:fixed;
}
td,th
{
width:20px;
word-wrap:break-word;
}
:primeiro filho ... : nth-child(1) or ...