티스토리 뷰

As a web developer, creating a user-friendly and responsive website is always my top priority. I believe that designing for the user's perspective is key to creating a successful website that is easy to use and navigate. When it comes to presenting complex data in tables, it can be especially tricky to make them easy to use and read on small screens. When I first received the task to make the tables that would be easy to use and read on mobile devices, I had to do a lot of research. There was the design given by the designer, but I believed there might be the better way to present the table to the users more efficiently. I did some research, created several different designs and presented them to the product managers. In the end, the scrollable table was the best option for presenting complex data in a way that was both user-friendly and optimized for any device. Today, I will talk about how to create a scrollable table using CSS and also Bootstrap 5

 

1. Using CSS

To make a responsive table using CSS, you can start with a basic HTML table structure and then add CSS properties to control its appearance and behavior. One common approach is to set the table's width to 100% and its display property to block. This will make the table fill the entire width of its container and allow it to be responsive to changes in screen size.

 

Next, you can add overflow-x: auto to the table's CSS to enable horizontal scrolling on small screens. This property allows the table to remain the same width while users can scroll left or right to view the entire table.

 

If you have a large table with a lot of data, you may also want to consider adding a fixed header to make the table more user-friendly. You can achieve this by using the CSS position: sticky property on the th elements within the thead section of your table. This will keep the header fixed at the top of the table as users scroll through the data.

 

2. Using Bootstrap 5 
 

Tables

Documentation and examples for opt-in styling of tables (given their prevalent use in JavaScript plugins) with Bootstrap.

getbootstrap.com

Bootstrap 5 provides a built-in class called table-responsive that makes it easy to create a responsive table. To use this class, we can simply add it to the parent element of the table, which is typically a <div> element.

<div class="table-responsive">
  <table class="table">
    <thead></thead>
    <tbody></tbody>
  </table>
</div>

In this example, we wrap the table in a <div> element with the table-responsive class. This tells Bootstrap to make the table scrollable on small screens, so users can easily view the table without having to zoom in or out. You can choose -sm | -md | -lg | -xl depending on your breakpoint. 

 

Creating a responsive table is crucial for providing a positive user experience on mobile devices. Whether you choose to use CSS or a front-end framework like Bootstrap 5, the key is to make the table easily readable and navigable, no matter what screen size the user is using. 

Thank you for being here today, have a wonderful day! :D