使用 CSS 动态调整 html 表的列宽

Dynamic resizing of column width of html tables using CSS

Note: I'm using a rendering engine to render my html to PDF that disables JavaScript by default, so I'm strictly looking for answers using *.css.

在我的报告中,我有一个 table 用于会计目的,有 11 列:

现在假设 table 的宽度需要为 1024px(根据 *.pdf 渲染引擎提供的要求而变化)。关于这一点,我想:

我试图给每列固定宽度(总共 1024px),我设法通过 *.css 使用省略号溢出,但是一些 space 被浪费了,我想避免这些:

你可以做些什么来给出以 % 为单位的宽度,但除非你设置一个最小宽度:以 px 之类的东西,否则这将崩溃。

//CSS
.col1{
  //the width you might need to play with it so it add up to 100% of the container
  width:5%;

  //this value will depend on the max nth number the table will display
  //plus it padding if you want some space form the number to the border
  min-width:20px;   
}
.container{
  width: 1024px;
  margin: auto;
}
#MyTable{
  width: 100%;
}

并对所有其他列执行相同操作!!!!

还有 在 HTML 周围添加一个容器 table

<div class="container">
  <table id="MyTable">
    <caption>table title and/or explanatory text</caption>
        <thead >
            <tr>
                <th id="col1">nth</th>
                <th id="col2">ID number</th>
                <th>Address</th>
                <th>Price 1</th>
                <th>Price 2</th>
                <th>Price 3</th>
                <th>...</th>
                <th>...</th>
                <th>...</th>
                <th>...</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>100</td>
                <td>201532112009</td>
                <td>Avenida Primera/ SOme address</td>
                <td>14.153,00</td>
                <td>0.00</td>
                <td>9.000</td>
                <td>and</td>
                <td>others</td>
                <td>others</td>
                <td>others</td>
            </tr>
        </tbody>
  </table>
</div>