Why is my HTML table not respecting my CSS column

2019-01-18 01:12发布

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 css Table
9条回答
女痞
2楼-- · 2019-01-18 01:44

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.

查看更多
劫难
3楼-- · 2019-01-18 01:46

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 ! :)

查看更多
你好瞎i
4楼-- · 2019-01-18 01:47

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!

查看更多
看我几分像从前
5楼-- · 2019-01-18 01:47

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

查看更多
别忘想泡老子
6楼-- · 2019-01-18 01:52

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;
}
查看更多
不美不萌又怎样
7楼-- · 2019-01-18 01:55

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.

查看更多
登录 后发表回答