[Note: for those who may be confusing this question with "why not use tables for HTML layout", I am not asking that question. The question I'm asking is why is a grid layout fundamentally different from a table layout.]
I'm researching CSS libraries (in particular Bootstrap) for a project. I am a programmer rather than a web designer and I feel I could benefit from a library that encapsulates good design.
We all know that it's bad practice to use HTML tables to accomplish basic site layout because it mixes presentation with content. One of the benefits provided by CSS libraries like Bootstrap is that they offer the ability to create "grid" layouts without using tables. I'm having a little trouble, however, understanding how their grid layouts differ in any meaningful way from the equivalent table layout.
In other words, what is the fundamental difference between these two examples of HTML? Am I wrong in thinking that the grid layout is simply a table with another name?
<div class="row"> <div class="span16"></div> </div> <div class="row"> <div class="span4"></div> <div class="span4"></div> <div class="span4"></div> <div class="span4"></div> </div>
and
<table> <tr> <td colspan=4></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table>
Bootstrap's grid system is responsive, and the columns will re-arrange depending on the screen size: On a big screen it might look better with the content organized in three columns, but on a small screen it would be better if the content items were stacked on top of each other.
Grid provides functionality for page layout vs. Data Table which provides data rendering and some interactivity vs. Data Grid which provides a data-driven spreadsheet-like level of interactivity.
The Bootstrap Grid System is used for layout, specifically Responsive Layouts. Understanding how it works is vital to understanding Bootstrap. The Grid is made up of groupings of Rows & Columns inside 1 or more Containers. The Bootstrap Grid can be used alone, without the Bootstrap JavaScript and other CSS Components.
If you're layout-first, meaning you want to create the layout and then place items into it, then you'll be better off with CSS Grid. But if you're content-first, meaning you have items that you want to place into a container and space out evenly, then go with Bootstrap.
The difference is that the first example is semantically marked up, assuming the data being marked up is not actually tabular. <table>
should only be used for tabular data, not for any data which happens to be displayed in a layout similar to a table.
It is correct though that using CSS packages like Bootstrap, which require you to assign classes to HTML elements which are not semantic but presentational, reduces the separation of content and presentation, making the difference somewhat moot. You should be assigning semantically meaningful classes to your elements and use lesscss mixins (or similar technology) to assign presentational behavior defined in the CSS framework to these classes, instead of assigning the presentational classes to the elements directly.
Say:
<div class="products"> <div class="product"></div> </div> .products { .row; } .products > .product { .span16; }
Note that I say should. In practice this is not necessarily always the more workable option, but it should be the theoretical goal.
I believe that CBroe comment is the best option, so I chose to clarify it.
Avoid div
's. A div
should be your last resort, not your first option. Instead, try to use Bootstrap classes on the actual elements. For instance:
<form class="container"> <fieldset class="row"> <label class="span4" for"search">Type your search</label> <input class="span6" type="text" id="search" /> </fieldset> </form>
It is a shame to use fieldset to contain a single field, but it is semantically best than using a div
for the same thing. The HTML5 standard defines many new container elements, such as article
, section
, header
, footer
and many more. In some cases you will have to use div
's, but if you minimize it's use then your code will be way more semantic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With