| Get your FREE home page! |
| TABLES FOR LAYOUT how to put things side by side on a Web page, and make them stay there by Tony Perlman · Raleigh, North Carolina |
|
LESSONS 1. The Basics 2. Real Layouts 3. When It Breaks STUCK? E-mail me |
Why tables? HTML was built for documents that read like a term paper: a title, then paragraphs, top to bottom. There is no tag that means "put this over here, next to that." So a few years ago people noticed that a table, which was meant for bus schedules and price lists, will hold anything you put in its cells: words, pictures, other tables, a whole page. Turn the borders off and nobody sees the grid. Nearly every good-looking home page you've visited this year is a table with the borders turned off. View Source on a few of them and you'll see. Isn't that what style sheets are for? Someday. Style sheets can position things, on paper, and Internet Explorer 4 and 5 do a decent job of it. Netscape 4 is another story: nudge a box and it lands somewhere else, and resize the window and half your styles can disappear until you hit Reload. Until everybody's browser gets positioning right, tables are what works for everybody, including the kids in the school computer lab who are still on Netscape 3. Who's writing this. I'm 25, a graduate student in Raleigh studying transportation engineering, which means my days are traffic counts and signal timing and my whole life is grids. I learned tables the hard way last year building the page for my research group, then my roommate's band page, and then answering the same handful of questions by e-mail from everybody who saw those. This site is those questions, answered once. Before you start: I'm assuming you know the basic tags (body, p, font, img, a). If you've never written HTML at all, go through Lydia Cavanaugh's HTML from Scratch first. It's the best beginner's course I know of, and her students write me the most organized questions I get. Rows, then cells A table is graph paper. You don't draw columns. You draw rows, and you split each row into cells, and the columns happen by themselves when the cells line up. Three tags do it:
WHAT YOU GET:
Two rows, two cells each, so two columns. The indenting is only for you; the browser ignores it. Every row should have the same number of cells. If one comes up short, the browser leaves a hole where the missing cell would be. The attributes that do the work On the <table> tag:
People mix up padding and spacing all the time, so here are both, with the same 10 pixels:
Padding makes fat cells. Spacing pushes skinny cells apart. For layout you'll nearly always want cellspacing="0" and whatever padding looks right. On the <td> tag:
Stretching a cell: colspan A cell can stretch across two or more columns with colspan. That's how you get a title bar across the top of a two-column page:
WHAT YOU GET:
Count as you go. The first row has one cell, but it's two columns wide, so it counts as two, same as the second row. When the counts don't match, the table comes out lopsided. rowspan works the same way going down instead of across. I use it much less, because it's easy to lose count. Tables inside tables A cell can hold anything, including a whole other table. That's called nesting, and it's behind most of the tricks on page 2. Try not to go more than three or four deep. Every level is more work for the browser, and Netscape gets slow about it. X-ray of this page Here's what you're looking at right now with the borders turned on. Three tables, stacked:
Why three and not one big one? That's on page 3, number 8. |
||||||||||||||||||||||||||
|
You are visitor number Questions, corrections, a table that won't behave: [email protected] Last updated: Saturday, September 18, 1999 · Checked in Netscape 4.61 and Internet Explorer 5 © 1999 Anthony Perlman. Link to these pages all you like; please don't copy them onto yours. |