Electric Type

Multimedia

About Us

News

Help

Lean and Mean Tables

Page 4 — Abdomenize Those Attributes

A previous rule of thumb, "Don't use unnecessary attributes," applies to tables as well. Take a look at the following code and see how many unnecessary attributes you can find:

    <TABLE WIDTH=300 BORDER=0 CELLSPACING=0 CELLPADDING=0>
    <TR ALIGN=left VALIGN=top>
    <TD WIDTH=200 ALIGN=left VALIGN=top>row 1, column 1</TD>
    <TD WIDTH=100 ALIGN=left VALIGN=top BGCOLOR=lightgrey>row 1, column 2</TD>
    </TR>
    <TR ALIGN=left VALIGN=top>
    <TD WIDTH=200 ALIGN=left VALIGN=top BGCOLOR=yellow>row 2, column 1

    <TABLE WIDTH=200 ALIGN=left VALIGN=top BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=yellow>
    <TR>
    <TD WIDTH=50 ALIGN=left VALIGN=top BGCOLOR=yellow>cell a</TD>
    <TD WIDTH=150 ALIGN=left VALIGN=top BGCOLOR=yellow>cell b</TD>
    </TR>
    </TABLE>

    </TD>
    <TD WIDTH=100 ALIGN=left VALIGN=top>row 2, column 2</TD>
    </TR>
    </TABLE>

Here's the result of the code:

row 1, column 1 row 1, column 2
row 2, column 1
cell a cell b
row 2, column 2

What code is extraneous? Lots. Here's what can be removed:

  • WIDTH=300 isn't needed because the two columns within this table are defined as 200 and 100 pixels wide, which obviously add up to 300. Defining the <TD> widths makes stating the overall table width unnecessary.
  • Various occurrences of ALIGN=left VALIGN=top aren't necessary because cell content alignment is already specified in the <TR>s, and those alignments carry down to the <TD>s within those <TR>s.
  • In the second row, the <TD>'s WIDTH=200 and WIDTH=100 aren't needed because the widths were already specified in the first row. By definition, the first column of the second row must be the same width as the first column of the first row, so there's no need to use WIDTH again.
  • Three instances of BGCOLOR=yellow can go away because the entire contents of the nested table inherit the background color of the cell that contains that table.
So the final, cleaned-up code looks like what you see below. It does exactly the same thing, but saves 239 bytes in file size.
    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
    <TR ALIGN=left VALIGN=top>
    <TD WIDTH=200>row 1, column 1</TD>
    <TD WIDTH=100 BGCOLOR=lightgrey>row 1, column 2</TD>
    </TR>
    <TR ALIGN=left VALIGN=top>
    <TD BGCOLOR=yellow>row 2, column 1

    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
    <TR>
    <TD WIDTH=50>cell a</TD>
    <TD WIDTH=150>cell b</TD>
    </TR>
    </TABLE>

    </TD>
    <TD>row 2, column 2</TD>
    </TR>
    </TABLE>

Now your code is looking pretty toned, but let's take it one step further.

next page»


Dynamic HTML  

Frames  

HTML Basics  

Stylesheets  

Tables  

XML  

Javascript  

Database Connections  

Intro To Perl  

HTML 4.0  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.