Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making DIVs act like a table using CSS

Alright, while designing a site, I came across a thought... I have a few parts of my site which would be better suited acting as a table, but isn't tabular data. For some reason it really bugs me to use a table for something that isn't a table. So I noted CSS's display options, yet I can't get it to work right. Here is what I am trying. What is the issue?

<div class="table">   <div class="tr">     <div class="td">Row 1, Cell 1</div>     <div class="td">Row 1, Cell 2</div>     <div class="td">Row 1, Cell 3</div>   </div>   <div class="tr">     <div class="td">Row 2, Cell 1</div>     <div class="td">Row 2, Cell 2</div>     <div class="td">Row 2, Cell 3</div>   </div> </div> 

This is what the CSS looks like.

div.table {border: 1px solid black; display: table; } div.tr {border: 1px solid black; display: table-row; } div.td {border: 1px solid black; display: table-cell; } 

I would like the page to look like a table was used but the 'cells' all go on a new-line. Any thoughts?

like image 889
Yoshiyahu Avatar asked Nov 23 '10 23:11

Yoshiyahu


People also ask

How do I make a div behave like a table?

To make DIVs behave like TABLEs, you have to tell them to behave that way using the display property. The three key values that we'll be using are table , table-row and table-cell . Each element now behaves like it were part of a table. In doing so, it also inherits cell capabilities.

Can we create tables using div tag?

Last week, I covered using the DIV tag to make unique content for your course, but did you know that you can also use the DIV tag to make tables! All you need to do is include CSS to create interesting tables.

Can you make a table in CSS?

So, when creating a table, all you need to do is, instead of the HTML 'table' tag, merely use the 'div' tag and add the corresponding CSS to display a table.

Can you style a div in CSS?

The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!


1 Answers

Strange: What you quote looks fine, and should work. Are you sure there is no overriding display: block !important somewhere?

But as my opinion, I'm going to say that for the love of God, just use a table. :)

Seriously. The main argument for not using tables in such situations is that they aren't the right element semantically. But looking at that div-soup, you have to admit a <table> is the way more preferable construct, even if it's not exactly tabular data you're displaying.

like image 183
Pekka Avatar answered Sep 19 '22 03:09

Pekka