CMS

Content

HTML

Images

Monetizing

Home » HTML

Building Simple Tables

Monday, 22 March 2010 · Print This Post Print This Post One Comment

Related Reading

This entry is part 2 of 6 in the series Tables

In this lesson of our free online web design course, we’ll first concentrate on how to create a table with a single cell. To achieve this, we have to use the <table>, <tr> (table row) and <td> (table data/table column) tags respectively.

Copy and paste the below HTML code to your notepad to create a table with one cell:

<html>
<head>

</head>
<body>

<table>
<tr>
<td>
The contents of your table cell go here
</td>
</tr>
</table>

</body>
</html>

Sidenote: A single-cell HTML table is composed of 6 different tags – 3 opening tags: <table><tr><td> and 3 closing tags: </td></tr></table>. Therefore, think of the whole set of 6 tags as one table and not only the <table> and </table> tags ;)

You will not see any table in the output when you save the above HTML code and open it in a browser window. This is because we did not include a “border” attribute within the <table> tag.

HTML Table Borders

By adding a “border” attribute to our single-cell table, we are going to make our table visible to the human eye. Have a look at the below HTML code:

<html>
<head>

</head>
<body>

<table border=”1″>
<tr>
<td>
The contents of your table cell go here
</td>
</tr>
</table>

</body>
</html>

As you see above, we have added the “border” attribute to our <table> tag. The border attribute can take any number starting from zero. This number defines the width of the border in pixel units, along with the “cellspacing” attribute. We’re going to learn about the “cellspacing” attribute later on in our free online web design course. For now, let’s just have a look at the output of the above code:

The contents of your table cell go here

We have a visible single-cell table!

Adding Extra Table Cells

Online Web Design Course continues by teaching you how to create more columns and rows inside your table. In order to create more columns, you simply need to add another <td> tag after the former </td>. This way we are starting another column on the spot where the first one ended. Copy and paste the below HTML code to have a table with two cells composed of one row and two columns:

<html>
<head>

</head>
<body>

<table border=”1″>
<tr>
<td>
Column 1
</td>
<td>
Column 2
</td>
</tr>
</table>

</body>
</html>

Notice that we have started our second <td> tag (i.e. second column) only AFTER the first <td> tag was closed by a </td> tag. The output of the above HTML code is:

Column 1 Column 2

We can also have a table with two rows and one column. To achieve that, you need to start a new <tr> tag where the first one ends. Also keep in mind that to have a valid table cell, you have to have a new <td> tag after each <tr> tag – you can not have raw data inside the <tr> and </tr> tags, without adding the <td> and </td> tags. Just copy and paste the below HTML code to your text editor to understand what I mean:

<html>
<head>

</head>
<body>

<table border=”1″>
<tr>
<td>
Row 1
</td>
</tr>

<tr>
<td>
Row 2
</td>
</tr>
</table>

</body>
</html>

This example of your free online web design course, will create a table with two cells composed of two rows and one column:

Row 1
Row 2

Here’s another example, this time with three rows and three columns:

<html>
<head>

</head>
<body>

<table border=”1″>
<tr>
<td>
Row 1, Column 1
</td>
<td>
Row 1, Column 2
</td>
<td>
Row 1, Column 3
</td>
</tr>

<tr>
<td>
Row 2, Column 1
</td>
<td>
Row 2, Column 2
</td>
<td>
Row 2, Column 3
</td>
</tr>

<tr>
<td>
Row 3, Column 1
</td>
<td>
Row 3, Column 2
</td>
<td>
Row 3, Column 3
</td>
</tr>
</table>

</body>
</html>

We get a 3×3 matrix:

Row 1, Column 1 Row 1, Column 2 Row 1, Column 3
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3
Row 3, Column 1 Row 3, Column 2 Row 3, Column 3

If you enjoyed this lesson, please link to it :)

In the next lesson of our online web design course, we’re going to learn about “cellspacing” and “cellpadding” attributes of HTML tables.

Series Navigation«HTML TablesCellpadding & Cellspacing»

Bookmark this Post Subscribe to Blog

Popular Tags

Adsense Background Cascading Style Sheets colors Content font Forms GIF Hex Codes HTML Hyperlinking Images IMeye JavaScript JPEG Keyword Research layout Merging meta PNG SEO Sizing spacer Tables text W3C
Comments
  • mzphit

    Hi!

    I just HAD to let you know that this sight is a m a z i n g !!! Well done!

    I have just completed an adult college course on entry level web design and so I am not sure if it is the combination of what I have been taught with what you have presented here – (I did did come out of the course a little overwhelmed though)…

    I have learnt so much more in half an hour here than over 18 weeks in class…

    You have explained everything so neatly and I cant tell you enough how good this info is presented. MUCH better I find than (for example) W3…
    ANYWAY – you’ve got my vote and I will tell everyone else back in Cert 3 including the facilitators!!

    peace out
    mzphit

Leave a Comment

Threaded commenting powered by Spectacu.la code.