CSS border-collapse applied to inline lists
One good trick using CSS is making wrapping blocks of elements. Grouping elements together used to be something developers used table cells for, but when CSS started to be used more regularly, alternate methods were invented. A common use is converting <ul> lists (which are typically vertical structures) into horizontal menus. The really good thing about the CSS-based solution is its flexibility when wrapping is involved.
For table-based designs, the number of rows and columns had to be preset. With a CSS-based wrapping solution the size of the container can dictate the wrapping. For instance:
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
Can be produced like this:
The “block” and “caption” groups are kept together, and sequentially listed in the HTML. They are all floated, and therefore ’stacked’ horizontally, wrapping at the edge of the container. An arbitrary number of these groups can be placed using the same code, and the wrapping is taken care of by the rendering. Here is the same code with ten blocks:
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
To get an even better demonstration, this link will open a new window that you can resize and watch how the wrapping takes place.
At first one of the drawbacks of doing this with CSS as opposed to table is the lack of a border-collapse style! As you can see from the examples above, the bordering edges are doubled. Uh-oh! Border-Collapse only applies to tables!
No problem, just reduce the right and bottom margins by -1. If you want thicker borders, just change these margins to the negative border thickness.
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption
-
blockcaption

November 19th, 2006 at 2:12 pm
Oh my gosh! That is SOOOOO SWEET! Thanks
!!!!
November 19th, 2006 at 2:17 pm
[…] […]
December 5th, 2006 at 12:18 pm
Such a simple solution to a problem that left me scratching my head many nights. Nice write up and good thinking! I will be using that code in the future no doubt.