Building Rounded Corners With CSS and JavaScript - Our old friendly tables
(Page 2 of 6 )
Being still present in so many websites, tables are intensively used to tackle the rounded corners effect. Since they are well-supported elements for the vast majority of browsers, using layout tables is the old-fashioned approach to solve the problem. Let’s see the code for that:
<table width="300" cellpadding="0" cellspacing="0">
<tr>
<td width="20"><img src="tl.gif" width="20" height="20" alt="" /></td>
<td width="260" bgcolor="#0000ff"> </td>
<td width="20"><img src="tr.gif" width=20” height="20" alt="" /></td>
</tr>
<tr bgcolor="#0000ff">
<td width="20"> </td>
<td width="260">Content goes here</td>
<td width="20"> </td>
</tr>
<tr>
<td width="20"><img src="bl.gif" width="20" height="20" alt="" /></td>
<td width="260" bgcolor="#0000ff"> </td>
<td width="20"><img src="br.gif" width="20" height="20" alt="" /></td>
</tr>
</table>
While it's still used widely across the Web, and was considered an acceptable approach a few years ago, now it’s rather an old solution, with plenty of useless and redundant markup for a decorative effect. The major drawback of using this technique is the problem that usually arises when images are inserted into tables. Small gaps and spaces will usually appear below the corner images, breaking down the table structure and making our rounded corners look very ugly and unprofessional.
The reason for this nasty effect is that images are “inline” elements, behave accordingly, and leave spaces between them and the cell boundaries where they are included. An easy approach to getting rid of the undesired artifact is to define that images within table cells will behave as “block” elements, instead of “inline” elements. Just including the proper CSS declaration within the stylesheet will fix the problem:
td img {
display: block;
}
Setting the “block” element behavior for images will achieve the desired result. Also, Internet Explorer renders undesirable small gaps when inserting images into tables. If we have the following code:
<td>
<img src="tl.gif" width="20" height="20" alt="" />
</td>
the image won’t fit properly into the table cell. A quick fix for this is to restructure the code as follows:
<td><img src="tl.gif" width="20" height="20" alt="" /></td>
Now, the image will fit nicely into the cell. Pretty weird, huh?
The result for rounded corners with tables is shown in the next figure:

As seen previously, table-based rounded corners is still a viable solution for a few cases. But with the power of CSS, we can achieve the same effect without writing a lot of redundant markup, making our code tight and efficient.
Next: The CSS approach >>
More JavaScript Articles
More By Alejandro Gervasio
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|