Danny Danny - 5 days ago 6
CSS Question

HTML Table - 100% width table, combination of fixed width and UNIFORM fluid columns

I'm trying to build a custom calendar in HTML and Javascript where you can drag and drop tasks from one day to another. I would like to have the first column and last two columns as fixed width and have the remaining columns (Monday through to Friday) fluid so that the table always fills up 100% of it's parent.

The problem that I am having is that the fluid TD's automatically size themselves based on how much content is in them, meaning that when I drag a task from one day to another the columns widths resize. I would like to have Monday to Friday be exactly the same size regardless of content and without setting text-overflow:hidden; (as the tasks always need to be visible)

i.e. I want the gray columns fixed width and the red columns fluid but uniform with each-other regardless of the content within them.


Edit: I'm using jQuery for the drag and drop functionality so a JavaScript solution would also be OK (although not preferable).


JSFiddle Live example

HTML:

<table>
<tr>
<th class="row_header">Row Header</th>
<th class="fluid">Mon</th>
<th class="fluid">Tue</th>
<th class="fluid">Wed</th>
<th class="fluid">Thurs</th>
<th class="fluid">Fri</th>
<th class="weekend">Sat</th>
<th class="weekend">Sun</th>
</tr>
<tr>
<td>Before Lunch</td>
<td>This col will be wider than the others because it has lots of content...</td>
<td></td>
<td></td>
<td></td>
<td>But I would really like them to always be the same size!</td>
<td></td>
<td></td>
</tr>
</table>


CSS:

table {
width: 100%;
}

td, th {
border:1px solid black;
}

th {
font-weight:bold;
background:red;
}

.row_header {
width:50px;
background:#ccc;
}

.weekend {
width:100px;
background:#ccc;
}

.fluid {
???
}

Answer

Using jQuery (could probably be done with regular JS too, but I prefer it for this kind of jobs):

(Note: I gave the table an ID so it'll be easier to select, can be done without it with a bit of tinkering)

    $(function() {
        var $my_table = $("#my_table")
            ,current_width = $my_table.width()
            ,fluid_columns = $("table .fluid")
            ,spread_width = (current_width - 150) / fluid_columns.length;

        fluid_columns.width(spread_width);

        $(window).on("resize", function() {
            current_width = $my_table.width();
            spread_width = (current_width - 150) / fluid_columns.length;
            fluid_columns.width(spread_width);
        })
    });
Comments