问题
I have a code like this:
main {
position: relative;
width: 80%;
float: right;
height: auto;
}
div.container {
width: 95%;
height: auto;
margin: 0 auto;
}
main div.container>table {
width: 10px;
overflow: hidden;
background-color: red;
}
<main>
<div class="container">
<table>
<thead>
<tr>
<th>Nama event</th>
<th>Kategori event</th>
<th>Keterangan event</th>
<th>Waktu event</th>
<th>Lokasi event</th>
<th>Cover</th>
</tr>
</thead>
<tbody>
<tr>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
</tr>
</tbody>
</table>
</div>
</main>
But the table width doesn't work. When I try the width over than 322px it works, but when I try width < 322px isn't work (I try 10px but the table
isn't become smaller). And I try to width > 33% it's work < 33% it's not work too. Why?
(The main width is 80% is because there is aside
with width 20%
on the left)
回答1:
This is due to the behavior table where you cannot decrease its width less than the needed width for its content.
To be able to change this you need to set table-layout
to fixed as by default it's auto and as you can read here:
Automatic table layout algorithm (this is default):
The column width is set by the widest unbreakable content in the cells Can be slow, since it needs to read through all the content in the table, before determining the final layout
and
Fixed table layout algorithm:
The horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells Allows a browser to lay out the table faster than the automatic table layout The browser can begin to display the table once the first row has been received
main {
position: relative;
width: 80%;
float: right;
height: auto;
}
div.container {
width: 95%;
height: auto;
margin: 0 auto;
}
main div.container>table {
width: 10px;
overflow: hidden;
background-color: red;
}
<main>
<div class="container">
<table>
<thead>
<tr>
<th>Keterangan event</th>
<th>Waktu event</th>
<th>Lokasi event</th>
<th>Lokasi event</th>
<th>Cover</th>
</tr>
</thead>
<tbody>
<tr>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
</tr>
</tbody>
</table>
after adding table-layout:fixed;
<table style="table-layout:fixed;">
<thead>
<tr>
<th>Keterangan event</th>
<th>Waktu event</th>
<th>Lokasi event</th>
<th>Lokasi event</th>
<th>Cover</th>
</tr>
</thead>
<tbody>
<tr>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
</tr>
</tbody>
</table>
</div>
</main>
回答2:
Is this what you need?...you set Table->width : 10px..... i guess you meant td->width: 10px..
.main {
width: 100%;
}
div.container {
width: 100%;
height: auto;
margin: 0 auto;
}
div.container > table {
width: 90%;
overflow: hidden;
background-color: red;
}
<div class="main">
<div class="container">
<table>
<thead>
<tr>
<th>Nama event</th>
<th>Kategori event</th>
<th>Keterangan event</th>
<th>Waktu event</th>
<th>Lokasi event</th>
<th>Cover</th>
</tr>
</thead>
<tbody>
<tr>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
<td>something</td>
</tr>
</tbody>
</table>
</div>
</div>
来源:https://stackoverflow.com/questions/48049965/why-width-property-doesnt-work-on-my-table