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.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.