Explain Codes LogoExplain Codes Logo

How to create a table only using `` tag and CSS

html
responsive-design
css-grid
flexbox
Anton ShumikhinbyAnton ShumikhinยทJan 9, 2025
โšกTLDR

To emulate a table using <div> elements, the keys are CSS display properties: table, table-row, and table-cell. Here's a nifty example you could use right off the bat:

<div class="table"> <div class="row"> <div class="cell">A1</div> <div class="cell">B1</div> </div> <div class="row"> <div class="cell">A2</div> <div class="cell">B2</div> </div> </div>
.table { display: table; /* We are putting the "table" back in "div table"! ๐Ÿ˜Ž */} .row { display: table-row; /* Pretending to be a normal table row here... ๐ŸŽญ */} .cell { display: table-cell; padding: 5px; border: 1px solid #ccc; /* Just a regular cell here... not suspicious at all... ๐Ÿ•ต๏ธโ€โ™€๏ธ */}

This code snippet makes your cells align just like traditional table cells, gives them a nice customary border, and formats the padding, all in a neat, reusable CSS structure.

Mimicking table spacing with CSS

When you're not using actual <table>, replicate table-like spacing using CSS margins:

.cell { margin: 0.5em; /* Exactly a lil' space for our cell ๐Ÿ“ฆ */}

For those who still cherish older browsers, switch modern spacing techniques with inline-block and margin adjustment:

.cell { display: inline-block; /* Going retro here ๐Ÿ’พ */ margin-right: -4px; /* Because older browsers like it edgy ๐Ÿ˜‰ */ }

Dealing with legacy browsers

Accommodating anachronistic Internet Explorer versions can be tricky. For instance, for IE7, we trade display: table-cell for float: left:

.row { overflow: hidden; /* Just clearing the floating rubbish here ๐Ÿšฎ */ } .cell { float: left; /* Let's take a float back to the past ๐ŸŠโ€โ™‚๏ธ */ width: 50%; /* Because half is sometimes better than the whole ๐Ÿฅ */ }

For a retro touch, serve up conditional CSS for IE7 using <!--[if lt IE 8]>:

<!--[if lt IE 8]> <style> .cell { float: left; width: 50%; /* Because ancient explorers love wide views ๐Ÿ‘€ */} </style> <![endif]-->

Maintaining layout consistency

To ensure consistent layouts, use clearfix method to clear floats on .row and avert subsequent content from wrapping around floated elements:

.row:after { content: ""; display: block; clear: both; /* Oops! We had some floaters. All clean now โœ… */ }

Imbuing semantic meaning with divs

Even though using divs for table-making may raise a few eyebrows, maintaining semantic significance shouldnโ€™t be thrown out of the window, especially for accessibility. Entities like ARIA roles, for example, are worth considering:

<div class="table" role="table"> <div class="row" role="row"> <div class="cell" role="cell" data-label="Column A">A1</div> <div class="cell" role="cell" data-label="Column B">B1</div> </div> </div>

Exploring modern CSS alternatives

Unchained from the div concession, you can explore greener pastures like CSS Grid and Flexbox:

.table { display: grid; grid-template-columns: repeat(2, 1fr); /* It's a grid bonanza ๐ŸŽ‰ */} .cell { padding: 5px; border: 1px solid #ccc; /* Ain't nobody got time for unstyled cells! ๐ŸŽจ */}

These powerful layout systems can supersede traditional tables providing you with flexible, cleaner codes.

Implementing responsive design

Responsive design is a must to ensure your pseudo-table is ready for screens of all shapes and sizes. Use media queries and adjust cell display properties accordingly:

@media only screen and (max-width: 600px) { .table-cell { display: block; /* Because mobile devices prefer long reads ๐Ÿ“š */ width: 100%; /* Total make-over for our cell on small screens ๐Ÿ’… */ } }

Visualization

Who says coding can't be fun? We're basically constructing a table with <div> elements and CSS analogous to playing with building blocks:

| ๐Ÿ“ CSS Style | ๐Ÿงฑ Structure | |:----------------------------|:---------------------:| | `display: flex;` | [ ๐Ÿšช ] | <-- row | `flex-direction: row;` | [๐Ÿšช][๐Ÿšช][๐Ÿšช] | <-- cells in a row

Each <div> is a building block:

๐Ÿงฑ = <div class="table-row"></div> // This block is our table row. ๐Ÿงฑ = <div class="table-cell"></div> // This block is a single cell.

Putting it together:

.table-row { display: flex; /* We're going all in for flexible alignments ๐Ÿ’ช */ } .table-cell { flex: 1; /* Flexing not just our muscles, but our cells too ๐Ÿ’ผ */ }

With just a bit more effort, we manage to build a flexible and responsive table - stack by stack!

Advanced techniques

If you're into advanced styling, properties like box-shadow can simulate cell borders, while :nth-child() selector can provide alternating background colors:

.cell { box-shadow: 0 0 5px rgba(0,0,0,0.1); /* Shadow play for cells, because borders are mainstream ๐Ÿ‘ฅ */ background-color: nth-child(even) ? '#333' : '#fff' /* Checkers, anyone? ๐Ÿ */ }

Performance considerations

With vast data quantities, performance and load time become crucial. Strive for efficient rendering paths, such as avoiding excess nested divs and minimizing CSS complexity. You wouldn't want your users to wait while your table is still under construction scope is too long. Scope needs to be defined