8. Tables

The problem with tables is that they tend to take up a lot of horizontal space, and we can't break them up as the browser width shrinks. That's why the technique is to wrap it in a container with overflow-x: auto. This way we'll have a scroll that will help view it in full without breaking the layout.

.container-table {
  overflow-x: auto;
}
<div class="container-table">
  <table>
    ...
  </table>
</div>

Reduce its width until the horizontal scroll appears. You'll notice that the scroll isn't shown on the table, but on the section that contains it.

Name Age Location Profession Children Married
Juan 45 Valencia Programmer 2 Yes

Now we can have tables that can be viewed on mobile devices without breaking the layout.

In case it is a very long table, we can use position: sticky to lock the headers.

thead {
position: sticky;
top: 0;
}

Scroll in the following example.

Name Age Location Profession
Lorem. Sed? Ut. Architecto?
Lorem. Nihil! Eveniet! Eveniet.
Lorem. Eveniet! In! Non.
Lorem. Vitae? Veniam! Omnis.
Lorem. Omnis. Non? Ea.
Lorem. Asperiores! Atque? Cupiditate?
Lorem. Praesentium. Iure! Placeat.
Lorem. Ipsum! Nostrum! Odio?
Lorem. Autem. Quae. Maxime!
Lorem. Veritatis? Deleniti. Adipisci.
Lorem. Veritatis! Ipsam. Autem.
Lorem. Cumque. Sunt. Debitis?
Lorem. Iste. Tenetur. Maiores?
Lorem. Repellat. Laudantium. Inventore.
Lorem. Fuga. Soluta! Sint.
Lorem. Inventore. Quos? Quis.
Lorem. Praesentium. Iusto. Inventore!
Lorem. Praesentium. Natus! Doloremque.
Lorem. Nostrum? Fugiat! Autem.
Lorem. Modi. Blanditiis. Laborum.
Lorem. Amet! Nulla! Illum?
Lorem. Possimus! Dolorem. Dolorem!
Lorem. Quas. Molestiae. Blanditiis!
Lorem. Dolore. Voluptatem. Nisi?
Lorem. Pariatur. Totam? Quaerat.
Lorem. Vitae! Consequuntur. Atque?
Lorem. Rerum! In! Tempora.
Lorem. Vero! Aperiam? Tempora.
Lorem. Iure! Neque! Delectus?
Lorem. Dicta. Magni. Perferendis.
Lorem. Voluptatum! Veniam. Hic.
Lorem. Asperiores. Optio. Perferendis!
Lorem. Commodi! Reprehenderit? Officia.
Lorem. Voluptatibus! Quasi! Quibusdam.
Lorem. Quisquam? Aut! Quidem.
Lorem. At? Porro. Rerum.
Lorem. Voluptatem? Commodi. Ab?
Lorem. Dolore. Eveniet! Ab.
Lorem. Ab? Vitae! Doloremque.
Lorem. Qui. Vel. Molestiae?
Lorem. Dolorum. Magni. Voluptatum?
Lorem. Voluptatum. Autem. Libero?
Lorem. Neque! Rerum. Aliquam.
Lorem. Non? Labore! Quibusdam?
Lorem. Voluptatem. Consequuntur. Officiis!
Lorem. Dolore. Libero! Tenetur?
Lorem. Doloremque. Molestiae! Deserunt.
Lorem. Asperiores. Recusandae! Unde.
Lorem. Quisquam! Aut. Praesentium!
Lorem. Sed? Aliquam. Quas.
Lorem. Magnam? Voluptas! Cum.
Lorem. Iste? Maxime? Facilis.
Lorem. Rerum! Eligendi! Aperiam!
Lorem. Sint. Nihil. Deserunt.
Lorem. Saepe. Obcaecati. Similique?
Lorem. Similique! Dignissimos! Nemo?
Lorem. Animi? Debitis? Aliquid!
Lorem. Molestias. Perferendis. Temporibus.
Lorem. Repellendus! Sed. Necessitatibus?
Lorem. Provident. Odit? Officiis.

These aren't the only ways to improve the user experience with tables, but they are the cheap ones. I also recommend that you weigh the possibility of creating charts instead of the table, since they are easier to read and understand.

Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.

Buy the book

Help me keep writing

Every coffee gives me a push toward the next article.

Comments

There are no comments yet.