XHTML Tables

21
1 XHTML Tables A table is a matrix of cells, for displaying content in rows and columns The cells can include almost any element Some cells display row or column labels and some display data A table is specified as the content of a <table> tag

description

XHTML Tables. A table is a matrix of cells, for displaying content in rows and columns The cells can include almost any element Some cells display row or column labels and some display data A table is specified as the content of a tag. XHTML Tables. Document subtree for table:. - PowerPoint PPT Presentation

Transcript of XHTML Tables

Page 1: XHTML Tables

1

XHTML Tables

• A table is a matrix of cells, for displaying content in rows and columns

• The cells can include almost any element

• Some cells display row or column labels and some display data

• A table is specified as the content of a <table> tag

Page 2: XHTML Tables

2

XHTML Tables

• Document subtree for table:

<table>…</table>

<tr>…</tr> <tr>…</tr>

<td>…</td> <td>…</td>

•Table data elements may contain anything

Page 3: XHTML Tables

3

Tables

• Use a <tr>…</tr> element for each row• Use a <td>…</td> element for each column

• Tables may begin with table header element: <thead>…</thead>

• Tables may end with table footer element: <tfoot>…</tfoot>• Use <th>…</th> element for cells in the table header and footer• Regular rows may be grouped in a table body element: <tbody>…

</tbody>

Page 4: XHTML Tables

4

Tables

<table>…</table>

<thead>…</thead> <tbody>…</tbody>

<th>…</th> <th>…</th>

<tr>…</tr> <tr>…</tr>

<td>…</td> <td>…</td>

…<tr>…</tr> <tr>…</tr>

Page 5: XHTML Tables

5

border attribute

• A border attribute in the <table> tag specifies a border between the cell

• If border is set to "border", the browser’s default width border is used

• The border attribute can be set to a number as the border width in pixels <table border = “5”>

• Without the border attribute, the table will have no lines!

• Tables may be given titles with the <caption> tag, which can immediately follow <table>

• Applications: to display a matrixas layout of the web page

Page 6: XHTML Tables

6

Table With Caption

• See example caption.html<table border = "border"> <caption> Fruit Juice Drinks </caption>

<tr> <th> </th> <!-- Note an empty header here --> <th> Apple </th> <th> Orange </th> <th> Pineapple </th> </tr> <tr> <th> Breakfast </th> <td> 0 </td> <td> 1 </td> <td> 0 </td> </tr> <!– etc. --></table>

Page 7: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline7

Table1.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 5.1: table1.html -->6 <!-- Creating a basic table -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>A simple XHTML table</title>11 </head>12 13 <body>14 15 <!-- The <table> tag opens a table -->16 <table border = "1" width = "40%" 17 summary = "This table provides information about18 the price of fruit">19 20 <!-- The <caption> tag summarizes the table's -->21 <!-- contents (this helps the visually impaired) -->22 <caption><strong>Price of Fruit</strong></caption>23 24 <!-- The <thead> is the first section of a -->25 <!-- table. It formats the table header -->26 <!-- area. <th> inserts a heading cell. -->27 <thead>28 <tr>29 <th>Fruit</th>30 <th>Price</th>31 </tr>32 </thead>

The border attribute gives the size in pixels of the table’s border.

The width attribute gives the width of the table.

The summary attribute describes the table’s contents.

Text placed in a table header is rendered bold and centered in the cell.

Page 8: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline8

Table1.html

33 34 <!-- All table content goes is enclosed within -->35 <!-- <tbody>. <tr> inserts a table row. <td> -->36 <!-- inserts a data cell. -->37 <tbody>38 <tr>39 <td>Apple</td>40 <td>$0.25</td>41 </tr>42 43 <tr>44 <td>Orange</td>45 <td>$0.50</td>46 </tr>47 48 <tr>49 <td>Banana</td>50 <td>$1.00</td>51 </tr>52 53 <tr>54 <td>Pineapple</td>55 <td>$2.00</td>56 </tr>57 </tbody>58 59 <tfoot>60 <tr>61 <th>Total</th>62 <th>$3.75</th>63 </tr>64 </tfoot>65 66 </table>

The body of the table is placed between the tbody tags.

Table rows are created using the tr element

Data placed between td tags are placed in an individual cell.

The table footer belongs at the bottom of the table. It formats text in a similar manner to a table header.

Page 9: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline9

Table1.html

Program Output

67 68 </body>69 </html>

Table footer

End of table body

Start of table body

Table header

Table Caption

Page 10: XHTML Tables

10

Non-uniform cells

• Usually every row has the same number of columns– Cells in same row have equal height

– Cells in same column have equal width

• Sometimes we want a cell to span multiple rows and/or columns

• Use rowspan and colspan attributes

Page 11: XHTML Tables

11

Using a Column Label instead of Caption

• A table may have two levels of column labels instead of a caption– Use a colspan attribute in the<th> tag to specify that the top-

level label span several columns

<tr> <th colspan = "3"> Fruit Juice Drinks </th></tr><tr> <th> Orange </th> <th> Apple </th> <th> Pineapple</th></tr>

Page 12: XHTML Tables

12

See example WOcaption.html

• If more than one row have labels and there is a spanning column label, the upper left corner cell must be made larger, using rowspan

<table border = "border"> <tr> <td rowspan = "2"> </td> <th colspan = "3"> Fruit Juice Drinks </th> </tr> <tr> <th> Apple </th> <th> Orange </th> <th> Pineapple </th> </tr> …</table>

Page 13: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline13

Table2.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 5.2: table2.html -->6 <!-- Intermediate table design -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Internet and WWW How to Program - Tables</title>11 </head>12 13 <body>14 15 <h1>Table Example Page</h1>16 17 <table border = "1">18 <caption>Here is a more complex sample table.</caption>19 20 <!-- <colgroup> and <col> are used to -->21 <!-- format entire columns at once. -->22 <!-- span determines how many columns -->23 <!-- the col tag affects. -->24 <colgroup>25 <col align = "right" span = "1" />26 </colgroup>27 28 <thead>29 The align attribute is used to

horizontally align data in a cell.

The span attribute indicates width of the data cell in number of columns.

Page 14: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline14

30 <!-- rowspans and colspans merge the specified -->31 <!-- number of cells vertically or horizontally -->32 <tr>33 34 <!-- Merge two rows -->35 <th rowspan = "2">36 <img src = "camel.gif" width = "205" 37 height = "167" alt = "Picture of a camel" />38 </th>39 40 <!-- Merge four columns -->41 <th colspan = "4" valign = "top">42 <h1>Camelid comparison</h1><br />43 <p>Approximate as of 8/99</p>44 </th>45 </tr>46 47 <tr valign = "bottom">48 <th># of Humps</th>49 <th>Indigenous region</th>50 <th>Spits?</th>51 <th>Produces Wool?</th>52 </tr>53 54 </thead>55 56 <tbody>57 58 <tr>59 <th>Camels (bactrian)</th>60 <td>2</td>61 <td>Africa/Asia</td>62 <td rowspan = "2">Llama</td>63 <td rowspan = "2">Llama</td>64 </tr>

Table2.html

The vertical alignment of data in a cell can be specified with the valign attribute.

The value of the colspan attribute gives the amount of columns taken up by the cell.

The value of the rowspan attribute gives the amount of rows taken up by the cell.

Page 15: XHTML Tables

2001 Prentice Hall, Inc.All rights reserved.

Outline15

Table2.html

Program Output

65 66 <tr>67 <th>Llamas</th>68 <td>1</td>69 <td>Andes Mountains</td>70 </tr>71 72 </tbody>73 74 </table>75 76 </body>77 </html>

Cell spanning the size of two rows.

Cell spanning the size of four columns.

Page 16: XHTML Tables

16

Missing and empty cells

• Rows may have different numbers of cells (without using colspan)

• Missing cells and empty cells are not rendered• To force rendering of a blank cell use &nbsp; for

content

Page 17: XHTML Tables

17

Attributes align and valign

• align attribute controls the horizontal placement of the contents in a table cell

• Values: left, right, and center (default)

• Use align attribute in <tr>, <th>, and <td> elements

• valign attribute controls the vertical placement of the contents of a table cell

• Values: top, bottom, and center (default)

• Use valign attribute in <th> and <td> elements

• See example cell_align.html

Page 18: XHTML Tables

18

Attributes cellspacing and cellpadding

• Use cellspacing attribute of <table> to specify the distance between neighboring cells in a table

• Use cellpadding attribute of <table> to specify the spacing between the content of a cell and the inner walls of the cell

• See example space_pad.html

Page 19: XHTML Tables

19

Another Example of spacing

<table cellspacing = "50">

<tr>

<td>

<p>Mitten-shaped Michigan greets visitors...</p>

</td>

<td>

<p>In America's Dairyland, there are lots of reasons …</p>

</td>

</tr>

</table>

See example Michigan_Wisconsin.html

Page 20: XHTML Tables

20

Table Sections

• A table may have header, body, and footer, which are the elements:

thead, tbody, and tfoot• See example table2.html of textbook• A document may have multiple tbody elements

Page 21: XHTML Tables

21