September 06, 2010
Page last modified: 08/04/2009

Switch style:   original / newsprint / earthtone / bluze / none

blog

Using CSS to style tables

Posted: Saturday, June 13, 2009

Ok, I haven't made any tech geeky posts in a while, so I thought I'd go ahead and throw this in. If nothing else, it'll give me an easy place to find it when I forget it myself.

Even though using tables for web page layout is pretty much considered a no-no these days, we still need tables when presenting tabular data. The problem is, if you're using an XHTML strict doctype, attributes like border="1" are no longer valid mark-up. So, how do we handle tables in CSS? Here's a quick reference list to most of the common table attributes.
HTMLCSS
cellpadding="0"td { padding: 0;}
cellspacing="1"table { border-spacing: 1px; }
border="1"td { border: 1px solid #000; }
valign="top"td { vertical-align: top; }
align="right"td { text-align: right; }


So this:

<table cellpadding="5" cellspacing="0" border="1" width="600">
<tr>
<td align="center" valign="top">

Can be be written as:

<table>
<tr>
<td>

With the following CSS:

table { border-spacing: 0; width: 600px; }
td { padding: 5px; text-align: center; vertical-align: top; }


Other useful tidbits:

border-collapse (collapse|separate|inherit): When using the border property on both the table and the td, you'll get a double border between cells. Use border-collapse to compress it into a single border.

empty-cells (show|hide|inherit): Specifies whether to show empty cells or not. No more non-breaking spaces required!

Also remember that you can set top, bottom, left, and right padding to different values.

There are advantages to using CSS over the old method.

The sky is pretty much the limit when using CSS. Experiment and see what you can come up with.


<< Back to Blog

cydebar...

You better cut the pizza in four pieces because I'm not hungry enough to eat six.

- Yogi Berra


news...

...cycling

more...

Cycling feed courtesy of cyclingnews.com

...formula 1

more...

F1 feed courtesy of Formula1.com

counter