Why is my HTML table not respecting my CSS column width?

HtmlCssHtml Table

Html Problem Overview


I have a HTML table and I want the first few columns to be quite long. I am doing this in CSS:

td.longColumn
{ 
     width: 300px;
}

and here is a simplified version of my table

<table>
  <tr>
   <td class='longColumn'></td>
   <td class='longColumn'></td>
   <td class='longColumn'></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   [ . . and a bunch more columns . . .]
  </tr>
</table>

For some reason the table seems to make this column < 300px when there are a lot of columns. I basically want it to keep that width no matter what (and just increase the horizontal scroll bar).

The container that the table is inside, doesn't have any type of max width so I can't figure out why it's squeezing this column down as opposed to respecting this width.

Is there anyway around this so no matter what, this column will stay a certain width?
Here is the CSS of the outer container div:

#main
{
    margin: 22px 0 0 0;
    padding: 30px 30px 15px 30px;
    border: solid 1px #AAAAAA;
    background-color: #fff;
    margin-bottom: 30px;
    margin-left: 10px;
    _height: 1px; /* only IE6 applies CSS properties starting with an underscrore */
    float: left;
    /*width: 1020px;*/
    min-width:1020px;
    display: block;
    overflow: visible;
    z-index: 0;
}

Html Solutions


Solution 1 - Html

You may get more luck with setting widths for your table cells if you apply the rule table-layout: fixed to the table - this has helped me with a lot of cell-sizing issues when using tables. I would not recommend switching to using just DIVs to arrange your content if it fits the purpose of tables - to display multidimensional data.

Solution 2 - Html

Giving it both max-width and min-width attributes should work.

Solution 3 - Html

I agree with Hristo but there are some cases where table need to be used and solution to your table problem is adding below class to the table and then changing any td width as per your need.

.tables{ border-collapse:collapse; table-layout:fixed;}

I hope this helps for someone who is looking for table solution!

Solution 4 - Html

I had the same problem with a bunch of columns where I wanted spacers columns. I used to do:

<td style='width: 10px;'>&nbsp;</td>

But when the table was wider than window, the spacers were not really 10px, but maybe 5px. And using only DIVs without a TABLE was not an option in my case. So I tried:

<td><div style='width: 10px;'></div></td>

And it worked very well ! :)

Solution 5 - Html

The best way to set your column widths (td's) is to use a table header (th's). Table headers will set the width on your td's automatically. You just have to make sure that your columns inside your thead are the same number of columns in your tbody.

Check it out here: http://jsfiddle.net/tKAj8/

HTML

<table>
    <thead>
        <tr>
            <th class="short-column">Short Column</th> <!-- th sets the width -->
            <th class="short-column">Short Column</th> <!-- th sets the width -->
            <th class="long-column">Long Column</th> <!-- th sets the width -->
        </tr>
    </thead>
    
    <tbody>
        <tr>
            <td class="lite-gray">Short Column</td> <!-- td inherits th width -->
            <td class="lite-gray">Short Column</td> <!-- td inherits th width -->
            <td class="gray">Long Column</td> <!-- td inherits th width -->
        </tr>
    </tbody>
</table>

CSS

table { table-layout: fixed; border-collapse: collapse; border-spacing: 0; width: 100%; }

.short-column { background: yellow; width: 15%; }
.long-column { background: lime; width: 70%; }

.lite-gray { background: #f2f2f2; }
.gray { background: #cccccc; }

Solution 6 - Html

I had issues with not being able to size columns in a table-layout: fixed table that was using a colspan. For the benefit of anyone experiencing a variant of that issue where the suggestion above doesn't work, colgroup worked for me (variation on OP's code):

div {
    margin: 22px 0 0 0;
    padding: 30px 30px 15px 30px;
    border: solid 1px #AAAAAA;
    background-color: #fff;
    margin-bottom: 30px;
    margin-left: 10px;
    _height: 1px; /* only IE6 applies CSS properties starting with an underscrore */
    float: left;
    /*width: 1020px;*/
    min-width:1020px;
    display: block;
    overflow: visible;
    z-index: 0;
}
td.longColumn { 
     width: 300px;
}
table {
    border: 1px solid; 
    text-align: center;
    width: 100%;
}
td, tr {
  border: 1px solid; 
}

<div>
    <table>
        <colgroup>
            <col class='longColumn' />
            <col class='longColumn' />
            <col class='longColumn' />
            <col/>
            <col/>
            <col/>
            <col/>
        </colgroup>
        <tbody>
        <tr>
            <td colspan="7">Stuff</td>
        </tr>
        <tr>
            <td>Long Column</td>
            <td>Long Column</td>
            <td>Long Column</td>
            <td>Short</td>
            <td>Short</td>
            <td>Short</td>
            <td>Short</td>
        </tr>
        </tbody>
    </table>
</div>

Solution 7 - Html

Use table-layout property and the "fixed" value on your table.

table {
    table-layout: fixed;
    width: 300px; /* your desired width */
}

After setting up the entire width of the table, you can now setup the width in % of the td's.

td:nth-child(1), td:nth-child(2) {
    width: 15%;
}

You can learn more about in on this link: http://www.w3schools.com/cssref/pr_tab_table-layout.asp

Solution 8 - Html

For those that are having Table Cell/Column width problems and table-layout: fixed did not help.

When applying fixed widths to table cells (<td> or <th>), do not assign a width to all of the cells. There should be at least one cell with an (auto) width. This cell will act as a filler for the remaining space of the table.

e.g.

<table>
  <thead>
    <tr>
      <th style="width: 150">Assigned 150 width to Table Header Cell</th>
      <th style="width: 100">Assigned 100 width to Table Header Cell</th>
      <th>No width assigned</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="width: 150">Assigned 150 width to Table Body Cell</td>
      <td style="width: 100">Assigned 100 width to Table Body Cell</td>
      <td>No width assigned</td>
    </tr>
  </tbody>
</table>

P.S. you can use style classes here, you don't need to use an in-line style.

Solution 9 - Html

Can't modify <td> width; that is, column width isn't settable. You can add the styling white-space:nowrap; which might help. Or you can add &nbsp;s to add space to columns.

Maybe you could set col width the HTML way: <td width="70%">January>/td>

Unfortunately, in HTML 4.01 and later, that way isn't valid.

Solution 10 - Html

How about something like this...

http://jsfiddle.net/qabwb/1/

HTML

<div id="wrapper">
    <div class="row">
        <div class="column first longColumn">stuff</div>
        <div class="column longColumn">more stuff</div>
        <div class="column">foo</div>
        <div class="column">jsfiddle</div>
    </div>
    <div class="row">
        <div class="column first longColumn">stuff</div>
        <div class="column longColumn">more stuff</div>
        <div class="column">foo</div>
        <div class="column">jsfiddle</div>
    </div>
    <div class="row">
        <div class="column first longColumn">stuff</div>
        <div class="column longColumn">more stuff</div>
        <div class="column">foo</div>
        <div class="column">jsfiddle</div>
    </div>
</div>

CSS

#wrapper {
    min-width: 450px;
    height: auto;
    border: 1px solid lime; 
}

.row {
    padding: 4px;   
}
.column {
    border: 1px solid orange;
    border-left: none;
    padding: 4px;
    display: table-cell;
}

.first { 
    border-left: 1px solid orange;
}

.longColumn {
    min-width: 150px;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleoraView Question on Stackoverflow
Solution 1 - HtmlaphaxView Answer on Stackoverflow
Solution 2 - HtmlumeshView Answer on Stackoverflow
Solution 3 - HtmlSagar RanpiseView Answer on Stackoverflow
Solution 4 - HtmlMatt RoyView Answer on Stackoverflow
Solution 5 - HtmlOneezyView Answer on Stackoverflow
Solution 6 - HtmlAllisonView Answer on Stackoverflow
Solution 7 - HtmlCyan BaltazarView Answer on Stackoverflow
Solution 8 - HtmlJo E.View Answer on Stackoverflow
Solution 9 - HtmlPete WilsonView Answer on Stackoverflow
Solution 10 - HtmlHristoView Answer on Stackoverflow