2

I have the HTML table & its CSS.

CSS :

#outerDIV {
  position: relative;
  padding-top: 20px;
}
#innerDIV {
  overflow-y: auto;
  height: 500px;
}
table#mytable tbody tr:first-child 
{
    width:100%;
    position :absolute;
}

HTML :

<div class="outerDIV">
  <div class="innerDIV">
    <table id="#mytable"></table>
  </div>
</div>

I need the fixed header tags of table, so is the above code does, but the width of the th tags gets compressed to the left & does not matches the width of the tr's of the body. How to make the tr to 100% width ?

Harshit
  • 5,147
  • 9
  • 46
  • 93

2 Answers2

0

You want the <th> to span the width of multiple columns, but you can't do this with CSS so you need to do it in the HTML (see HTML colspan in CSS).

If you add a colspan attribute to the <th> it should do what you want (see Colspan all columns). If you know the number of columns in the table, use that, e.g. for 5 columns use <th colspan="5">, but you can also use a larger number and it should be ok, e.g. <th colspan="100">.

Some other notes: as others have mentioned, you shouldn't use position on a <tr>. Also, don't include the # in an element id, i.e. it should be

<table id="mytable"></table>
Community
  • 1
  • 1
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
0

NEVER try to use absolute or even relative re-positioning on <tr>, <td> and <th>. It's not supposed to work in tables, and in most cases the results will be nowhere near what you'd expect.

connexo
  • 53,704
  • 14
  • 91
  • 128