Html table that scrolls

Making Tables Scrollable in CSS

Because HTML tables are set to display: table by default, adding scrollbars to them is a bit nonintuitive.

We can set our tables to display: block and modify their overflow from there, but I’ve found wrapping tables in containers to be more adaptable and flexible.

Vertical table scrollbars

First, let’s set up vertical scrolling.

div class="tableContainer"> table class="table"> . table> div> 

We can set a height for the table’s container and modify the table such that it will fit snug within the container:

.tableContainer < height: 300px; overflow: hidden; > .table < position: sticky; top: 0; width: 100%; > 

In this example, I set the height to 300px — but you can set it to whatever you want!

Our table will now have an inset vertical scrollbar and will expand no further than the height we set.

Horizontal table scrollbars

If you want to implement horizontal scrolling, there is an approach very similar to the vertical scrolling example:

.tableContainer < overflow: hidden; width: 800px; > .table < position: sticky; top: 0; width: 100%; > 

I set the width here to 800px , but again, you can change it to whatever you want.

If you want both horizontal and vertical scrollbars for your table, simply specify both a height and a width in .tableContainer .

And that’s how you can make your tables scrollable with just a bit of CSS!

I refer back to this snippet all the time, and I hope you found it useful too.

Источник

How to make an HTML table scrollable (vertically & horizontally)?

Vertically and horizontally scrollable html table

In this post, I will show you how to make an HTML table vertically scrollable. You’ll also learn how to make the table header sticky when scrolling. End of this post, you’ll see how to make the table horizontally scrollable.

Last but not least, those tables will be mobile-responsive.

Before you get started, you can check the demo of the finished products in the link below.

How to make a table vertically scrollable? (Example 1)

You can use any HTML markup for the table. However, in my HTML, I have a table header ( ) and table body ( ) as you see below.

HTML

 
Column 1 Column 2 Column 3 Column 4
Apple Bicycle Carrot Dog

Not to mention, the table works well without the table body ( ) tag. However, in this context, you need it because I wrote some CSS based on it.

Читайте также:  Выполнить javascript в url

Also, as you see in the above HTML, I wrapped the entire table within a container ( .container .example-1 ). It’s not mandatory to make the table scrollable but I have some basic CSS based on the wrapper such as padding, background color, etc.

CSS

/* general styles for the table */ table < border-spacing: 0; border-collapse: collapse; margin: 0 auto; width: 100%; max-width: 900px; text-align: left; >table tr < -webkit-transition: background-color 0.3s ease-in-out; transition: background-color 0.3s ease-in-out; >table tr th < background-color: #cce6ff; >table tr:nth-child(even) < background-color: #edf2fa; >table tr:hover < background-color: #d9dadb; >table tr td, table tr th < border: 1px solid #ff0000; padding: 12px 8px; >/* general styles ended above */ /* adding padding & background color to the container div */ .example-1 < padding: 90px 15px; background-color: #fff1f0; >.example-1 table < width: 100%; max-height: 370px; overflow-y: scroll; /* makes the table vertically scrollable */ overflow-x: auto; /* it adds a horizontal scrollbar on mobile and when needed */ display: block; >.example-1 table tbody < display: block; >.example-1 table tr < display: grid; /* the line below makes equal width 4 columns where the minimum width is 150px and max width is 1 fraction */ grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) minmax(150px, 1fr) minmax(150px, 1fr); border-bottom: 1px solid transparent; width: max-content; /* it helps the background color go upto the end. otherwise, background color will stop after 100% viewport width */ width: -webkit-max-content; width: -moz-max-content; >@media (min-width: 768px) < .example-1 table tr < /* offset the max-content on large device. otherwise, the table will shrink if you have less content */ width: 100%; >> .example-1 table tr td, .example-1 table tr th < border-right: 1px solid transparent; >.example-1 table tr td:last-child, .example-1 table tr th:last-child

I wrote the necessary comments on the above CSS so you can understand which line does what specific thing. But if you still have any confusion, feel free to leave a comment.

vertically scrollable html table that shows a scrollbar on the right side

Anyways, after you save your HTML & CSS, your table will look like the screenshot below.

As I mentioned about the mobile responsiveness, the above screenshot does not show any horizontal scrollbar but it will when necessary (especially on mobile). If you need to check the responsiveness, please check this demo once again (from mobile).

In my demo, the table is four columns but in your case, it could be different. Note that you have to adjust the grid-template-columns according to your number of columns.

For example, if you have 3 columns, it will be like this:

grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) minmax(150px, 1fr);

How to make the table header sticky when scrolling? (Example 2)

In this example, I will slightly change the HTML markup. I will wrap the table header within the table head ( ). So I can easily target the table header and make it sticky. Otherwise, the table markup will remain the same.

HTML

 
Column 1 Column 2 Column 3 Column 4
Apple Bicycle Carrot Dog

I wrote a comment in the HTML to indicate where exactly I made the changes. See my CSS below.

CSS

I used the exact same CSS as the first example. Just wrote a few new lines of CSS for the sticky behavior.

/* in addition to the above css (example 1) */ table thead < position: sticky; top: 0; left: 0; right: 0; display: -webkit-box; display: -ms-flexbox; display: flex; /* preventing the table head from shrinking */ overflow-x: auto; >

After you save the changes, your table will look like this demo.

How to make a table horizontally scrollable?

To create a horizontal scrollbar, wrap the entire table within a container and write CSS based on it. Let’s see what I am referring to.

HTML

  
Column 1 Column 2 Column 3 Column 4 Apple Bicycle Carrot Dog /* other table reows go here */

CSS

.table-x-scrollbar < overflow-x: auto; >.table-x-scrollbar table

With these HTML & CSS in place, your table will look like this demo.

In this example, I used overflow-x: auto for the horizontal scrollbar. So the scrollbar will appear when necessary. But if you want to show it always, try this line instead overflow-x: scroll

GitHub repository for this entire project/post

How to download a GitHub repository

If you messed up your code or did not get the expected output from the source code that I provided, please refer to this project on GitHub. After downloading it, you’ll have the exact same template as the live preview. It also contains all the demo data, code, and everything in between that you saw in the live preview.

If you’re familiar with Git, you can clone it directly from your terminal using the following command: git clone https://github.com/shihabiiuc/table-scrollable.git

And this brings me to the end of this post.

Learn more about tables

Learn more about the scrollbar

Conclusion

I did not only give you the source code but also showed you the live previews. I gave you multiple examples and explained the CSS. If you did not get the expected output, you can download the entire project from my GitHub repository.

Do you need to hire a web developer?

Shihab, headshot

About Shihab

With over 20K monthly readers, my goal is to help small businesses to create a stunning online presence.

At the same time, I’ve been creating resources for web developers, designers & freelancers in general.

Источник

How to Create a Table with a Fixed Header and Scrollable Body

In this tutorial, find some methods of creating an HTML table, which has a fixed header and scrollable body. Of course, you need to use CSS.

It is possible to achieve such a result by setting the position property to “sticky” and specifying 0 as a value of the top property for the element.

As usual, you can open the Try it Yourself demo link and play with the properties to understand them better.

You can also read the comments in front of each line to understand the properties better.

Example of creating a table with a scrollable body by using the position property:

html> html> head> title>Title of the document title> style> .tableFixHead < overflow-y: auto; /* make the table scrollable if height is more than 200 px */ height: 200px; /* gives an initial height of 200px to the table */ > .tableFixHead thead th < position: sticky; /* make the table heads sticky */ top: 0px; /* table head will be placed from the top of the table and sticks to it */ > table < border-collapse: collapse; /* make the table borders collapse to each other */ width: 100%; > th, td < padding: 8px 16px; border: 1px solid #ccc; > th < background: #eee; > style> head> body> div class="tableFixHead"> table> thead> tr> th>Col 1 th> th>Col 2 th> tr> thead> tbody> tr> td>1.1 td> td>2.1 td> tr> tr> td>1.2 td> td>2.2 td> tr> tr> td>1.3 td> td>2.3 td> tr> tr> td>1.4 td> td>2.4 td> tr> tr> td>1.5 td> td>2.5 td> tr> tr> td>1.6 td> td>2.5 td> tr> tr> td>1.7 td> td>2.5 td> tr> tr> td>1.8 td> td>2.5 td> tr> tbody> table> div> body> html>

Result

Great! Huh? But let’s face it! I don’t like to see that scrollbar starting from the head section of the table!

So, let’s continue to the next example and fix that issue together!

Thers’s another method of creating a table with a fixed header and scrollable body. In the example below, we set the display to “block” for the element so that it’s possible to apply the height and overflow properties.

Example of creating a table with a scrollable body by using the display property:

html> html> head> title>Title of the document title> style> .fixed_header < width: 400px; table-layout: fixed; border-collapse: collapse; > .fixed_header tbody < display: block; width: 100%; overflow: auto; height: 100px; > .fixed_header thead tr < display: block; > .fixed_header thead < background: black; color: #fff; > .fixed_header th, .fixed_header td < padding: 5px; text-align: left; width: 200px; > style> head> body> table class="fixed_header"> thead> tr> th>Col 1 th> th>Col 2 th> th>Col 3 th> th>Col 4 th> th>Col 5 th> tr> thead> tbody> tr> td>1 td> td>2 td> td>3 td> td>4 td> td>5 td> tr> tr> td>6 td> td>7 td> td>8 td> td>9 td> td>10 td> tr> tr> td>11 td> td>12 td> td>13 td> td>14 td> td>15 td> tr> tr> td>16 td> td>17 td> td>18 td> td>19 td> td>20 td> tr> tr> td>21 td> td>22 td> td>23 td> td>24 td> td>25 td> tr> tbody> table> body> html>

As mentioned before, we used overflow: auto on the tbody along with the display: block .

Here’s our result, and enjoy the difference!

Col 1 Col 2 Col 3 Col 4 Col 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

As you have noticed, we didn’t use borders in the previous examples. However, if you need to add borders, you can simply use border property on all td tags.

.fixed_header td < border: 1px solid #797878; >
Col 1 Col 2 Col 3 Col 4 Col 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Источник

Оцените статью