Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

CSS Table Formatting - The Way Forward

Rated 3.56 (Ratings: 10) (Add your rating)

Log in to add a comment
(15 comments so far)

Want more?

 
Picture of danfascia

Daniel Fascia

Member info | Full bio

User since: July 29, 2001

Last login: October 16, 2008

Articles written: 3

Abstract

Recently on evolt.org there have been several articles relating to and encouraging the use of CSS over HTML standard formatting. It seems that confidence in CSS has increased recently and it is now preferred over standard HTML formatting solutions. What can I say... finally!

To continue this series, I'm going to look at how useful CSS can be for table formatting and relate in particular to tables containing dynamic data.

Introduction

Formatting a table to look elegant and behave within a design layout is never easy and usually requires extortionate amounts of verbose HTML. Defining all of the attributes necessary to make a table behave is frustrating when hand coding and also bulks code when making dynamic tables where chunks are re-iterated many times.

Using CSS, we can modularise many common formatting properties into a style and re-use them where appropriate. Take the following example...

<table summary="" width="250" cellpadding="0" cellspacing="0" border="1">
<tr><td><font face="sans-serif" size="-1" color="#000066">cell data</font></td></tr>
</table>

Ugly I think you will agree. It could look like this

<table class='mytable' width="250">
<tr><td>cell data</td></tr>
</table>

With the approprate stylesheet attached, every time we want a table with these properties we can simply call upon it using the class='xx' attribute. And notice, that key important features such as width remain editable at source level so that in effect we are controlling only useful properties. Let's face it, how many times does any designer use a table with one of those ghastly 3d default borders?

The Next step

Okay, so thus far we haven't re-invented any wheels; but there are some snazzier features that we can look at in CSS tables.

The TABLE has the annoying feature of non-inheritance in CSS hierarchies. That means, that if you set the TABLE font to sans-serif, other important tags within will not inherit that property. TBODY is an example of such a tag that does not inherit -- which is annoying, since we are of course all good little coders and use TBODY (??)

Well in reality, that is a gift as it means that we can set core formatting parameters for a table in one style and then sub-parameters in another style to be applied locally to a TBODY item.

<table class='tableformatting' width="250">
<tr><td>Heading</td></tr>
<tbody class='dataformatting'>
<tr><td>cell data</td></tr>
</tbody>
</table>

In the above, the style class applied to TBODY (and everything wrapped within) overrides that of the table, giving total local control over the data items within it, independently of the styles controlling the TABLE. This also works with other grouping tags within tables such as THEAD and TFOOT but one must be cautious with these tags since their support is inconsistent through browsers.

In the real world

This CSS control of tables may seem slightly anal and intense with regard to real world applications, but consider CSS the next time you are making a script which spits out data dynamically. Rather than chopping up and hacking together lumps of HTML with print() commands all over the place, why not use a style to control matters.

The advantages are immense.

  1. class='xx' is much more elegant than chunks of HTML in your code
  2. You can update the whole look of your TABLE formatting without editing your (illegible) code
  3. Stylesheets can be standardised across a workgroup
  4. Usability and accessibility (read on...)

Usability and Accessibility

A final coup in favour of CSS for table formatting has to be its use in the realms of equality in the use of the web. A visually impaired user can be offered a different stylesheet for table formatting allowing larger fonts and more vivid colours -- nothing new there BUT using our TBODY model, the formatting of the table need not change, just that of the data within. Both parties are happy; your design remains and the user can access your site.

Similarly, text to speech software can have trouble with tables as it tends to ignore the fact that most designers use them for layout and not only data tables. The word 'table' starts to really annoy you when using one of these systems. With CSS one could notify the text-reader to ignore all but the data within TBODY and so on...

Conclusions

It is time to start using CSS to format tables. Although it may not be as hands-on as hacking out a TABLE in HTML, the end results are more elegant, more manageable and more accessible; and after a few tables done this way, you will never go back.

No longer actively making websites since being a full time doctor has taken over most of my spare time and sapped what imagination I had left by the end of medical school completely.

Legacy site designs:
· EC Executive Search automotive recruitment
· Edinburgh University: Microbiology CAL
· Edinburgh University: Lawn Tennis Club
· Follow Fascia : on elective
· Alpybus : low cost chamonix geneva transfer company
· Malignant-Melanoma.org : Evidence based medical advice for patients suffering malignant melanoma, a form of skin cancer

www.fascianewmedia.co.uk

Submitted by jobarr on December 2, 2001 - 22:37.

You can put cellpadding, cellspacing, etc into a style sheet? Can you post an example please? Also...is this any less compatible than CSS in other tags?

login or register to post comments

further expansion

Submitted by i_am_flipper on December 2, 2001 - 22:39.

you can further expand the Real World Application of this by loading multiple stylesheets based on dynamic server side browser sniff and also by the clever use of subdomains and SSI's you can create multiple looks and feels [?] with multiple stylesheets and the same tables of data for simple content sharing schemes ... its been done for years ;)

login or register to post comments

Submitted by smok on December 4, 2001 - 09:43.

jobarr:

how about using padding-left:0px;, and padding-right and -top and -bottom
(CSS 1.0

i don't know what about cellspacing, I think border-spacing is used for this. (CSS 2.0)

anyway, i would note cellpadding and spacing also in the table, since not every browser can handle ...

login or register to post comments

using styles and tables

Submitted by teradome on December 4, 2001 - 14:28.

The one trick when using CSS to style tables is to remember that the styles are applied only on the specified element's layout.

The way things like <table border="1"> makes a border that runs within table rows and cells doesn't happen with CSS, because if you used table { border: 1px; }, ONLY the table block would have a border. Same with padding -- the space holding the table would get the padding (and border), but the cells would not inherit the setting, because it's not really getting applied to the table itself, only to the space it sits in.

You must apply styles to the TRs and TDs seperately. So for what you want, you'd do something like:

.mytable {
width: 250px;
}
.mytable > tr > td { /*only immediate tds of mytable*/
padding: 6px; /*cellpadding*/
margin: 0px; /*cellspacing*/
}
.mytable td { /*any td at any level of mytable*/
font-size: small;
}

login or register to post comments

partly true

Submitted by danfascia on December 6, 2001 - 06:18.

that is not entirely true since there is some inheritance between table elements. TD and TR inherit but higher level elements such as TBODY do not... as I mention in the article, this is a good basis for separating the control of layout from the control of data formatting in a dynamic set-up

login or register to post comments

extortionate?

Submitted by gohlkus on December 20, 2001 - 13:14.

Dan:

An "extortionate" amount of HTML? Are your clients blackmailing you? Perhaps you mean "exorbitant."

Either way: good article, good idea.

For those that would like code examples (and I've been sloppy on my personal site; forgive me), I've applied CSS to tables on the front page of http://gohlkusmaximus.com, which uses CSS for table background colors and type styles. (Just look for 'class="mid".')

It was a bit tricky to decide what tags to use to get it to degrade gracefully. In browsers that support CSS, the featured text is black with red links on an orange field; in NS 3, in contrast, I have white type and orange links on black. (I haven't applied the same code to internal pages out of sheer laziness.)

login or register to post comments

One's English

Submitted by danfascia on December 20, 2001 - 21:23.

As the Oxford dictionary says: EXORBITANT -/ adj. (formal) (of a price) much too high: exorbitant costs EXTORTIONATE AmE -str-/ adj. (disapproving) (of prices, etc.) much too high SYN: excessive, outrageous... decide for yourself but I reckon they are fairly identical

login or register to post comments

you say extortionate... I say, embarrassed:

Submitted by gohlkus on December 20, 2001 - 22:37.

Well, you're right, they are fairly equivalent. I guess it's just a matter of word choice. My apologies for being needlessly pedantic and brash.

login or register to post comments

;-) have a beer...

Submitted by danfascia on December 21, 2001 - 18:49.

Haha, I dont mind, Im more pedantic for printing the oxford online, all goodfun...

I find having a beer makes for even more inappropriate use of words!

login or register to post comments

Yup.

Submitted by haidary on December 22, 2001 - 02:34.

I would have to agree with that last part.lol.

login or register to post comments

heh

Submitted by gohlkus on December 22, 2001 - 05:09.

And sometimes beery words are the most appropriate of all. :)

login or register to post comments

Use of 'extortionate'

Submitted by jsp on January 22, 2002 - 21:55.

I must respectfully disagree with the use of extortionate in this context. Though the OED was invoked, it’s very relevant here to note that the definition included the comment “of prices.” There are extortionate prices, surely — because we’re referring to the people who set them. The word’s Latin root means “to wrench out,” and for that we’re referring to the shopkeeper’s intent.

Yet can we apply this same definition to something, like an amount, that’s not related to price? Who is trying “to wrench” (extort) something from us in that case? The W3C? It’s unlikely. Since there is no person behind this effort, we really are on a weak footing when we discuss extortion.

Exorbinant, on the other hand, comes from the Latin for “to deviate.” This seems like a better fit. Numbers can deviate. Numbers can exceed.

Clearly, exorbinant is the only choice.

login or register to post comments

exorbinant, exorbitant

Submitted by isuadam on January 22, 2002 - 22:06.

potato, potato, tomato, tomato, let's call the whole thing off...

login or register to post comments

Yawn...

Submitted by danfascia on January 23, 2002 - 09:46.

Okay I dont want anymore bollocks about extortionate vs exorbitant.... I dont care and dont have time to care

login or register to post comments

revision on comment

Submitted by teradome on January 29, 2002 - 14:48.

I had spoken too soon: margins in cells don't really exist because of the columnizing that needs to be applied to them within a table layout.

The way to address cellspacing is through this rule:

table {
border-collapse: collapse;
border-spacing: 0;
}

Be prepared to see it fail in a number of browsers as it is strictly a CSS-2 property.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.