Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

HTML 4_01 Weekend Crash Course - G. Perry

.pdf
Скачиваний:
34
Добавлен:
24.05.2014
Размер:
6.32 Mб
Скачать

Session 12—Tables Organize Data

155

<th>Quantity</th>

<th>Price</th>

Laughing Kids 24 $19.95

I Stood Still 13 $7.95

Make Money Now 52 $22.95

</table>

Title will appear at the top of the table’s first column; Quantity will appear at the top of the table’s second column; Price will appear at the top of the third column.

Specifying the rows

When you use the <tr> tags, your table begins to look like a real table. Insert <tr> before the data value that begins each row and </tr> at the end of each row. The code looks like the following:

<table>

<tr>

<th>Title</th>

<th>Quantity</th>

<th>Price</th>

</tr>

<tr>

Laughing Kids 24 $19.95

</tr>

<tr>

I Stood Still 13 $7.95

Afternoon Saturday—III Part

12 Session

156

Saturday Afternoon

</tr>

<tr>

Make Money Now 52 $22.95

</tr>

</table>

At this time, the table is shaping up in rows, as shown here:

Title

Quantity Price

Laughing Kids 24 $19.95

I Stood Still 13 $7.95

Make Money Now 52 $22.95

Note

The <th> tag automatically boldfaces the header cells to help separate them from the data cells. You can always format the header and data further with the available formatting tags.

Obviously, the table needs work. The data does not properly align in columns, but that should not be a surprise considering no <td> tags appear yet to separate the data into columns.

Specifying the columns

Use the <td> tag before each cell value, followed by the </td> end tag to specify columns. This will complete the standard table. Here is the code to do just that:

<tr>

<th>Title</th>

<th>Quantity</th>

<th>Price</th>

</tr>

<tr>

<td>Laughing Kids</td> <td>24</td> <td>$19.95</td>

</tr>

<tr>

Session 12—Tables Organize Data

157

<td>I Stood Still</td> <td>13</td> <td>$7.95</td>

</tr>

<tr>

<td>Make Money Now</td> <td>52</td> <td>$22.95</td>

</tr>

The table now appears in nice rows and columns, with each column wide enough to hold either the longest cell data or the longest header. Here is the table as it now stands:

Title

Quantity

Price

Laughing Kids

24

$19.95

I Stood Still

13

$7.95

Make Money Now

52

$22.95

Much work is left, but the basic structure is now in place. It may seem as though HTML tables are time-consuming to create. However, you’ll combine many of the steps that this session has broken down individually when you create your tables. In addition, you’ll begin formatting the tables more appropriately earlier in

the process. The next few minutes concentrate on table formatting.

Improving the Look of Your Table

Think of all the improvements needed to make the book inventory a more useful table. The table’s look would benefit from lines around and in between the table cells. Headers should consistently center or left-justify over the columns, and the cell widths need to change somewhat.

Adding borders

The <table> tag supports a border= attribute that draws borders (or lines) around and between your table’s cells. The format of the tag and attribute are as follows:

<table border=width>

Afternoon Saturday—III Part

12 Session

158

Saturday Afternoon

The width value is the number of pixels wide the border should consume. To create a border, therefore, you might replace the opening <table> tag with this:

<table border=8>

Figure 12-2 shows how a bookseller’s table with a border appears in the browser window. The table looks good, actually, considering how little effort went into it.

Figure 12-2

The table’s shaded borders create a professional look.

Aligning data

The table tags give you complete control over your table’s format, including the alignment of the header and data cells against the left or right edge of the borders as well as to other cells. You can align entire rows and columns or individual cells.

The following HTML statement aligns a table header’s text so it left-justifies within the cell:

<th align=left>Title</th>

The align= attribute works in both header cells and data cells to align the data inside those cells to the left, center, or right side of the cells in relation to the border. Even within the same row of header cells, you may want to center some headers and left-align others.

In addition to aligning data horizontally, you can align text vertically within a row with the valign= attribute. The valign= attribute takes these three values: valign=top, valign=middle, and valign=bottom.

Session 12—Tables Organize Data

159

Spanning individual cells and columns across multiple rows in the table (discussed next) is when you need to vertically align data.

Spanning cells

One of the most useful aspects of HTML tables is that each cell in each row does not have to conform to the width or height of each cell in other rows. The uniqueness of the cells lends HTML tables to entire and complex Web page design.

When the cells in each row are not the same height and width, you can span individual cells and columns across multiple rows. For example, you can put the table’s title in your first row and span the title across the entire table.

When using the <caption> tag to add a table title, in many ways, the <caption> tag offers little more than regular HTML text offers. The only exception is that

the caption stays with the table when you move the table and is actually part of the table. The <caption> tag falls directly below the <table> tag, as follows:

<table border=8>

<caption>Our In-Stock Books</caption>

Again, the caption appears above the table. The browser does not even boldface the text as it does for column header cells. You are welcome to apply any formatting tags to the caption as long as you place those tags after the start tag, <caption>. Your other option for folding a table title into the table itself is to place the

title directly into the first cell of the first row and then span, or lengthen, that cell to consume the entire width of the table. To span across columns, you use

the <colspan=width> tag, and to span across rows, you use the <rowspan=width> tag. The width value is the number of columns or rows that you want the cell

to span.

The code in Listing 12-1 generates the table shown in Figure 12-3, with the table title appearing directly inside the table. Because of the colspan=3 attribute, the first row spans all three columns. Without the colspan=3 attribute, the row would consume only the first column of the table, and the rest of the table would fall beneath that.

Afternoon Saturday—III Part

12 Session

160

Saturday Afternoon

Figure 12-3

Because of the colspan= attribute, the table title now spans across the three columns.

Listing 12-1

Using the colspan= attribute to distribute a cell across several columns

<table border=8> <tr>

<th colspan=3>Our In-Stock Books</th> </tr>

<tr>

<th align=left>Title</th> <th align=left>Quantity</th> <th align=left>Price</th>

</tr>

<tr>

<td>Laughing Kids</td> <td>24</td> <td>$19.95</td>

</tr>

<tr>

<td>I Stood Still</td> <td>13</td> <td>$7.95</td>

</tr>

<tr>

Session 12—Tables Organize Data

161

<td>Make Money Now</td> <td>52</td> <td>$22.95</td>

</tr>

</table>

More Powerful Tables

Some of the formatting tricks discussed in this section can enhance the appearance of your tables. Although tables hold data effortlessly, the page layout tricks and tips you learn here make tables almost a requirement for anything more than a simple sequential, flat Web site. They lend themselves to more powerful Web page design.

Adding a background color to a table

Adding a background color to your table makes the table stand out. You can do this by adding the bgcolor= attribute inside the <table> tag to colorize all the cells in the table. As with all HTML color-related attributes, you can specify either a hexadecimal color code such as #20CCFF or one of the 16 named colors such as red (as discussed in Session 8).

The following statement colors the cell’s background blue:

<td valign=top align=left bgcolor=blue>

Adjusting table spacing

Two attributes, cellpadding= and cellspacing=, enable you to place cells more precisely in relation to each other. You must specify the attributes in pixels with the width value as follows:

cellpadding=width cellspacing=width

By adjusting the cellpadding= attribute, you add some room around your cell’s text that is greater than the default pixel value of 1. The cellspacing= attribute increases the size of the space between the cells. The inner lines that divide the cells increase in width as you increase the cellspacing= attribute value.

Afternoon Saturday—III Part

12 Session

162

Saturday Afternoon

The width= attribute enables you to determine exactly how much of the Web page your table will span no matter how wide the user’s browser window is. The default width of a table is the amount of space necessary to display that table. If, however, you specify a fixed width value, the table enlarges or contracts to respect that width. If you do not specify a width that is wide enough to display the cell contents, the browser squeezes the table and wraps the contents of the cells to respect the width that you specify.

Never specify a table width greater than 550 pixels. Users with small displays will not be able to see a wider table reliably.

Never

Adjusting table space with percentages and multiple tables

Instead of specifying the width in pixels, you can specify the width= attribute in percentage terms. Therefore, you can request that a table consume 85 percent of the browser’s remaining window size. Whatever the browser’s window size is, the browser will adjust to 85 percent of that size. By centering the table, you can ensure that the table maintains a uniform look, in most cases, even though you cannot predict the size of the browser window that will display the table. You can also use more than one table to help accomplish appropriate spacing, among other tasks.

The example in this session will go a long way towards showing you what you can do with the knowledge of tables you now possess. Consider the partial Web page shown in Figure 12-4. The author of this page wants this information to span the user’s Web page no matter how wide or how narrow the user sets the browser’s window.

Although tables are simple to create and maintain, such a Web page element is not simple without the use of tables. The portion of the Web page you see in Figure 12-4 contains two tables, not just one. The tables are on top of each other, and both tables contain one row each. The top table, which forms the black band, contains one row and two columns, and the bottom table, which forms the text and the graphics, contains one row and three columns.

The reason for the two tables is so the black band across the top of the three cells can expand or contract as needed without messing up the width of the text on that band. In other words, Today’s Events resides in the first cell to maintain its spacing, size, and height no matter how wide or narrow the Web page becomes. Yet, the rest of the cell is black and expands to fill the window no matter how wide the Web page is.

Session 12—Tables Organize Data

163

Figure 12-4

Tables make professional Web site elements possible.

Actually, the bottom table is larger but more straightforward. The Web page author created the one-row, three-column table, centered the text and graphic images inside each of the three cells, set the border size to 2 pixels, set the cellpadding to 10 pixels, and set the cellspacing to 5 pixels. (The border determines the thickness of the border lines, and the cellspacing attribute determines how wide apart the lines are that comprise the border.) In addition, the Web page author set the width= attribute in the bottom of the table to 100 percent. The table spans the user’s Web browser screen no matter how wide the screen becomes.

The following code defines the bottom table:

<table border=2 width=100% height=177 cellspacing=5 cellpadding=10> <tr>

<!-- First cell of the bottom table -->

<td width=33% height=137 valign=top align=center><font face=”Arial” size=3>

<b>Investor’s Roundtable</b></font>

<p><font face=”Arial” size=3>Listen to the experts debate the where’s and why’s of investing in today’s volatile market.</font></p>

<p><img border=0 src=”BD04972_.gif” width=136 height=104 align=middle>

Afternoon Saturday—III Part

12 Session

164

Saturday Afternoon

</td>

<!-- Second cell of the bottom table -->

<td width=33% height=137 valign=top align=center><font face=”Arial” size=3>

<b>Online Investing or Betting?</b></font>

<p><font face=”Arial” size=3>Are your odds better when you send money to your discount broker than when you throw the dice at the Vegas tables?</font></p>

<p><font face=”Arial” size=3><img border=0 src=”BD04897_.gif” width=160 height=94 align=center></font>

</td>

<!-- Third cell of the bottom table -->

<td width=33% height=137 valign=top align=center><font face=”Arial” size=3>

<b>Retirement Funds</b></font>

<p><font face=”Arial” size=3>IRAs, Roth IRAs, Keoghs, 401-K’s, the terms get confusing. Find out what you can do to maximize your future wealth.</font></p>

<p><font face=”Arial” size=3><img border=0 src=”BS02064_.gif” width=94 height=91 align=middle></font>

</td>

</tr>

</table>

Note

Notice that each cell in the lower table, defined with the <td> tag, consumes 33 percent of the screen width. The fact that three cells at 33 percent consume 99 percent of the screen means that the bottom table will be slightly smaller, by about a pixel or two, than the top row with the black band. However, the difference is negligible and noticeable by only the author and you.

The top table, the black band, consists of a one-row, two-column table with no cellspacing, no border, and no cellpadding. The left cell holds the text and is fixed at 25 percent of the screen width. To fill out the right portion of the band, the second cell, also formatted with a black background color, contains a width of

75 percent and a right alignment as follows:

<table width=100% height=24 cellpadding=0 cellspacing=0 border=0> <tr>

<td valign=top align=left width=25% height=24 bgcolor=black><font face=”Arial” size=4 color=white>

<b>T o d a y ‘ s   E v e n t s</b></font><br> </td>

<td width=75% height=24 valign=top align=right bgcolor=black>