我正在尝试在我的桌子width: 100%;
上创建第二个单元格并将文本保留在一行上。
移动版本运行正常,但我找不到桌面版的解决方案,因为最后一个单元格不会折叠第二个单元格中的文本。
链接:CodePen
此外,这种方式有效:
table {
table-layout: fixed;
width: 100%;
}
但是所有单元格的宽度均匀。 (我希望第二个单元格为全宽,其余单元格适应内部的内容)
谢谢!
答案 0 :(得分:0)
不确定最终目标,但是当遇到这样的问题时,我通常使用div而不是使用表来创建伪表。这将允许您比使用表格单元格更自由地操纵块定位。
答案 1 :(得分:0)
您应该使用th
来设置col的宽度:
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
table {
border-collapse: collapse;
margin: 20px auto;
table-layout: fixed;/* will also fix width to cells if specified */
width: 100%;
}
table thead tr th {
text-align: left;
font-weight: bold;
text-transform: uppercase;
background: #f8f8f8;
}
table thead tr th,
table tbody tr td {
padding: 10px;
}
table tbody tr td {
border: 1px solid #e5e5e5;
}
table tbody tr,
table thead tr {
border: 1px solid #e5e5e5;
}
/* set th width */
table thead tr th:first-child {
border-right: 1px solid #e5e5e5;
width: 2em;/* should be enough*/
}
/* LAST TWO CELLS */
table thead tr th:nth-child(2) ~th {
width: 4em;/* should be enough*/
}
/* end th width */
table tbody tr td:first-child {
text-align:right;
padding:0 0.25em 0 0
}
/* SECOND CELL */
table tbody tr td:nth-child(2) {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
table tbody tr td:nth-child(2) ~td {
background: red;
text-align:center;
}
/* RESPONSIVE */
@media screen and (max-width: 600px) {
table {
width: 100%;
}
table thead {
display: none;
}
table tbody tr {
display: block;
}
table tbody tr td {
display: block;
text-align: center;
border: 0;
border-bottom: 1px solid #e4e5e7;
}
table tbody tr td:first-child {
background: #f8f8f8;
}
table tbody tr td:last-child {
border-bottom: 0px;
}
table tbody tr td:before {
content: attr(data-th);
font-weight: bold;
}
}
<table>
<thead>
<tr>
<th>ID</th>
<th>Post Title</th>
<th></th>
<th></th>
</tr>
<tbody>
<tr>
<td data-th="POST ID: ">1</td>
<td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
<td><a href="#">Edit</a>
</td>
<td><a href="#">Delete</a>
</td>
</tr>
<tr>
<td data-th="POST ID: ">01</td>
<td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
<td><a href="#">Edit</a>
</td>
<td><a href="#">Delete</a>
</td>
</tr>
<tr>
<td data-th="POST ID: ">150</td>
<td data-th="TITLE: ">Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
<td><a href="#">Edit</a>
</td>
<td><a href="#">Delete</a>
</td>
</tr>
</table>