1. A basic table

We want to show several data records in a handy grid, let the user sort the list, and delete one or more selected records.

We’ll start with a basic table.

Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes

2. Differentiate the column headers in HTML

We’ll make the first row stand out by making it a <thead> instead of <tr>. Instead of table-data cells, <td>s, table heads use <th>.

Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes

3. Style the table

So far our table doesn’t have any styling applied. Next step is to give it a CSS class that sets various properties.

 .tabular_list th { border:1px solid; border-color:#ddd #999 #888 #ddd; background-color:#ddd; font-weight:bold; text-align:left; color:#003; padding:2px; padding-bottom:0px; font-size:.9em; } .tabular_list td { border:1px solid; border-color:#fff #bbb #bbb #fff; background-color:#fff; padding:1px; font-size:.9em; } 
Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes

These CSS styling rules are contained in a separate file, interface_styles.css.

The table is now described as <table class="tabular_list"> in the HTML. It inherits the properties of the tabular_list class (classes are prefixed with a dot/period in CSS).

The CSS sets properties for all table cells (<td>s) and table header cells (<th>s) in any element whose class is "tabular_list".

<th>s get a 3D-effect border (top and left edges are lighter than the background; right and bottom edges are slightly darker), as well as padding (empty space around the contents of the cell).

<td>s get a grey right edge and bottom edge. This produces the light grid effect.

4. Make the grid stand out from its background

Currently, the table seems to melt into its background. To make it stand out more clearly, I’ll apply a thin black border, again using CSS.

The table now sits inside a <div class="form_border">

 .form_border { border:1px solid #333; } 
Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes

5. Add a select/deselect-all control

I want users to be able to select records in the list (which might be in order to delete them, or ‘mark as read’).

I’ll add a further column, with checkboxes in each, plus a ‘select all’ type checkbox in the column header.

Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes
<script type="text/javascript">
<!--
function checkAll(wotForm,wotState) {
   for (a=0; a<wotForm.elements.length; a++) {
      if (wotForm.elements[a].id.indexOf("delete_") == 0) {
	     wotForm.elements[a].checked = wotState ;
      }
   }
}
// -->
</script>

The HTML for the master ‘select/deselect all’ checkbox looks like this:

<input type="checkbox" onClick="checkAll(this.form,this.checked);" /> 

The call to the checkAll function passes two parameters: firstly, the current form object; followed by the checked status of the master checkbox after it’s clicked.

The function loops through all the elements in the form object passed to it, and tests whether the element’s name starts with the string "delete_". (indexOf returns the first occurrence of a substring within another string, starting at position zero. If the substring is not found, -1 is returned).

If the substring is found at position zero (i.e. the name of the checkbox begins with "delete_"), the function sets the checked property of the current form control to True.

6. Add column sorting functionality

We’ll need a sorted-column indicator, to show which column is currently sorted by.

Each column header should be clickable, to change the sort order. So we need to apply a link anchor to column headers, enabling user to click anywhere in the <th>.

For the sorted-column indicator, I’ll just use a down- or up-arrow GIF image.

Now, each table head cell needs to be totally clickable. The way I’ve done this is to put an anchor/link tag (<a href="etc">) in the <th>, and set the anchor’s properties in the stylesheet as follows.

th a { color:#003; display:block; width:100%; height:100%; } 

The color setting makes the text very dark blue.

"display:block;" makes the anchor behave like a box, rather than like a wrap-around (which would be style "display:inline;". (Note: By default, <div> tags are "display:block;" , whereas <span> tags are display:inline;)

By setting its display to block, I can then set the size of the box to width 100% and height 100%, which makes it fill the space.

(I know the CSS specification says that anchors should never contain block elements, but I can’t find a better way to achieve this effect, so in this case the standard can take a back seat).

Now, if you move the mouse over any of the column head cells (except the ‘select all’), the link highlights, telling you that it’s active.

I’ve also used the title attribute of the anchor tag (i.e. <a title="Link description>Link</a> to provide more information on what clicking each column head will do.

7. Final tabular grid

Item Description Supplier Price Avail
021 Small rubber widget Widgets Inc. £1 Yes
204 Photoshop CS Adobe Inc. £499 No
213 About Face 2.0 Alan Cooper £24.99 Yes
336 Dell Inspiron 5150 Dell.co.uk £999 Yes

Notes on column sorting:

  • Conventional behaviour is that clicking any non-sorted column will sort records by the data in that column, in ascending order.
  • When data is sorted ascending, the column should display an ‘up’ arrow (which goes from thin-to-fat, like the data going from small-to-big). When sorted in reverse order, the arrow should point down.
  • Clicking on the sorted column will reverse its sort order.
  • When developing, I usually write two hidden form fields:
    • "sorted" stores the name of the field currently sorted by.
    • "desc" whose value is either blank, if the current field is sorted ascending, or "desc" if the current field is sorted descending.
    • When writing the SQL query that retrieves the data, I simply write in the current (switched-round) "desc" value at the end of the "ORDER BY" line.

Make Better Web Pages!

How to make your web site sell - use my secrets

Find out How

Do you love our approach to crafting simple & effective web sites that just work for people?

We'd love to hear about your web strategy.

Contact one of our team today!

2 Comments

  1. Anand says:

    Interesting article but the interface_styles.css link doesn’t seem to be working. None of the table boders are showing and the “select all” checkbox doesn’t seem to work.

    Love the site!

  2. ben says:

    Thanks Anand. By-product of our recent migration to WordPress. Now fixed.

Leave a comment

Articles + tutorials in HTML / CSS

Complete List of HTML/xHTML Tags, With Guide to Proper Semantic Use
My Complete List of HTML/xHTML Tags, With Guide to their Proper Semantic Use
Semantic HTML Handbook – Benefits of Writing Semantic HTML
Free article on Semantic HTML. Why you should learn Semantic HTML. Benefits for SEO and code reuse explained.
A Few Tips and Tricks to Write Better Semantic HTML
Tips and tricks for writing better semantic HTML or xHTML from a professional web producer
Building a web page with HTML & CSS for complete beginners
Learn what HTML is and how to build a website from scratch. A guide to creating a web page using HTML and CSS for people with no prior knowledge
Keeping your content in order of priority with flexible CSS layouts.
How to keep your most important content at the top of your page, no matter what kind of column layout you wish to achieve.
Web page production with xHTML and CSS (ebook)
Introduction to Semantic HTML
A Basic Guide to writing semantically correct semantic HTML or xHTML markup
Anatomy of HTML/xHTML tags
HTML basics tutorial: Learn HTML tags, covering the most common attributes
Introduction to Cascading Style Sheets (CSS)
Beginner's introduction to Cascading Style Sheets (CSS), learn CSS.
Making a Tabular list in HTML
Create an appealing tabular list using HTML, CSS and JavaScript
How HTML, CSS and JavaScript work together in web pages
Best practice for using HTML, Cascading Style Sheets, and JavaScript together to make web pages.
HTML Tables – when and how to use tables in HTML
When to use tables in HTML, and how to do it properly
Block vs Inline display style in CSS
An article explaining the differences between block and inline display property in CSS. Gives examples of CSS Block vs Inline CSS and how they are applied in CSS/HTML files and pages.
Datasheet-style form using HTML and CSS
Make a datasheet-style web form using HTML, CSS and JavaScript
Inheritance and Cascading Styles in CSS Explained
Introduction to CSS inheritance and how styles apply in CSS through inheritance and cascading. Read this guide detailed guide on using CSS inheritance.
HTML Lists: unordered, ordered and definition lists
My guide to HTML lists explains the 3 main types of lists web browsers support and how to implement them in HTML / xHTML
HTML, how to code HTML/xHTML markup
Free HTML tutorials: Learn top-quality web page production skills using HTML/xHTML markup
Using CSS (Cascading Style Sheets)
Free tutorial on learning CSS for web design and development
Introduction to HTML – basics of HTML
Introduction to basic HTML tags and the structure of HTML documents.
© Scratchmedia Limited, 2006-2010
Floor 3, 111 Buckingham Palace Road, London, SW1W 0WQ, UK
+44 (0)207 1600 989