2

Here's a simple table code.

Here's is the jsfiddle code for it - JSFiddle Link

The thing is that when the head gets a position : fixed it simply lose all the properties of the table- such as the whole width, each cell width.

So the thing I would be happy to know is if there's any way to fix it?

Thanks for any kind of help

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    
        <style>
            table {
            width: 100%;
            }
    
            th td {
             width:25%;
            }
    
            thead {
            background-color: #f00;
            position: fixed;
            }
    
        </style>
    
    
    </head>
    <body>
    
        <table>
            <thead>
                <tr>
                    <th>1</th>
                    <th>2</th>
                    <th>3</th>
                    <th>4</th>
                </tr>
            </thead>
    
            <tbody>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
    
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                </tr>
    
    
            </tbody>
    
        </table>
    
    
    </body>
    </html>
Aadil Keshwani
  • 1,385
  • 1
  • 18
  • 29
PESfcs
  • 115
  • 9

2 Answers2

1

Please try with the following css

thead > tr> th {width: 330px;}

If the position is fixed it gets auto width, if you set width for the th then it seems to be correct.

Jagadeesh
  • 734
  • 6
  • 26
0

Update css

        table {
        width: 100%;
        }

        th td {
         width:25%;
        }

        thead {
        background-color: #f00;
        position: fixed;
    width: 100%;
          left: 0;
          right: 0;
        }
thead th{width: 2000px}
Ram kumar
  • 3,152
  • 6
  • 32
  • 49