Gideon
Gideon

Reputation: 1517

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.

In my report I have a table used for accounting purposes with 11 columns:

Now let's say the table's width needs to be 1024px (varies depending on the requirement provided by the *.pdf rendering engine). On that point, I want to:

I tried to give each columns fixed width (with a total of 1024px), and I manage to use the ellipsis overflow through *.css but some space are wasted and I want to avoid these:

Upvotes: 1

Views: 8191

Answers (1)

T04435
T04435

Reputation: 13992

What you could do it to give the width in %, but that will crash unless you set a min-width: in px something like.

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

and do the same with all the other columns!!!!

also in the HTML add a container around the 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>

Upvotes: 1

Related Questions