As a web developer, you sometimes feel the need to use the good-ol’ TABLE elements, since it is so DAMN hard to achieve some things with DIV elements. Although I think TABLE elements are OK if used sparesly, I have been looking for a nice DIV-based alternative for column-based designs.
A few days ago, my collegues Tomas Danemar and Mikael Brander showed me a nice-as-hell solution for building grids with DIV elements. Still, the solution they had found used a lot of class names (like naming each cell to “size1of3″, “size2of3″ etc. so I started experimenting on how to clean it up.
This solution still has the advantage that you just have to do the following:
- Add the class name “grid” to the root element of the grid structure.
- Add the class name “row” to each row elements within the “grid”.
- Add the class name “one”, “two”, “three” etc. (the amount of cells) to each row DIV to specify how many cells it contains.
- Add the class name “cell” to each cell DIV within each row.
- Add the class name “last” to the last row and the last cell of each row.
I first considered not using the “row” and “cell” class names, but believe that they give some encapsulation of the element behavior within the grid.
I will try to improve this design even more in the future, but for now this works very well. I will also make it possible to apply column spans.
CSS
The CSS is really simple:
/* Basic grid settings (you should not have to change these) ************ */
/* Basic grid settings (you should not have to change these) ************ */
.grid {}
.grid .row {}
.grid .row .cell { float:left; _zoom:1; }
.grid .row.one .cell { float:none; }
.grid .row.two .cell { width:50%; }
.grid .row.three .cell { width:33.2%; }
.grid .row.four .cell { width:25%; }
.grid .row.five .cell { width:20%; }
.grid .row.six .cell { width:16.6%; }
.grid .row.seven .cell { width:14.2%; }
.grid .row.eight .cell { width:12.5%; }
.grid .row.nine .cell { width:11.1%; }
.grid .row.ten .cell { width:10%; }
.grid .row, .grid .row .cell.last { overflow: hidden; _overflow:visible; _zoom:1; }
.grid .row .cell.last { float:none; _position:relative; width:auto; }
/* Style your various grids below **************** */
.grid.border .row { border-top: solid 1px black; }
.grid.border .row.last { border-bottom: solid 1px black; }
.grid.border .row .cell { border: none; border-left: solid 1px black; }
.grid.border .row .cell.last, .grid.border .row.one .cell { border-right: solid 1px black; }
HTML example
The following HTML code will create a nice grid with ten rows and an increasing cell amount for each row. Note that the grid uses the “border” class, which adds borders to the grid.
<div class="grid border">
<div class="row one">
<div class="cell">1</div>
</div>
<div class="row two">
<div class="cell">1</div>
<div class="cell last">2</div>
</div>
<div class="row three">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell last">3</div>
</div>
<div class="row four">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell last">4</div>
</div>
<div class="row five">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell last">5</div>
</div>
<div class="row six">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell last">6</div>
</div>
<div class="row seven">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell last">7</div>
</div>
<div class="row eight">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell last">8</div>
</div>
<div class="row nine">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell last">9</div>
</div>
<div class="row ten last">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
<div class="cell last">10</div>
</div>
</div>
Update 2009-12-11: Removed border: none from the last border class.
jh 11:30 pm on December 10, 2009 Permalink |
this is all crap. Try adding a lot of text in a cell and see everything break!!!!!!!!!!
danielsaidi 6:40 am on December 11, 2009 Permalink |
Ha ha ha, well…naturally, the divs will live their own lives if you populate them with different amount of content, especially if you want the cells to have a border. Still, if you are in control of the content, I find it quite convenient to use this solution.
I also use this approach to create column layouts. For a 3 column layout, for instance, I just use a grid/row three/cell-cell-cell structure. Really easy to use…which of course comes with its limitations. It’s NOT as flexible as a table.
Keep in mind that the skeleton above is really general. You are, naturally, free and able to extend the solution with your own classes to help customize your specific needs.