Why are my column headings not the same?

I am setting the same width for the column headers as the data rows. But they refuse to line up.

I made a fiddle here: http://jsfiddle.net/bwdc78tr/

Here is my CSS

html, body {
    height: 100%;
    margin: 0;
    width: 100%
}

#header {
    width: 100%;
    height: 60px;
    background-color: #013499;
    margin: 0;
}

#sidebar {
    background-color: #7690C5;
    bottom: 60px;
    float: left;
    height: calc(100% - 120px);
    top: 60px;
    width: 200px;
}

#content {
    background-color: #F2F2F2;
    bottom: 60px;
    float: left;
    height: calc(100% - 120px);
    top: 60px;
    width: calc(100% - 200px);
}

footer {
    clear: both;
    margin: -60px 0 0 0;
    width: 100%;
    height: 60px;
    background-color: #013499;
}

.buttons {
    float: right;
    margin-right: 20px;
}

#dropDownButton {
    vertical-align: -5px;
}

#WholeNumber {
    width: 135px;
}

#LookupSection {
    color: white;
    height: 60px;
    margin-left: 220px;
}

.WholeNumberCell {
    background-color: white;
    color: #000000;
}

#ImageDataTable {
    border: 1px solid black;
    border-spacing: 0;
    height: 100px;
    padding: 0;
    width: 100%;
}

.ImageDataCell {
    background-color: white;
    border: 1px solid black;
    color: #000000;
}

#WholeNumberDiv {
    margin-left: 100px;
    height: 0;
    overflow: auto;
}

.send_button {
    margin-right: 20px;
    text-align: right;
}

      

The table is dynamically created. I have read several sites on how to create tables, but they all explain basic things. Methods always break when they expand. I can't find anything that explains more advanced things, so if anyone knows any good links please post them.

I think one problem might be that the example I'm using is a sized table ( http://www.imaputz.com/cssStuff/bigFourVersion.html ) and mine is dynamic. Any ideas on how to adapt this?

0


source to share


3 answers


In your table ImageDataTable

you have thead

and then in the first tr

you set the properties display:block

when it should be display: table-row

Next in the code you set tbody id="TmageDataBody"

to display: block

when it should bedisplay: table-row-group;



See the updated Fiddle here

+3


source


A few questions:

  • Your first one is <th>

    closed with </div>

    instead of the obvious. When I see that HTML is giving very unusual and unexpected behavior, I try to look for these problems using the W3 Validator; most of its output is irrelevant to me, but it is at least good at finding mismatched tags. Browsers often do these things in case they are minor HTML errors, but sometimes bad end tags can seriously break the format.
  • You define the width in a block <th>

    , but then you override it with an ancient width

    HTML attribute . If you want the headers to determine the width, set the width only.
  • It might just be a jsfiddle, but the space that was specified in the table prevented it from giving the cells their full intended width. I got this to look ok by setting width: 300%;

    so that it has horizontal scrollbars. You also had a typo on the property "widht"

    .


EDIT: As you pointed out, there seems to be more for this. You also had a property display: block;

set on some elements via CSS. This overrides the default element, which is display: table-cell;

and display: table-row;

, which gives it its constant caption width behavior.

0


source


Typically, you are trying to control your table too much.

First remove the inline styles from the elements thead > tr

and tbody

that set display:block

. You usually don't want this when using tables. It just will align the columns.

Now remove all inline styles that set column widths ... an easier way to use this colgroup

:

<table>
  <colgroup>
    <col style="width: 50%"></col>
    <col style="width: 20%"></col>
    <col style="width: 30%"></col>
  </colgroup>
  <thead>
    <tr>
     <th>Header 1</th>
     <th>Header 2</th>
     <th>Header 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
     <td>Header 1</td>
     <td>Header 2</td>
     <td>Header 3</td>
    </tr>
  </tbody>
</table>

      

Now this leaves a problem that I BELIEVE that you were trying to deal with, from your table data creating a table wider than your free space. You can deal with this in several ways. A few suggestions:

• Place the table in a container to loop through its overflow:

 div.tableContainer {
   overflow-x: auto;
 }

      

• Customize the table to truncate the contents of the cell:

table {
  table-layout: fixed;
}

td, th {
  text-overflow: ellipsis;
  overflow:hidden;
}

      

Generally, your goal should be to have a table with minimal inline CSS.

0


source







All Articles