1

I am dynamically creating a HTML table. Number of rows and columns are not fixed. I am using JQuery datatable to extend my html table.Problem is width of the columns, it always take the auto width (I guess dividing the sScrollX width among the number of columns). How can I set the width of my columns? CSS has no effect on column width.

As seen in the image below columns are wider than expected

enter image description here

3
  • Do the cols have a class? Commented Feb 20, 2014 at 15:23
  • I see the columns go off the page, do you have width 100% on the table and do the current td have a fixed width on them? Commented Feb 20, 2014 at 15:42
  • Try this jsfiddle.net/HKW43 Commented Feb 20, 2014 at 15:51

1 Answer 1

0

You could do something like this

HTML

<table>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>   
</table>

jQuery:

$(function(){
    winW = $(window).width();       
    firstRow = $('table tr:eq(0) td').length;
    $('table tr td').css({width:winW/firstRow+'px'});
});

Example => http://jsfiddle.net/HKW43/

For update width on window resize see => http://jsfiddle.net/HKW43/2/

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure about "height"??
Please see the attached image.Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.