Explain Codes LogoExplain Codes Logo

Can you do this HTML layout without using tables?

html
responsive-design
css-grid
flexbox
Anton ShumikhinbyAnton Shumikhin·Nov 14, 2024
TLDR

To construct non-table-based layouts, choose CSS Flexbox for linear layouts or CSS Grid for more complex designs. Here’s a Flexbox example for a responsive two-column layout:

<style> .flex-container { display: flex; /*Flexin' on 'em.*/ } .flex-item { flex: 1; /* We be flexin'. You see what I did there? */ } </style> <div class="flex-container"> <div class="flex-item" style="background: lightblue;">Left Column</div> <div class="flex-item" style="background: lightgreen;">Right Column</div> </div>

Each .flex-item occupies an equal fraction of the space within .flex-container, removing the need for HTML tables.

Alternatives to table layouts

Tables have been a staple of web design for years, but modern CSS provides better flexibility. With properties such as padding, margin, and precise positioning, you can build visually appealing and responsive layouts with no table restrictions.

Flexing with Flexbox and Grid

CSS Flexbox is ideal for one-dimensional layouts - if you want a row or a column, this is your tool. It is exceptionally good for equal-width columns, as demonstrated above.

For complex, two-dimensional designs, CSS Grid is the way to go. It gives you the power to control both rows and columns, making it easier to design sophisticated layouts.

CSS properties to the rescue

To clear floats and contain elements, utilize overflow: hidden. Additionally, to ensure the next elements flow as expected after floating elements, utilize clear: both.

Inline blocks come in handy when fixed widths aren't necessary. They offer a neat method to align columns vertically. For a more "in your face" placement, combine absolute positioning with negative margins.

Browser compatibility & accessibility

When old-age browsers are on your radar, Twitter Bootstrap offers predefined classes for backward compatibility.

For heightened accessibility, WAI-ARIA roles can be applied to non-semantic layouts. Always check the WAI-ARIA presentation role documentation for the latest compatibility updates.

Dealing with Internet Explorer... yes, really

For stubborn Internet Explorer issues, there are fixes using hasLayout and display: inline-block. For the most stubborn cases, like support for ancient browsers like IE5, you might have to revert to tables—only as a last resort, though!

Crafting vertical centering

Without tables, achieving a vertically-centered layout was once an uphill battle.

Mimicking table layouts with CSS

The display: table-cell property creates a table-like behavior that enables vertical alignment—no actual table element needed. This keeps your code clean by dodging nested tables.

Modern centering solutions

In the realm of modern CSS, methods like align-items: center; for Flexbox or place-items: center; for Grid center your content vertically and horizontally.

Additional technical tips

Avoid underestimating the importance of spacing and widths when designing layouts. Make use of precise percentage widths or flex-basis with media queries to ensure elements resize or reposition correctly across a variety of devices.

Spacing between wrappers also requires attention to prevent unexpected layout shifts that could disrupt user experience.

Test your layout on different browsers to catch any issues and be prepared for making browser-specific tweaks when necessary.