Signup/Sign In
LAST UPDATED: JUNE 9, 2023

How to make Table Horizontally Scrollable for Small Screen size

Technology #css#html

    In the era of responsive web design, ensuring optimal user experiences across various devices is crucial. However, presenting large tables on small screens can be challenging, often leading to content overflow and a compromised user interface. Fortunately, there's a solution: making tables horizontally scrollable.

    By enabling horizontal scrolling for tables on small screen sizes, we can provide users with a seamless and intuitive way to explore tabular data without sacrificing readability or functionality. In this article, we will dive into the techniques and best practices for creating horizontally scrollable tables, ensuring a delightful user experience on every screen size.

    With the growing use of websites on Mobile screens, the need for solving issues related to the responsiveness of HTML components has increased. One such component is the HTML table.

    HTML responsive table

    Generally, HTML tables do not break UI in the case of a desktop but because the screen size of mobile devices is small, and if you have a table that has more than 4-5 columns then most likely the table will extend out of the UI and you will see a horizontal scroll bar, which is not very good as far as user experience is concerned.

    There are two ways to solve this:

    1. By creating a container to keep the table.

    2. Making the table horizontally scrollable.

    How to make Table Horizontally Scrollable for Small Screen size

    Keep the table inside a div Container

    We can enclose the table in a <div> tag and provide the following styling to the <div> tag:

    .table-container { 
        width: 100% !important;
        overflow-x: scroll; 
    }

    To make it even better, we can even set a width for column tables:

    th, td { min-width: 200px; }

    It is not necessary to have this style for evry screen size and you can only add this for screen size smaller than say 556px or more, using the @media query in CSS.

    @media (max-width: 600px) {
    
        .table-container { 
            width: 100% !important;
            overflow-x: scroll; 
        }
    
        th, td {min-width: 200px; }
    }

    Making the table horizontally scrollable

    This is a better solution and we have implemented this in our website too. With this solution, you do not have to enclose the table inside any other tag, as this style rule is applied directly to the table tag.

    table {
        display: block;
        max-width: -moz-fit-content;
        max-width: fit-content;
        margin: 0 auto;
        overflow-x: auto;
        white-space: nowrap;
    }

    Just like we used the @media query above, for this CSS styling too, we can use the @media query to apply these CSS style rules only for small screen sizes.

    @media (max-width: 600px) {
        table {
          display: block;
          max-width: -moz-fit-content;
          max-width: fit-content;
          margin: 0 auto;
          overflow-x: auto;
          white-space: nowrap;
        }
    }

    Conclusion:

    Tables are a powerful means of organizing and presenting data, but they can pose challenges when it comes to small screen sizes. By implementing horizontal scrolling for tables, we can optimize the user experience and ensure that tabular data remains accessible and easy to navigate on any device.

    Through CSS and JavaScript techniques, we can enable fluid horizontal scrolling, allowing users to explore the complete table content effortlessly. As we strive for responsive and user-friendly designs, mastering the art of creating horizontally scrollable tables becomes a valuable skill for web developers. So, incorporate these techniques into your projects, and unlock the full potential of tables on small screens.

    Hope these two solutions help you in making your HTML table responsive and improve the user experience on your website. If you face any issues, please share them with us in the comment section below.

    Frequently Asked Questions(FAQs)

    1. Why do tables become problematic on small screens?

    Tables can become problematic on small screens because they are often wider than the available viewport width. This can result in content overflow, requiring users to scroll horizontally to view the entire table, which can be inconvenient and hinder the overall user experience.

    2. How can I make a table horizontally scrollable?

    You can make a table horizontally scrollable by wrapping it in a container element with a fixed width and setting the CSS overflow property to "auto" or "scroll." This allows the table to exceed the container's width and enables users to scroll horizontally to view the hidden content.

    3. Should I use CSS or JavaScript to make tables scrollable?

    CSS alone can be used to make tables scrollable. However, if you require more advanced functionality, such as dynamic resizing or touch gestures, JavaScript can be utilized to enhance the scrolling behavior.

    4. Are there any best practices for designing scrollable tables?

    Yes, there are several best practices for designing scrollable tables. It's essential to ensure that the table headers remain visible as users scroll horizontally. Additionally, optimizing the table for touch devices, considering performance implications, and testing the scrolling behavior on different screen sizes are all crucial steps.

    5. Can I apply custom styling to a scrollable table?

    Absolutely! You can apply custom styling to a scrollable table using CSS. You have full control over the table's appearance, including fonts, colors, borders, and backgrounds. Custom styling allows you to match the table design to your overall website or application aesthetics.

    You may also like:

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS