You are here:   HomeHTML LanguageHTML TutorialHTML MARQUEE
by Dinesh Thakur Category: HTML

A table is an orderly arrangement of data distributed across a grid of rows and columns similar to a spreadsheet. In printed documents, tables commonly serve a subordinate function, illustrating some point described by accompanying text. Tables still perform this illustrative function in HTML documents.

However, because HTML alone does not offer the same layout capacities available to print designers, Web page tables also are commonly used to structure a page for layout. But unlike printed tables, HTML tables can contain information that is dynamic, or even interactive, such as the results of a database query.

 

Tables are used on web sites for two major purposes:

 



The obvious purpose of arranging information in a table

The less obvious - but more widely used - purpose of creating a page layout with the use of hidden tables.

Using tables to divide the page into different sections is an extremely powerful Tool. Almost all major sites on the web are using invisible tables to layout the pages.

 

The most important layout aspects that can be done with tables are:

 

Dividing the page into separate sections. An invisible table is excellent for this purpose .

Creating menus. Typically with one color for the header and another for the links following in the next lines.

Adding interactive form fields. Typically a gray area containing a search option .

Creating fast loading headers for the page. A colored table with a text on it loads like a bullet compared to even a small banner.

Easy alignment of images that have been cut into smaller pieces.

A simple way to allow text to be written in two or more columns next to each other.

 

BASIC TABLE TAGS

 

In order to create a table in a Web page, firstly the structure of the table is to be defined using TABLE tag in HTML. The Beginning and end of a table are marked using <TABLE>and </TABLE> tags. When the structure of a table is created <TR> tag is used to add a row in the table. It marks the beginning of a row. The closing tag is optional but you are advised to include all closing tags in order to enhance the readability of the code. In the absence of closing tag, debugging of the code may be frustrating. Instead of writing the HTML head, body etc., tags again and again, the table creation is explained using the table tags only.

Now our table has a single row and it looks like below

 

<TABLE>

<TR>

 

……………….

………………

 

</TR>

<TABLE>

 

An empty row has been created and now we need to have some table cells so that we can put something in those cells. There are two types of cell tags used in a table.

1. <TH>: Marks a heading cell within a row. It is similar to the heading tag <HI.. ..H6> on a web page and is used to give a proper heading to the columns of the table. This tag is optional and you can create tables without using <TH>tag.

2. <TD>: It is a data cell tag and used for the body of the table. Again the closing tags </TH> and </TD> are optional. Let us include header and data cells into our sample table and put some data to make it complete. The lines of code illustrate this concept:

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE >

<TR>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TO>THOMAS A. POWELL</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>AWAD</TD>

<TR>

</TABLE>

</BODY>

</HTML>

 

SETTING TABLE BORDERS

 

Tables with borders are much easier to read and are more attractive. You can specify a table border by using "border = number" attribute in the opening table tag. The number specifies the width of the border around the table in pixels. The default value is generally zero. Most of the browsers display table borders with 3D-effect. The following code shows the use of border attribute in a table.

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=7>

<TR>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>AWAD</TD>

<TR>

</TABLE>

</BODY>

</HTML>

 

Also the color of the border can be set using the “border color” attributes in <TABLE> tag. It accepts either an RGB number or an accepted color name. The following code shows the use of border color attribute in a table as:

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=7 BORDERCOLOR=”PINK”>

<TR>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>AWAD</TD>

<TR>

</TABLE>

</BODY>

</HTML>

 

INCLUDING BACKGROUND COLOR

 

You can even set background color for the whole table, row or for a table cell (data or header). The background color can be set for the whole table by using bgcolor = color" attribute in the <TABLE>Tag.

In order to set different background colors for a particular cell or a row, you can use the bgcolor attribute with <TE> and <TR> tags.

The following code sets pink color for the first row and blue color for the cell containing Text "AWAD".

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 BORDERCOLOR=”PINK”>

<TR BGCOLOR=”PINK”>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

<TH>COST</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

<TD>RS.200</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>BGCOLOR=”BLUE “>AWAD</TD>

<TD>RS.150</TD>

</TR>

<TR>

<TD>ANSI C</TD>

<TD>BALAGURUSWAMY</TD>

<TD>RS.250</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

DELETING ROWS AND COLUMNS

 

To delete a row just delete the related <TD> and <TR>tags enclosing that row. Instead of deleting the associated lines of code; you can also make them as comments. Any text or code included between <- and -> will act as a comment. A comment can be placed anywhere in the document and the browser will ignore everything within the brackets.

 

You can insert hidden messages, notes to yourself, write a helpful message or you can even put HTML tags in a comment and they will be ignored. On the similar basis columns can be deleted from a table.

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 BORDERCOLOR=”PINK”>

<TR BGCOLOR=”PINK”>

<TH>TITLE OF BOOKS</TH>

<!-- <TH>AUTHORS</TH> à

<TH>COST</TH>

</TR>

<TR>

<TD>DHTML</TD>

<!-- <TD>THOMAS A. POWELL</TD> à

<TD>RS.200</TD>

</TR>

<TR>

<TD>SAD</TD>

<!-- <TD>BGCOLOR=”BLUE “>AWAD</TD> à

<TD>RS.150</TD>

</TR>

<TR>

<TD>ANSI C</TD>

<!--  <TD>BALAGURUSWAMY</TD> à

<TD>RS.250</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

A particular size of the table can be specified using “Width" and “height" attributes in the <TABLE> tag. Both “width" & “height" attribute accept either number of pixels or the percentage of the browser's window width. For example, a table 200 pixels high is specific as height = “200" 'whereas height = “20%" reserves twenty percent of the available browser height to that particular table. Also align attribute can be used to specify the position of the table in the browser window.

 

This attribute is to be included within the opening <TABLE> tag and it tabs either left right or center values. By default tables are aligned to the left of the browser's window. The following code will show the use of width, height and align attributes.

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 BORDERCOLOR=”PINK” WIDTH = “100” HEIGHT = “200” ALIGN=”CENTER”>

<TR BGCOLOR=”PINK”>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

<TH>COST</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

<TD>RS.200</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>BGCOLOR=”BLUE “>AWAD</TD>

<TD>RS.150</TD>

</TR>

<TR>

<TD>ANSI C</TD>

<TD>BALAGURUSWAMY</TD>

<TD>RS.250</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

INCLUDING IMAGES WITHIN TABLES

 

An image can either be included in a table cell or can act as a background in a table:

1. To add a background image: Use "background" attribute in the opening <TABLE>tag. It will take the URL of the image as its value and the background images are titled throughout the available table space.

Let’s include "shed in field. if " image file as a table background image in Code as:

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 BACKGROUND=”d:\shed in field.gif” WIDTH = “100” HEIGHT = “200” ALIGN=”CENTER”>

<TR BGCOLOR=”PINK”>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

<TH>COST</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

<TD>RS.200</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD>BGCOLOR=”BLUE “>AWAD</TD>

<TD>RS.150</TD>

</TR>

<TR>

<TD>ANSI C</TD>

<TD>BALAGURUSWAMY</TD>

<TD>RS.250</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

2. To include images within Cells: Images can be included within header or data cells. For that add <IMG> attribute between the <TD> or <TH> cell tags with proper URL specifying the source of the image. The following code shows the insertion of an image in a specific cell.

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 BORDERCOLOR=”PINK” WIDTH = “100” HEIGHT = “200” ALIGN=”CENTER”>

<TR BGCOLOR=”PINK”>

<TH>TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

<TH>COST</TH>

</TR>

<TR>

<TD>DHTML</TD>

<TD>THOMAS A. POWELL</TD>

<TD>RS.200</TD>

</TR>

<TR>

<TD>SAD</TD>

<TD><IMG SRC=”C:\WINDOWS\HLPGLOBE.GIF” HEIGHT=68 WIDTH=55>AWAD</TD>

<TD>RS.150</TD>

</TR>

<TR>

<TD>ANSI C</TD>

<TD>BALAGURUSWAMY</TD>

<TD>RS.250</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

SPANNING ROWS AND COLUMNS

 

Two attributes "rowspan" and "colspan" are used to combine adjoining cells into larger cells. These attributes are used in either <TH> or <TD> tags and accepts a number as value. For rowspan attribute this number indicates the number of rows a cell should take up and for colspan the number of columns the cell should span. In both the cases the present cell is included in counting.

 

You should note that when you are using these attributes, the neighboring cells are not eliminated. They are just concealed and the acquiring cell uses their space. For example, rowspan = 4 indicates that four cells are joined down the row including the current cell. Firstly we will span rows in a table: The layout of the code can be built.

As you see, each of the two cells having contents "Numerical Methods" "ANSI C" are stretched over two rows. So we need to specify rowspan = 2 in the corresponding <TD> tags. The final HTML code is as shown below:

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 WIDTH="100" HEIGHT="200" ALlGN="CENTER">

<TR BGCOLOR="PINK">

<TH> TITLE OF BOOKS</TH>

<TH>AUTHORS</TH>

<TH>EDITION1 </TH>

<TH>EDITION2</TH>

<TH>EDITION3</TH>

<TH>EDITION4</TH>

</TR>

<TR>

<TD ROWSPAN=2>NUMERICAL METHODS</TD>

<TD>R S SALARIA</TD>

<TD>1997</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD>C.JOSHI</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD ROWSPAN=2>ANSI C</TD>

<TD>BALAGURUSWAMY </TD>

<TD>1996</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2002</TD>

</TR>

<TR>

<TD>R S SALARIA</TD>

<TD>1994</TD>

<TD>1997</TD>

<TD>2000</TD>

<TD>2003</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

As the browser encounters rowspan = 2 in a <TD> tag, it comes to a decision that this cell will need the space of two cells vertically and in view of that it provides the space. Then it continues across the row and adds the next five cells and comes back to the next row.

 

Now the first cell of this row has already been filled due to rowspan attribute and so it has only five data cells instead of six. The same is true for colspan attribute. This attribute spans columns instead of rows. To see how it works, add these lines of code after the opening <TABLE>tag in the previous code.

 

<TR>

<TH COLSPAN=6 BGCOLOR="PINK">INFORMATION ABOUT BOOKS</TH>

</TR>

 

These lines introduce a new row at the top of our sample table and have only a single cell. The Colspan=6 is included as we need to span this cell over all the six columns of our sample table.

 

SPECIFYING CELL SIZE AND ALIGNMENT

 

It’s fine idea to specify how big each cell is. When no instructions are given to the browser, each cell may be of different size depending on the size of the cell contents. Specifying cell sizes avoids wrapping of text and makes the content easier to read. The size of a cell in a table can be adjusted using the "Width" attribute.

 

The width of a cell is specified as number of pixels or as a percentage of table width. This attribute can be used with <TH> and <TD> tags to adjust the width of cells. The following code will change the width of cells in the first rows as:

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 WIDTH="100" HEIGHT="200" ALlGN="CENTER">

<TR BGCOLOR="PINK">

<TH WIDTH=40%> TITLE OF BOOKS</TH>

<TH WIDTH=40%> AUTHORS</TH>

<TH WIDTH=40%> EDITION1 </TH>

<TH WIDTH=50%> EDITION2</TH>

<TH WIDTH=50%> EDITION3</TH>

<TH WIDTH=50%> EDITION4</TH>

</TR>

<TR>

<TD ROWSPAN=2>NUMERICAL METHODS</TD>

<TD>R S SALARIA</TD>

<TD>1997</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD>C.JOSHI</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD ROWSPAN=2>ANSI C</TD>

<TD>BALAGURUSWAMY </TD>

<TD>1996</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2002</TD>

</TR>

<TR>

<TD>R S SALARIA</TD>

<TD>1994</TD>

<TD>1997</TD>

<TD>2000</TD>

<TD>2003</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

Not only the size of a cell can be adjusted but we can even control where in the cell the data and other contents will appear. This can be done by setting the alignment of data within the cell. The cell contents can be aligned either horizontally or vertically. The horizontal alignment can be set to left, right, center and vertically alignment to top, middle, bottom.

 

There are mainly two attributes It align" and "valign" for specifying horizontal and vertical cell alignments. By default the table contents are aligned (left, middle) and the table headings are aligned (center, middle) in the cell. You can specify these alignments for a row or for a cell.

 

When used with <TR>tag, the values specified in these attributes will be applicable to all the cells in that particular row. But if a <TD> or <TH> cell in that row has its own specific alignments, then these alignments will override the earlier alignments in the <TR>tag. The following code shows the use of these attributes in a table as:

 

<HTML>

<HEAD>

<TITLE> TABLE TAGS</TITLE>

</HEAD>

<BODY>

<TABLE BORDER=5 WIDTH="100" HEIGHT="200" ALlGN="CENTER">

<TR BGCOLOR="PINK">

<TH WIDTH=170%> TITLE OF BOOKS</TH>

<TH WIDTH=89%> AUTHORS</TH>

<TH WIDTH=170%> EDITION1 </TH>

<TH WIDTH=28%> EDITION2</TH>

<TH WIDTH=156%> EDITION3</TH>

<TH WIDTH=156%> EDITION4</TH>

</TR>

<TR>

<TD ROWSPAN=2>NUMERICAL METHODS</TD>

<TD>R S SALARIA</TD>

<TD>1997</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD>C.JOSHI</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2001 </TD>

<TD>2003</TD>

</TR>

<TR>

<TD ROWSPAN=2>ANSI C</TD>

<TD>BALAGURUSWAMY </TD>

<TD>1996</TD>

<TD>1998</TD>

<TD>1999</TD>

<TD>2002</TD>

</TR>

<TR>

<TD>R S SALARIA</TD>

<TD>1994</TD>

<TD>1997</TD>

<TD>2000</TD>

<TD>2003</TD>

</TR>

</TABLE>

</BODY>

</HTML>

 

You can observe that heading row has default alignment i.e. (center, middle). Now next row is being aligned as (center, bottom). The third row is aligned (left, top) but the last cell of this row is aligned (center, middle) and this alignment override the earlier (left, top) alignment. The third row is aligned (right, middle) and the last row has the default alignment i.e. (left, middle).





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us