Explain Codes LogoExplain Codes Logo

Table with 100% width with equal size columns

html
responsive-design
css-grid
flexbox
Alex KataevbyAlex Kataev·Nov 4, 2024
TLDR

To build an equally spaced table assign table-layout: fixed; to your <table>. Set each <th> or <td> to a fixed percentage. For a three-column layout, you'll want to use 33.33%.

<style> /* Secure your table at 100% width */ table { width: 100%; table-layout: fixed; } /* Your table columns are equally sized now. Yay! */ th, td { width: 33.33%; } </style> <table> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> <!-- Other Table Rows --> </table>

This approach enforces equal column width regardless of content. Adjust the width percentage for additional or fewer columns.

Responsiveness for various screen sizes

Responsive design is a non-negotiable thing in web development. Your table columns should maintain their equal width regardless of device size. Achieve this by setting width percentages and utilizing media queries to dynamically adjust the width and number of columns based on the user's screen size.

/* For mobile screens */ @media (max-width: 600px) { th, td { width: 50%; } /* Two columns layout */ } /* For tablet screens */ @media (min-width: 601px) and (max-width: 1024px) { th, td { width: 33.33%; } /* Three columns layout */ } /* For desktop screens */ @media (min-width: 1025px) { th, td { width: 25%; } /* Four columns layout */ }

Overflow control & column width consistency

Content overflowing from a cell can disrupt the harmony of your fixed table layout. Make use of CSS properties overflow: hidden; and text-overflow: ellipsis; to ensure that particular contents don't cause your table column to expand beyond the set size. It's like a very polite way of saying, "Hey, you got content out of line! Please go back in line!"

th, td { width: 25%; /* Hey content, stay within your border. We’re maintaining order here! */ overflow: hidden; /* Make overflowed content look pretty by adding ... */ text-overflow: ellipsis; }

Grid & Flexbox: The superheroes of layout

Tables are good. They've been serving the web since its inception. But sometimes, CSS Grid and Flexbox can be better solutions for ensuring equal-width column layouts, particularly when dealing with non-tabular content. This is where you get out of the "Tables R Us" store and enter the "Layout Flex" market.

Here's a taste of Flexbox magic:

<style> /* Flex-biles unit! */ .flex-container { display: flex; } /* Space: "I need space." Columns: "Worry not! Here's some!" */ .flex-container > div { flex: 1; /* Distributes leftover space equally */ } </style> <div class="flex-container"> <div>Content 1</div> <div>Content 2</div> <div>Content 3</div> </div>

And the ever-graceful Grid move:

<style> /* Grid-ception, was it? */ .grid-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /* Creates as many 250px wide columns as possible */ } </style> <div class="grid-container"> <div>Content 1</div> <div>Content 2</div> <div>Content 3</div> </div>

Other important points to consider

Using SCSS and rem units

If you're working with SCSS, you can use variables and calculations for simpler and cleaner markup. Also, rem units become incredibly handy when you're aiming for scalable and accessible font sizing across different screen sizes.

/* Enter the number of columns you desire */ $column-count: 4; table { width: 100%; table-layout: fixed; th, td { /* Column width calculation in check, boss! */ width: ((100% / $column-count) * 1%); } }

Optimizing for user-experience

Using max-width can limit the width of cells on smaller screens. This ensures better readability and usability. Uniform stylings on headers (th) and data cells (td) also significantly boost user comprehension and experience.

Advantages of using percentage widths and viewport units

Using percentage widths promotes fluidity throughout your layout. Sometimes, specifying widths using viewport units (vw for width and vh for height) can enhance responsiveness across different devices.

th, td { /* Each cell takes up 20% of the viewport width */ width: 20vw; }