[SalesForce] How set table width dynamically based on list count

I am generating pdf and word documents. In that displaying the tables using html tags. columns will be different for each records. I want to set width of the column based list count. If list have 3 records i want to display 3 coulumns fit in the table width.(means table width is 100%, then column width should be 100/3). If list has 5 records i want to display 4 columns(here column width should be 100/4). max 7 records. How to set the width dynamically..?

Best Answer

You can simple check the number of rows in your controller and calculate width .

for ex:

Integer width = 100 / number_of_records;

And then use this variable in you VF/HTML. for ex.

<table>
    <TH>
        <TD width="{!width}">
        <TD width="{!width}">
        <TD width="{!width}">
    </TH>
    <tr>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Gender</td>
   <tr/>
<table/>