SPANNING COLUMNS AND ROWS
This lesson assumes that you understand the basic tables presented in Lesson Eleven and are now ready to take a look at some more advanced table features.
You may have noticed in the table on my main home page that I did not use the CAPTION tag. Instead, I placed the instructions for the table in the first row which spanned across all the columns. For example, here is a simplified version of the table on my home page. I grouped the first four lessons together and used a value of "3" for each of the BORDER, CELLSPACING and CELLPADDING attributes.
| |||
LESSON ONE Introducing HTML | LESSON TWO Getting started | LESSON THREE <BR>, <P>, <HR> and other basic tags | LESSON FOUR Emphasizing text and Heading tag |
<TABLE BORDER="3" CELLSPACING="3" CELLPADDING="3" WIDTH ="100%">
<TR>
<TD ALIGN="CENTER" COLSPAN="4"><H3> <BR>CHOOSE ANY LESSON</H3>
<TR>
<TD ALIGN="CENTER"><A HREF="lesson1.htm">LESSON ONE<BR>Introducing HTML</A>
<TD ALIGN="CENTER"><A HREF="lesson2.htm">LESSON TWO<BR>Getting started</A>
<TD ALIGN="CENTER"><A HREF="lesson3.htm">LESSON THREE<BR><BR>, <P>, <HR><BR> and other basic tags</A>
<TD ALIGN="CENTER"><A HREF="lesson4.htm">LESSON FOUR<BR>Emphasizing text<BR> and Heading tag</A>
</TABLE>
The line that generated the first row that spanned across all four columns is:
<TD ALIGN="CENTER" COLSPAN="4"><H3> <BR>CHOOSE ANY LESSON</H3>
Note the following points:
- To span across any number of columns, use the COLSPAN attribute and state how many columns the row is to span across. Thus in our example,COLSPAN="4"(the quote marks are optional) tells the browser to span the first row across four columns.
- The second row begins with LESSON ONE and note the <TR> tag to signify the beginning of this row. Recall that <TR> stands for Table Row. Each new row must be designated with a <TR> tag.
- Note that each cell in row 2 is a link to another page. That is, each cell is contained between the anchor tags <A> and </A>, and each HREF points to another file.
- Note also the use of the <BR> tag to end lines at certain points in each cell. This is needed to prevent the cell from becoming too wide.
- The topics in the Lesson Three cell includes the <BR>, <P>, and <HR> tags. When you place the "<" and ">" angle brackets around a word, the browser will treat the word as a tag and won't print the word. So if I want to have <BR> treated by the browser as text and not as a tag, I use ampersand commands to print the "<" and ">" symbols. The ampersand command for "<" is <. The lt stands for "less than" The letters lt must be written in lower case as shown. The ampersand command for ">" is > (greater than). The ampersand command was introduced in Previous Lesson and so if you need more information on this command, please visit Lesson Five.
- I placed an ALIGN="CENTER" in each cell to center the contents of the cell. As discussed in Lesson Eleven, I could have used only one ALIGN="CENTER" for the entire row if it is placed with the TR tag as in:<TR ALIGN="CENTER">
METALS AND THEIR REASONABLE CUTTING SPEEDS | ||
---|---|---|
MATERIAL | Starting Speed in Feet per Minute | |
High-Speed Steel | Carbide | |
Stainless Steel | 50 | 150 |
Cast Iron | 74 | 225 |
Brass | 300 | 800 |
Copper | 300 | 1000 |
- <HTML>
- <HEAD>
- <TITLE>METAL CUTTING SPEEDS</TITLE>
- </HEAD>
<TABLE BORDER="5" CELLSPACING="0" CELLPADDING="3">
<TR>
<TH COLSPAN="3">METALS AND THEIR REASONABLE CUTTING SPEEDS
<TR>
<TH>MATERIAL
<TH COLSPAN="2">Starting Speed<BR>in Feet per Minute
<TR>
<TH>
<TH>High-Speed<BR>Steel
<TH>Carbide
<TR>
<TD>Stainless Steel
<TD ALIGN="CENTER">50
<TD ALIGN="CENTER">150
<TR>
<TD>Cast Iron
<TD ALIGN="CENTER">74
<TD ALIGN="CENTER">225
<TR>
<TD>Brass
<TD ALIGN="CENTER">300
<TD ALIGN="CENTER">800
<TR>
<TD>Copper
<TD ALIGN="CENTER">300
<TD ALIGN="CENTER">1000
</TABLE>
</BODY></HTML>
Now for a discussion:
- Note that I used <TH> instead of <TD> for the cells that contain the headings. TH (Table Header) both centers and emphasizes the headings.
- Since "Starting Speed in Feet per Minute" spans across two columns, it is considered to take the place of the cells in row 2, columns 2 and 3.
- Note that each <TR> indicates the beginning of a new row in the table.
- The first cell in row 3 is blank and to make it take on the appearance of the other cells, I placed the space character ( ) in the cell.
- Each number was centered in its cell with the ALIGN="CENTER" attribute.
Let's take the last example, and have the heading MATERIAL fill the first two rows in the first column so that the empty cell is eliminated. In other words, let's change the table to look like:
METALS AND THEIR REASONABLE CUTTING SPEEDS | ||
---|---|---|
MATERIAL | Starting Speed in Feet per Minute | |
High-Speed Steel | Carbide | |
Stainless Steel | 50 | 150 |
Cast Iron | 74 | 225 |
Brass | 300 | 800 |
Copper | 300 | 1000 |
- remove the empty cell and
- add the ROWSPAN="2" to the <TH> tag for the "MATERIAL" cell.
<TABLE BORDER="5" CELLSPACING="0" CELLPADDING="3">
<TR>
<TH COLSPAN="3"><BR>METALS AND THEIR REASONABLE CUTTING SPEEDS<BR><BR>
<TR>
<TH ROWSPAN="2">MATERIAL
<TH COLSPAN="2">Starting Speed<BR>in Feet per Minute
<TR>
<TH>High-Speed<BR>Steel
<TH>Carbide
<TR>
Note that "MATERIAL" spans row 2, column 1 and row 3, column 1. These two cells are now considered occupied. Thus the first <TH> for row 3 (High Speed Steel) is considered to be in the second column. Therefore "High-Speed Steel" in the third row was printed in the second column as the first column was occupied. So if a cell spans more than one row or column, just ignore the cell and move to the next available cell in the row.
Now it's your turn to make up a table. There is nothing like learning by doing.
Problem 1: Make up the HTML document that will give the following table:
DRILL SPEEDS | |||||
---|---|---|---|---|---|
MATERIALS | DIAMETER IN INCHES | CUTTING SPEED | RPM | ||
Feet | Metres | ||||
1. | Brass | 2.5 | 350 | 560 | |
2. | Cast iron | 4.5 | 69 | 201 | |
3. | Copper | 1 | 300 | 1200 | |
4. | Stainless Steel | 3.625 | 46 | 167 |
| go to the answer section |
PRINTING A CALENDAR
A calendar is nothing more than a table with a lot of rows and columns.Problem 2: Write the HTML document that gives the following calendar. Use the value "3" for each of the attributes BORDER, CELLSPACING and CELLPADDING.
JULY | ||||||
---|---|---|---|---|---|---|
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
| go to the answer section |
A SIDE-LINE
If you would like to see your web page or part of your web page separated into two sections by a vertical line (a side-line), you could set up it up as a one row, two column table. You could choose a table WIDTH of 100% and a table HEIGHT of any number of pixels that you desire. The width of the table can also be expressed in pixels. If your browser supports full color, the left side is colored yellow.Here is an example:
This side is colored yellowFeel free to place anything on this side. This cell is 30% of the browser screen width. Note that the HEIGHT is in pixels (600) and not as a percent. VALIGN="TOP" (Verticle ALIGN) aligns the text with the top of the cell. | This side just shows plain text.You can color it too if you want. This side is 70% of the width of the screen. Both sides are 600 pixels long. But you can change it to anything you want. Here is the BODY of the HTML document that gives this table. A brief explanation is given to the left. <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="10" WIDTH="100%" HEIGHT="600"> <TR VALIGN="TOP"> <TD WIDTH="30%" BGCOLOR="#FFFF00">This side is colored yellow <P>Feel free to place anything on this side. <P>This cell is 30% of the browser screen width. <TD WIDTH="70%">This side just shows plain text. <P>You can color it too if you want. <P>This side is 70% of the width of the screen. <P>Both sides are 600 pixels long. <P>But you can change it to anything you want. </TABLE> |
<TABLE CELLPADDING="10" WIDTH="100%" HEIGHT="600">
Here is what you should see:
This side is colored yellowFeel free to place anything on this side. This cell is 30% of the browser screen width. Note that the HEIGHT is in pixels (600) and not as a percent. VALIGN="TOP" (Verticle ALIGN) aligns the text with the top of the cell. | This side just shows plain text.You can color it too if you want. This side is 70% of the width of the screen. Both sides are 600 pixels long. But you can change it to anything you want. Here is the BODY of the HTML document that gives this table. A brief explanation is given to the left. <TABLE CELLPADDING="10" WIDTH="100%" HEIGHT="600"> <TR VALIGN="TOP"> <TD WIDTH="30%" BGCOLOR="#FFFF00">This side is colored yellow <P>Feel free to place anything on this side. <P>This cell is 30% of the browser screen width. <TD WIDTH="70%">This side just shows plain text. <P>You can color it too if you want. <P>This side is 70% of the width of the screen. <P>Both sides are 600 pixels long. <P>But you can change it to anything you want. </TABLE> |
Here is another example of a side-line dividing the screen into two two columns. I have removed the yellow background color but you could experiment by leaving it in to see the effect. An explanation and the BODY of this HTML document is given below the table.
Here is the BODY of the HTML document that gives rise to this table.
<TABLE CELLPADDING="10" WIDTH="100%" HEIGHT="600">
<TR VALIGN="TOP">
<TD WIDTH="30%"><IMG SRC="linevert.gif" ALIGN="RIGHT" HEIGHT="600" WIDTH="2" ALT="">Feel free to place anything on this side.
<P>This cell is 30% of the browser screen width.
<P>The vertical line is really an <STRONG>image</STRONG> placed in this cell and aligned to the right side of the cell.<BR>
<TD WIDTH="70%">This is the right side of the table.
<P> I could have placed the vertical line (the image) in this cell by aligning it to the left side of the cell.
<P> This line is 600 pixels in height (the same length as the table) and 2 pixels wide.
</TABLE>
You learned in Lesson Eleven that the WIDTH can also be expressed in pixels instead of as a percent. Therefore, instead of WIDTH="30%" for the first cell, you could use, for example, WIDTH="200" representing 200 pixels. If the average screen width is 600 pixels, then 200 pixels represents one third of the width. I often see the first cell defined in pixels on other web pages. There is no need to define the width at all for the second cell as screen widths vary from browser to browser. As you learned in the last lesson, specifying a width in pixels will give an exact width which can't be changed (and you may want this to be the case). Specifying the width as a percent of the current screen will allow the table to be expanded or compressed to fit the viewer's browser screen. It would be a good learning experience to do some experimenting with the WIDTH attribute.
There is one more side-line option that you will find very interesting and one that you may want to consider.
ANSWERS
Answer to Problem 1: Here is the BODY part of the HTML document. I numbered the first 13 lines so I can refer to them in the discussion given below the document.Line 1: <TABLE BORDER="5" CELLSPACING="0" CELLPADDING="3">
Line 2: <TR>
Line 3: <TH COLSPAN="6"><BR>DRILL SPEEDS<BR><BR>
Line 4: <TR>
Line 5: <TH ROWSPAN="2">
Line 6: <TH ROWSPAN="2">MATERIALS
Line 7: <TH ROWSPAN="2">DIAMETER<BR>IN INCHES
Line 8: <TH COLSPAN="2">CUTTING SPEED
Line 9: <TH ROWSPAN="2">RPM
Line 10: <TR>
Line 11: <TH>Feet
Line 12: <TH>Metres
Line 13: <TR>
- <TD>1.
- <TD>Brass
- <TD ALIGN="CENTER">2.5
- <TD ALIGN="CENTER">350
- <TD>
- <TD>560
- <TR>
- <TD>2.
- <TD>Cast iron
- <TD ALIGN="CENTER">4.5
- <TD>
- <TD ALIGN="CENTER">69
- <TD ALIGN="CENTER">201
- <TR>
- <TD>3.
- <TD>Copper
- <TD ALIGN="CENTER">1
- <TD ALIGN="CENTER">300
- <TD>
- <TD ALIGN="CENTER">1200
- <TR>
- <TD>4.
- <TD>Stainless Steel
- <TD ALIGN="CENTER">3.625
- <TD>
- <TD ALIGN="CENTER">46
- <TD ALIGN="CENTER">167
- </TABLE>
- The <TR> in line 2 indicates the beginning of the first row.
- Note in this table that the title of the table (DRILL SPEEDS) spans across all six columns (line 3). Thus after the title, we begin a new row as indicated by the <TR> in line 4.
- We are now in the second row of the table which is a row of headings. The first three cells in this second row span 2 rows each (lines 5, 6, 7). Therefore, row 2, column 1; row 3, column 1; row 2 column 2, row 3 column 2; and row 2 column 3, row 3, column 3 are all occupied.
- "CUTTING SPEED" is still considered to be in row 2 but it spans 2 columns instead of 2 rows (line 8).
- The heading RPM is also still in row 2 and it also spans 2 rows (line 9).
- After the RPM cell is set up, we need to begin a new row (row 3) as indicated by the <TR> in line 10.
- Note that the headings "Feet" and Metres" are considered to be in row 3, columns 4 and 5 respectively. Since row 3, columns 1 to 3 are occupied, a <TD> will automatically place "Feet" in column 4 (line 11) and "Metres" in column 5 (line 12). They are the only cells left to fill in row 3. Once that is done we begin row 4 (line 13). From row 4 and on, there are no more row and column spans. Some of the cells are empty and as before I placed the space character in them.
Answer to Problem 2:
The first row in the table contains the month "JULY" and this row spans all 7 columns. The blank cell in row 3 spans 2 columns (under "Sun" and "Mon") and to make it take on the appearance of the other cells, I printed the space character ( ) in it. Feel free to experiment with different designs of calendars. Here is the BODY of the HTML document that gives this calendar:
<H3 ALIGN="CENTER">
<P><TABLE BORDER="3" CELLSPACING="3" CELLPADDING="3">
<TR>
<TH COLSPAN="7">JULY
<TR>
<TH>Sun
<TH>Mon
<TH>Tue
<TH>Wed
<TH>Thu
<TH>Fri
<TH>Sat
<TR ALIGN="CENTER">
<TD COLSPAN="2">
<TD>1
<TD>2
<TD>3
<TD>4
<TD>5
<TR ALIGN="CENTER">
<TD>6
<TD>7
<TD>8
<TD>9
<TD>10
<TD>11
<TD>12
<TR ALIGN="CENTER">
<TD>13
<TD>14
<TD>15
<TD>16
<TD>17
<TD>18
<TD>19
<TR ALIGN="CENTER">
<TD>20
<TD>21
<TD>22
<TD>23
<TD>24
<TD>25
<TD>26
<TR ALIGN="CENTER">
<TD>27
<TD>28
<TD>29
<TD>30
<TD>31
<TD COLSPAN="2">
</TABLE></H3>
Post a Comment
THANKS for comment!!!!