How would YOU do this: Tables or CSS? [closed]

Update: Final edit. Switched to STRICT DTD, removed italic to match the image in the question, and reverted back to full colour names for ids to show intent as per OPs comment on question, and sorted the main column of id names in the css into the order they appear in the html.

I also opted not to reused the outer div as the white 7 square (it didn't have it's own div in previous edits), as it wouldn't have been practical if you wanted to use the layout, and felt a little like cheating (although from a brevity/pixel perfect standpoint I liked the cheekiness of it).

View here: http://jsbin.com/efidi
Edit here: http://jsbin.com/efidi/edit
Validates as XHTML strict

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>The Challenge</title>
<style type="text/css">
div     { text-align: center; width:175px; height:175px; line-height: 35px;}
div div {         float:left; width: 35px; height: 35px;}
#orange, #maroon,
#blue  , #green  {float:right;}

#orange, #silver {background-color:silver;  width:140px;}
#navy  , #maroon {background-color:maroon; height:140px; line-height:140px;}
         #navy   {background-color:navy  ;}
#green , #red    {background-color:red   ;  width: 70px;}
#yellow, #blue   {background-color:blue  ; height: 70px; line-height: 70px;}
         #yellow {background-color:yellow;}
         #white  {background-color:white ;}
         #green  {background-color:green ;}
         #orange {background-color:orange;}
</style> 
</head> 
<body> 
  <div> 
    <div id="silver">1</div> 
    <div id="maroon">2</div> 
    <div id="navy"  >3</div> 
    <div id="red"   >4</div> 
    <div id="blue"  >5</div> 
    <div id="yellow">6</div> 
    <div id="white" >7</div>
    <div id="green" >8</div> 
    <div id="orange">9</div> 
  </div>
</body></html>

Aside: I would perhaps put a little more whitespace in if I could, but this is at the limit before the code blocks here on SO starts getting scrollbars and I opted to have it all appear on screen.

Note: I borrowed the line-height fix from Tyson (who was first to get a correctly rendering answer).


Here are three solutions.

The markup:

<div id="outer">
    <div id="a1">1</div>
    <div id="a2">2</div>
    <div id="a3">3</div>
    <div id="a4">4</div>
    <div id="a5">5</div>
    <div id="a6">6</div>
    <div id="a7">7</div>
    <div id="a8">8</div>
    <div id="a9">9</div>
</div>

The basic stylesheet (dimensions and color):

#outer {
    width: 20em;
    height: 20em;
}
#a1 {
    background-color: #C0C0C0;
    width: 80%;
    height: 20%;
}
#a2 {
    background-color: #800000;
    width: 20%;
    height: 80%;
}
#a3 {
    background-color: #000080;
    width: 20%;
    height: 80%;
}
#a4 {
    background-color: #FF0000;
    width: 40%;
    height: 20%;
}
#a5 {
    background-color: #0000FF;
    width: 20%;
    height: 40%;
}
#a6 {
    background-color: #FFFF00;
    width: 20%;
    height: 40%;
}
#a7 {
    background-color: #FFFFFF;
    width: 20%;
    height: 20%;
}
#a8 {
    background-color: #008000;
    width: 40%;
    height: 20%;
}
#a9 {
    background-color: #FFA500;
    height: 20%;
    width: 80%;
}

And now the positioning:

  • Using float:

    #a1 {
        float: left;
    }
    #a2 {
        float: right;
    }
    #a3 {
        float: left;
    }
    #a4 {
        float: left;
    }
    #a5 {
        float: right;
    }
    #a6 {
        float: left;
    }
    #a7 {
        float: left;
    }
    #a8 {
        float: right;
    }
    #a9 {
        float: right;
    }
    
  • Using position:

    #outer {
        position: relative;
    }
    #outer div {
        position: absolute;
    }
    #a1 {
        top: 0;
        left: 0;
    }
    #a2 {
        top: 0;
        right: 0;
    }
    #a3 {
        top: 20%;
        left: 0;
    }
    #a4 {
        top: 20%;
        left: 20%;
    }
    #a5 {
        top: 20%;
        right: 20%;
    }
    #a6 {
        top: 40%;
        left: 20%;
    }
    #a7 {
        top: 40%;
        left: 40%;
    }
    #a8 {
        bottom: 20%;
        right: 20%;
    }
    #a9 {
        bottom: 0;
        right: 0;
    }
    
  • Using margin:

    #a1 {
    }
    #a2 {
        margin: -20% -80% 0 80%;
    }
    #a3 {
        margin: -60% 0 0 0;
    }
    #a4 {
        margin: -80% -20% 0 20%;
    }
    #a5 {
        margin: -20% -60% 0 60%;
    }
    #a6 {
        margin: -20% -20% 0 20%;
    }
    #a7 {
        margin: -40% -40% 0 40%;
    }
    #a8 {
        margin: 0 -40% 0 40%;
    }
    #a9 {
        margin: 0 -20% 0 20%;
    }
    

Here you go - less lines than any misuse of table tags can provide:

<img
    src="http://sontag.ca/TheChallenge/tiles.gif"
    alt="nine assorted coloured rectangles"
/>

:P