简体   繁体   English

CSS宽度在我的桌子上不起作用

[英]Css width doesn't work on my table

I have a code like this: 我有这样的代码:

<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>

CSS CSS

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;
}

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). 当我尝试超过322px的宽度时,它可以工作,但是当我尝试<322px的宽度不起作用时(我尝试10px,但table却不会变小)。 And I try to width > 33% it's work < 33% it's not work too. 我尝试将宽度> 33%设为有效,将<33%设为无效。 Why? 为什么? (The main width is 80% is because there is aside with width 20% on the left) (主要的宽度为80%是因为, asidewidth 20%左侧)

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 : 为了能够更改此设置,您需要将table-layout设置为固定,因为默认情况下它是自动的,您可以在此处阅读:

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> 

Is this what you need?...you set Table->width : 10px..... i guess you meant td->width: 10px.. 这是您需要的吗?...您设置Table-> width:10px .....我想您是说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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM