Creating Tables

Posted in HTML | Posted on 30-03-2009-05-2008

HTML tables can be used to display tabular data.  If you wanted to display data in the format of a spreadsheet, tables would probably be your best bet.  When you have data that you want to display in rows and columns, tables are the way to go.  You do not have to use tables just for the sake of displaying your spreadsheet data in rows and columns.  You can use Tables as creatively as you want. In this example, we are not going to use a table to display spreadsheet data.  Instead, we are going to use a table to create a calendar.

Copy and paste, or type the following code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
<title>Creating Tables</title>
</head>
<body>
<table style=”border:1px solid #7945D6;” width=”350″ cellspacing=”2″ cellpadding=”5″>
<tr style=”background:#D861C9″ align=”center”>
<td>S</td>
<td>M</td>
<td>T</td>
<td>W</td>
<td>T</td>
<td>F</td>
<td>S</td>
</tr>
<tr align=”center”>
<td></td>
<td></td>
<td style=”background:#C3F500″>1</td>
<td style=”background:#C3F500″>2</td>
<td style=”background:#C3F500″>3</td>
<td style=”background:#C3F500″>4</td>
<td style=”background:#C3F500″>5</td>
</tr>
<tr style=”background:#C3F500″ align=”center”>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr style=”background:#C3F500″ align=”center”>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
</tr>
<tr align=”center”>
<td style=”background:#C3F500″>20</td>
<td style=”background:#C3F500″>21</td>
<td style=”background:#C3F500″>22</td>
<td style=”background:#C3F500″>23</td>
<td style=”background:#C3F500″>24</td>
<td></td>
<td></td>
</tr>
</table>

</body>
</html>

Save this file as tables.htm

Understanding The Code

We have some ground to cover.  We will just take it one step at a time.

We open the code with following line:

<table style=”border:1px solid #7945D6;” width=”350″ cellspacing=”2″ cellpadding=”5″>

What we have here is the opening of a <table> tag.  In the table tag, we declared that this table should have the following attributes:  a 1pixel purple border , the width of the table is 350 pixels, cellspacing of 2 pixels, and cellpadding of 5pixels.

Cellspacing is the amount of pixels between <td> tags.  Cellpadding is the amount of pixels within a <td> tag.

We then open a <tr> tag.  This declares a row within the table.

<tr style=”background:#D861C9″ align=”center”>

Here we declare that the background color of this row will be a pinkish color.  We also want the content within this row to be aligned in the center.

Within in the <tr> tag is the <td> tag or tags.  This is the data that will be displayed in the cell.

<td>S</td>

In this example the letter S is the content.  In the example S stands for Sunday.

Final Word

You have just learned the basics of an HTML table.  With the knowledge that you now posses, you can look at the rest of the calendar example and know what is going on.

Earlier I said that you can use tables as creatively as you want.  There is one exception to the rule.  If you can, try not to use tables for your web layout.  You are much better off using div tags.  A lot of websites on the Internet still use tables for their layouts.  You can use Tables for your layout if you prefer.  I recommend that you learn to use div tags for your layouts.

If you don’t understand what all this layout stuff with tables and div tags are, don’t worry we will go over it in a future tutorial.  For now just experiment with the example we worked on.