🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
When working with large tables, making the table scrollable helps keep the data organized and easy to view, especially when dealing with a lot of rows or columns. In this tutorial, you’ll learn how to make a table scrollable using CSS while maintaining a clean and readable design.
Development Steps
- Create Basic HTML Table Structure: Set up a simple table with column headers and data rows.
- Style the Table and Make it Scrollable: Use CSS to make the table scrollable when its content exceeds a certain height or width.
- Add Optional Fixed Header: Make the header stay in place while the table content scrolls.
Step 1: Create a Basic HTML Table Structure
We’ll start by creating a simple HTML table structure with headers and data rows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scrollable Table</title>
</head>
<body>
<!-- Step 1: Basic table structure -->
<div class="table-container">
<table class="scrollable-table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ananya</td>
<td>25</td>
<td>Delhi</td>
<td>ananya@example.com</td>
</tr>
<tr>
<td>Rohan</td>
<td>30</td>
<td>Mumbai</td>
<td>rohan@example.com</td>
</tr>
<tr>
<td>Meera</td>
<td>22</td>
<td>Bangalore</td>
<td>meera@example.com</td>
</tr>
<tr>
<td>Vikram</td>
<td>35</td>
<td>Pune</td>
<td>vikram@example.com</td>
</tr>
<tr>
<td>Kiran</td>
<td>28</td>
<td>Hyderabad</td>
<td>kiran@example.com</td>
</tr>
<!-- Additional rows for scroll demonstration -->
<tr>
<td>Rahul</td>
<td>32</td>
<td>Chennai</td>
<td>rahul@example.com</td>
</tr>
<tr>
<td>Aditi</td>
<td>26</td>
<td>Kolkata</td>
<td>aditi@example.com</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Explanation:
- This table has a
theadfor column headers (Name,Age,City,Email) and several rows of data in thetbody. - We’ll now use CSS to make the table scrollable.
Step 2: Style the Table and Make it Scrollable with CSS
Now, we’ll apply CSS to make the table scrollable by limiting the height of the table container and adding scroll bars for overflow.
<head>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
max-height: 300px; /* Limit height for scrollable content */
overflow-y: auto; /* Enable vertical scrolling */
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
/* Step 2: Style the table */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
Explanation:
- Scrollable Table: The
max-height: 300pxproperty sets the maximum height of the.table-containerto 300px, and theoverflow-y: autoproperty allows vertical scrolling when the content exceeds the container’s height. - Basic Table Styling: The table cells (
thandtd) are styled with padding, borders, and zebra stripes for better readability.
Step 3: Add Horizontal Scrolling (Optional)
If your table has many columns, you can also make it horizontally scrollable by adding overflow-x: auto to the table container.
<head>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
max-height: 300px; /* Limit height for scrollable content */
overflow-y: auto; /* Enable vertical scrolling */
overflow-x: auto; /* Enable horizontal scrolling (optional) */
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
/* Style the table */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
Explanation:
- Horizontal Scrolling: The
overflow-x: autoproperty enables horizontal scrolling, making it useful for tables with many columns that don’t fit within the screen’s width.
Step 4: Make the Table Header Fixed (Optional)
To make the header stay in place while scrolling the table, we can fix the header using position: sticky and top: 0.
<head>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
max-height: 300px; /* Limit height for scrollable content */
overflow-y: auto; /* Enable vertical scrolling */
overflow-x: auto; /* Enable horizontal scrolling (optional) */
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
/* Style the table */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
position: sticky; /* Fix header */
top: 0; /* Stick header to top */
z-index: 1; /* Ensure header stays above other rows */
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
Explanation:
- Fixed Header: The
position: stickyandtop: 0properties make the header stick to the top when the table is scrolled. Thez-index: 1ensures that the header stays on top of the content when scrolling.
Complete Example
Here’s the complete HTML and CSS code for making a scrollable table with an optional fixed header:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scrollable Table</title>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
max-height: 300px; /* Limit height for scrollable content */
overflow-y: auto; /* Enable vertical scrolling */
overflow-x: auto; /* Enable horizontal scrolling (optional) */
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
/* Style the table */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
position: sticky; /* Fix header */
top: 0; /* Stick header to top */
z-index: 1; /* Ensure header stays above other rows */
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<!-- Step 1: Basic table structure -->
<div class="table-container">
<table class="scrollable-table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ananya</td>
<td>25</td>
<td>Delhi</td>
<td>ananya@example.com</td>
</tr>
<tr>
<td>Rohan</td>
<td>30</td>
<td>Mumbai</td>
<td>rohan@example.com</td>
</tr>
<tr>
<td>Meera</td>
<td>22</td>
<td>Bangalore</td>
<td>meera@example.com</td>
</tr>
<tr>
<td>Vikram</td>
<td>35</td>
<td>Pune</td>
<td>vikram@example.com</td>
</tr>
<tr>
<td>Kiran</td>
<td>28</td>
<td>Hyderabad</td>
<td>kiran@example.com</td>
</tr>
<tr>
<td>Rahul</td>
<td>32</td>
<td>Chennai</td>
<td>rahul@example.com</td>
</tr>
<tr>
<td>Aditi</td>
<td>26</td>
<td>Kolkata</td>
<td>aditi@example.com</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Output
Conclusion
In this tutorial, you learned how to make a table scrollable using CSS. By setting a maximum height and using overflow-y: auto, you can enable vertical scrolling for tables with a lot of rows. Additionally, we covered how to make the header fixed using position: sticky and ensure horizontal scrolling for tables with many columns using overflow-x: auto. These techniques improve the user experience, especially when dealing with large datasets.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment