Today's campus recruitment written test requires the implementation of a fixed first row and first column, with the width adaptive to window changes, but the window cannot be reduced to a certain width and a horizontal scroll bar appears... First understand a few concepts: table-layout: The table-layout property has two specific values: position : sticky Sticky positioning can be thought of as a hybrid of relative and fixed positioning. Elements are relatively positioned until they cross a certain threshold, after which they become fixed. accomplish: 1. Adaptive <br /> The table is wrapped in a div with a width of 100%, and a scroll bar appears when it overflows .box { width: 100%; height: 200px; background-color: #eee; overflow:auto; margin: 10px; } Table, width 100%, set a minimum width, I set it to 1000px here, this depends on personal settings table { width: 100%; min-width: 1000px; /* Custom width should be set to fixed */ table-layout: fixed; /* Set cell spacing */ border-spacing:0; } 2. To fix the first row and first column, you need to set sticky positioning on the first row and first column. thead tr th { /* th sets sticky positioning */ background-color: pink; position: sticky; top: 0; /* top border */ border-top: 1px solid black; } First column settings td:first-child { /* td first sticky positioning */ position: sticky; left: 0; background-color: skyblue; } If you need to change the cell width, you need to set table-layout: fixed td:first-child,th:first-child { /* Set the first column to 200. Setting th is effective. Adding td here is mainly for setting Border*/ width: 200px; border-left: 1px solid black; } Another thing to note is the border. You need to set the border of each cell individually. If the border collapses, the scrolling will follow, which will not look good. Overall code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { padding: 0; margin: 0; } .box { width: 100%; height: 200px; background-color: #eee; overflow:auto; margin: 10px; } table { width: 100%; min-width: 1000px; /* Custom width should be set to fixed */ table-layout: fixed; /* Set cell spacing */ border-spacing:0; } td,th { border-bottom: 1px solid black; border-right: 1px solid black; box-sizing: border-box; /* Display if the length exceeds... */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } td:first-child,th:first-child { /* Set the first column to 200, setting th is effective*/ width: 200px; border-left: 1px solid black; } /* If border collapses, scrolling will follow, so set border for each element separately */ td:last-child, th:last-child { border-right: 1px solid black; } th:last-child, td:last-child { border-right: 1px solid black; } .last td { /* Bottom border of the last line */ border-bottom: 1px solid black; } thead tr th { /* th sets sticky positioning */ background-color: pink; position: sticky; top: 0; /* top border */ border-top: 1px solid black; } td:first-child { /* td first sticky positioning */ position: sticky; left: 0; background-color: skyblue; } thead tr th:first-child { /* The first one is set at the top to ensure that it is not covered by up and down and left and right scrolling*/ z-index: 1; left: 0; } </style> </head> <body> <div class="box"> <table> <thead> <tr> <th>Name</th> <th>Student Number</th> <th>Age</th> <th>Results</th> <th>Hobbies</th> </tr> </thead> <tbody> <tr> <td>Coke11111111111111111111111111111111111111111111111111</td> <td>Coke11111111</td> <td>Coke 222222222</td> <td>Coke 33333333333333333333333333</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> <tr class="last"> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> <td>Coke</td> </tr> </tbody> </table> </div> </body> </html> Effect: (Not very good yet, keep working on it) This concludes this article about how to use CSS to fix the first row and first column of a table and implement adaptive window examples. For more information on how to use CSS to fix the first row and first column of a table, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: MySQL Database Basics SQL Window Function Example Analysis Tutorial
>>: Introduction to the process of using NFS remote directory mounting in CentOS environment
Preface A character set is a set of symbols and e...
Table of contents Overview Global hook function R...
The questions encountered in Baidu interviews nee...
During the development activity, I encountered a ...
This article uses examples to describe common bas...
We have introduced how to create a waterfall layo...
To understand what this means, we must first know ...
1. Same IP address, different port numbers Virtua...
Recently, when I was working on my "Football...
An index is a sorted data structure! The fields t...
1. The value used in the button refers to the text...
The author of this article @子木yoyo posted it on hi...
1. Problem The problems encountered when initiali...
Table of contents MySQL delete syntax alias probl...
1. Install JDK Check the computer's operating...