First, you really should start with good, clean, semantic markup. That means a THEAD tag and a TBODY tag. So we'll start with something like the following...
HTML
Next, we need to adjust the CSS to override the default styles.
First, make sure that you have at least some specificity, otherwise all your tables, even those that take up very little vertical space will be affected. You'll notice from line 1 of our HTML that we're doing this by adding
scrollable
as a class on the table.Second, we're adjusting both the TBODY and the TR tags to override the default. We're going to add a rule for overflow (on the y-axis) on the TBODY and for the
height
. A note about the height - the default line-height for a table row is 1.5 em, so if you don't adjust the line-height, using a multiple of 1.5 will give you a full line at the bottom - assuming you don't have borders around the TR or TD. In the example, I'm using a 6em height, which shows 4 full lines.One might think that setting the
height
and overflow-y
on the TBODY is enough, but alas it is not. The display mode is still table
, which overrides the height specification. To correct this, I'll change the display
to block
. Unfortunately, that has the sad side-effect of rendering the cells within the table using an auto width. I'll correct this - in part by setting the display
on the TR to table
- and in part by setting the width
on each of the columns. This gives us the CSS below.CSS
If you would rather have evenly-spaced columns, you can set the
width
on the TR within the TBODY to 100%
and change the table-layout
to fixed
, e.g., change line 8 in the CSS to display:table; table-layout:fixed; width:100%;
and remove lines setting width
on all columns.That's it. Your table is now scrollable with a stationary header.
Country | City | Activity | Name | Latitude | Longitude |
---|---|---|---|---|---|
US | ATL | 68343 | Hartsfield Jackson Atlanta International | 33.636719 | -84.428067 |
US | ORD | 59692 | Chicago O'Hare International | 41.978603 | -87.904842 |
US | DFW | 56496 | Dallas Fort Worth International | 32.896828 | -97.037997 |
US | LAX | 51396 | Los Angeles International | 33.942536 | -118.408075 |
CN | PEK | 48226 | Capital International | 40.080111 | 116.584556 |
US | CLT | 44583 | Charlotte Douglas International | 35.214 | -80.943139 |
US | DEN | 44438 | Denver International | 39.861656 | -104.673178 |
US | LAS | 41164 | McCarran International | 36.080056 | -115.15225 |
US | IAH | 39808 | George Bush Intercontinental | 29.984433 | -95.341442 |
GB | LHR | 37680 | London Heathrow | 51.4775 | -0.461389 |
Happy coding.
No comments:
Post a Comment