This
<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>
Can be done with this:
<div>
<div style="display: table-row;">
<div style="display: table-cell;">Hello</div>
<div style="display: table-cell;">World</div>
</div>
</div>
Now, is there any difference between these two in terms of performanc开发者_JAVA技巧e and/or render speed or they're just the same?
It is semantically incorrect to simulate data tables with divs and in general irrelevant to performance as rendering is instant. The bottle neck comes from JavaScript or extremely long pages with a lot of nested elements which usually in the old days is 100 nested tables for creating layouts.
Use tables for what they are meant to and div's for what they are meant to. The display table-row and cell properties are to utilize div layouts more then creating tables for data representations. Look at them as a layout columns and rows same as those you can find in a newspaper or a magazine.
Performance wise you have couple of more bytes with the div example, lol :)
In first instance, I wouldn't worry about performance, but more about semantics. If it's tabular data, use a <table>
. If it are just block elements representing a layout element, use <div>
.
If you really, really worry about performance, then the answer would still depend on the client used. MSIE for example is known to be slow in table rendering. You should at least test yourself in different browsers.
If this worriness is caused by large data, then I'd consider to introduce paging/filtering of the data you're about to show.
Just throwing in my two cents on the topic of performance. For small (under 100 rows, for example) tables, using DIVs wouldn't make much of a difference performance wise.
If you want to generate very long datasets, on the other hand, you hands down absolutely should use traditional HTML tables.
Brief Explanation:
This all spawned from my company's project, where I wrote a Javascript class to procedurally generate tables for me based on SQL data (it's a reporting kind of module). Anyway, I like DIVs so I wrote it to be DIV-based, much like the OP example.
After some horrendous performance (in IE8 particularly), I decided to re-write it using just tables since it's pretty simple tabular data, overall. Tables are, for whatever reason, about twice as fast on my machine in Chrome. The same is true for Safari.
That said, since I can't provide my work's code, I wrote a little benchmark thinggy that just let's you try either or methodology out. You'll see they're nearly identical, just one uses divs with styling to mimic the standard behavior of a table, and the other is just an old school table.
The only real difference is the type of element being generated, so that's about the only thing I can account for in the time delta. I'm sure it varies by browser, but it seems to me that table elements are just faster.
<script type="text/javascript">
var time_start = new Date().getTime();
var NUM_ROWS = 5000;
var NUM_CELLS = 8;
var OLD_STYLE = 1; // toggle 0/1, true/false
if(OLD_STYLE)
{
var table = document.createElement('table');
document.body.appendChild(table);
for(var a = 0; a < NUM_ROWS; a++)
{
var row = document.createElement('tr');
for(var b = 0; b < NUM_CELLS; b++)
{
var cell = document.createElement('td');
cell.innerHTML = 'cell';
row.appendChild(cell);
}
table.appendChild(row);
}
}
else
{
var table = document.createElement('div');
document.body.appendChild(table);
for(var a = 0; a < NUM_ROWS; a++)
{
var row = document.createElement('div');
row.setAttribute('style','display: table-row; padding: 2px;')
for(var b = 0; b < NUM_CELLS; b++)
{
var cell = document.createElement('div');
cell.setAttribute('style','display: table-cell; padding: 2px');
cell.innerHTML = 'cell';
row.appendChild(cell);
}
table.appendChild(row);
}
}
var dt = (new Date().getTime() - time_start)/1000;
console.log( dt + ' seconds' );
</script>
I wanted to mention that if you use a table structure instead of div than users can hold CMD (or ALT in windows) to select a certain area of data from the table to copy. That data also pastes very easily into excel and other similar workbook programs.
There are many discussions about this and div table usually gets the upper hand because of its flexibility in styling. Here's one link - http://css-discuss.incutio.com/wiki/Tables_Vs_Divs
The table wont be rendered until all its markup has been downloaded. While the individual divs will be rendered as soon as their markup is downloaded. I guess the total time will be the same, but the divs will give a perception of better performance and more responsiveness.
You really shouldn't worry about performances in table rendering. Even if there is one, you won't notice it until there are hundreds (thousands?) of tables to display. Use what you feel is more comfortable for you.
In my opinion, the only reason to use divs are for styling and adjusting the layout based on browser size. If it isn't broke, don't fix it. Divs are easier to style though with css.
Tables: pros- you can get a very complicated layout exactly how you want it. More reliable. cons- tables get a little weird sometimes with complicated css styling. not good for responsible websites.
Divs: can adjust based on browser input, more flexible and easier to style.
If you are presenting tabular data, then you should use a table - it, and the related elements, have all the necessary semantics to represent a table of data. A mess of divs has none.
I know this is a really old thread. But it is the #1 result on google when people search "div vs table". I just want to add my 2 cents.
I am an old school PHP coder. I always tended to use table tr td when outputting data (particularly from sql). It seemed completely logical to render rows of sql data in rows of an HTML table. But I have recently changed my mind.
I now recommend div's because: 1. Table's just aren't flexible (at all). div's are completely flexible and customizable. 2. Flexbox (flex) makes it super easy to control layout in CSS (direction, wrap, etc). Layout rows of data in a column and individual data in a row - done!
Tables look like tables. Div's can appear however you want them to appear. Not to mention the issues with '<a href' links in a table. You won't experience any of that weirdness in a div. I was very slow to accept change. But IMHO div's are definitely the way to go.
精彩评论