这是我的布局:
body {
font: 12px sans-serif;
}
#div1 {
width: 750px;
padding-top: 20px;
padding-bottom: 20px;
background-color: lime;
}
#table1 {
background-color: white;
}
#table1 th, #table1 td {
white-space: nowrap;
}
.class1 {
background-color: pink;
}
/* all background colors are for testing */
<div id="div1">
<table id="table1">
<tr>
<th>Name</th>
<th>Col 1</th>
<th>Col 2</th>
<th>Fr<br/>10</th>
<th>Sa<br/>11</th>
<th>Su<br/>12</th>
<th>Mo<br/>13</th>
<th>Tu<br/>14</th>
<th>We<br/>15</th>
<th>Th<br/>16</th>
<th>Fr<br/>17</th>
<th>Sa<br/>18</th>
<th>Su<br/>19</th>
<th>Mo<br/>20</th>
<th>Tu<br/>21</th>
<th>We<br/>22</th>
<th>Th<br/>23</th>
</tr>
<tr>
<td class="class1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Vestibulum consequat dapibus nibh, ac bibendum turpis ornare ut.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Curabitur bibendum tellus id turpis fermentum eu fermentum sapien ornare.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Donec eu neque tortor, ornare viverra sapien.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Praesent ac leo justo, et tempus turpis.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Vestibulum hendrerit metus et mauris volutpat hendrerit.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Quisque consequat ante sit amet nibh ornare quis gravida sapien consectetur.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Maecenas at turpis ut libero volutpat mattis eget a mi.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Fusce id dui in justo porta gravida eu ac leo.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Vivamus in metus at purus congue lacinia.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Aliquam suscipit mauris vulputate odio adipiscing vitae semper odio varius.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Vestibulum accumsan nunc at magna facilisis gravida.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Pellentesque tempus nisi mauris, quis porttitor purus.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Fusce tincidunt elit condimentum tellus aliquet in ultrices augue aliquam.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Vestibulum faucibus mauris sit amet enim bibendum fringilla.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Nam in purus non sapien cursus mollis.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Proin pulvinar ligula quis nulla varius tincidunt.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Integer faucibus hendrerit velit, nec tempor velit auctor id.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Nunc iaculis ligula pulvinar elit suscipit in volutpat augue dictum.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Pellentesque id augue sit amet nibh luctus eleifend ut non urna.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Suspendisse et neque arcu, ac fermentum ligula.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Mauris sed mauris vitae nisi suscipit bibendum sit amet vitae quam.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Morbi sagittis lorem laoreet velit consectetur ac sodales enim pretium.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Proin mattis urna in velit viverra in vulputate sapien laoreet.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Sed condimentum urna in orci interdum eu consequat felis scelerisque.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Quisque non nunc et purus mattis dictum.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Duis dictum nibh sed leo hendrerit eget dapibus lacus hendrerit.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Nam vel leo justo, sed rhoncus sem.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Ut sed augue sit amet elit scelerisque suscipit.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="class1">Proin vulputate dui et sapien tristique quis placerat nunc accumsan.</td>
<td>Click here</td>
<td>$100 - $300</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
我希望表格适合固定宽度的 div。所有单元格都有nowrap
,所以我希望其中一列(标有.class1
)缩小,以便表格与外部 div 一样宽。我尝试设置overflow: hidden
,但没有奏效。如何在不硬编码任何像素宽度的情况下获得所需的结果?
期望的结果是将所有内容保持在单行上(因此 nowrap),但尽可能多地切掉一列(第一列)以使表格适合固定宽度。