HTML Table
HTML Table
** If you want to see the table border looks like in the above picture,
you should replace < table border=1 > in the above HTML code,
otherwise no border will display.
Table layout structure in picture
In the above picture you can see the Table starts with < table > and end
with < /table > tag and each row start with < tr > and end with < tr/ > tag
and each cells start with < td > and end with < /td > tag.
Table Border
The HTML Table Border attribute sets the width of Border around the
table and table cells. We can set the Border attributes in pixels.
<table border="2">
In the above HTML code we set the Border attribute to 2, this applies a
border to each cell, and to the table as a whole. But whatever values you
to set in the Border attribute, only the size of the border around the table
will change, the border of the table cells remain 1 pixel width.
HTML Source Code :
<html>
<body >
<table border=5>
<tr>
<td>
January
</td>
<td>
February
</td>
</tr>
<tr>
<td>
March
</td>
<td>
April
</td>
</tr>
</table>
</body>
</html>
In the above HTML code we set border=5 , you can see only the Border
around the table will change the size to 5 , but cells border inside the
table will remain 1 pixel.
HTML Table Bordercolor
The bordercolor attribute will change the color of the border around
your table. You can directly type the color names or you can use
hexadecimal codes of colors to Boardercolor attribute.
<table border=5 Bordercolor=red>
You can set additional two border color attributes to HTML Table, they
are bordercolorlight and bordercolordark attributes. It will give you a
3D effect to your HTML Table.
<table border=5 bordercolorlight=green bordercolordark=blue>
The width value 100% indicates a width for the table that is the full
width of the browser window.
<html>
<body >
<table border=1 width=100>
<tr>
<td>
Table width is 100 pixel
</td>
</tr>
</table>
<br>
<table border=1 width=100%>
<tr>
<td>
Table width is 100 %
</td>
</tr>
</table>
</body>
</html>
The above HTML code display two tables, one is 100 pixel width and
another one is 100% width. First table is only 100 pixel width in any
changes in browser window state, while other table will always stretch
the full width of the window it is viewed in, that is the table
automatically expands as the user changes the window size when you
set width in % .
Cell Width or Column width
You can set the width of a table cell using width attribute.
<td width=30%>
The < td > width can be set either as an absolute value in pixels, or as in
percentage (%).
<html>
<body >
<table border=1 width=400>
<tr>
<td width=30%>
Cell width is 30%
</td>
<td width=70%>
Cell width is 70%
</td>
</tr>
</table>
</body>
</html>
Table Height
Height attributes can be added to the < table > tag as well as the < td >
tag.
<table height=400>
<td height=100>
You can use color names or use hexadecimal color code in bgcolor
attribute.
<html>
<body >
<tr>
<td>
Background color
</td>
</tr>
</table>
</body>
</html>
The above code will change only the particular cell color to red.
<html>
<body >
<tr>
<td bgcolor=red>
</td>
<td bgcolor=green>
</tr>
</table>
</body>
</html>
<td background="bg-red.png">
If the image dimensions are smaller than the table dimensions and there
is enough space in the table, the image will repeat.
Note : The bgcolor and background attributes are deprecated in HTML
4.01.
http://www.tagindex.net/html/table/table_background.html
visit this link
Cellspacing
The Cellspacing attribute places space, in pixels, around each cell in the
table.
<table border="1" cellspacing="5">
The above code will Set the space between the cells to 5 pixels .
<html>
<body >
<tr>
<td bgcolor=white>
January
</td>
<td bgcolor=white>
February
</td>
</tr>
<tr>
<td bgcolor=white>
March
</td>
<td bgcolor=white>
April
</td>
</tr>
</table>
</body>
</html>
In the above HTML code we set cell-spacing as 15. That means each
adjacent table cells create 15 pixels space to another. For display
purpose set the table background Color as Red and Cell background
Color as White. So you can see the cell spacing in red color.
If you want no spaces at all, you should set Cellspacing="0", otherwise
the default is Cellspacing="1" will set, even if you don't mention
Cellspacing. The below picture is the same HTML code with default
Cellsapcing, that is no Cellspacing is set or Cellspacing="1" .
The cellspacing is applied both vertically and horizontally. The cell
spacing is uniform for the entire table. Individual cell spacing between
each row or column cannot be specified.
The Cellspacing attribute is similar to the cellpadding attribute, which
control the spacing between the contents of a Cell and the Cell's border.
Cellpadding
CellPadding attribute is used to control the spacing between the contents
of a Cell and the Cell's border.
<body >
<td>
January
</td>
<td>
February
</td>
</tr>
<tr>
<td>
March
</td>
<td>
April
</td>
</tr>
</table>
</body>
</html>
Colspan
The colspan attribute defines the number of columns a cell should span
(or merge) horizontally. That is, you want to merge two or more Cells in
a row into a single Cell.
The above image shows two tables . The first HTML Table has 2 rows
and 2 columns in each row. The second HTML Table has 2 rows and 1
column in first row and 2 column in second row. In the second Table we
merge first two Cells horizontally using Colspan attribute. You can see
the second Table HTML code below.
How to colspan ?
HTML Source Code :
<html>
<body >
<tr>
<td colspan=2>
Merged
</td>
</tr>
<tr>
<td>
Third Cell
</td>
<td>
Forth Cell
</td>
</tr>
</table>
</body>
</html>
<body >
<tr>
<td>
First Cell
</td>
Merged
</td>
</tr>
<tr>
<td valign=middle>
Third Cell
</td>
</tr>
</table>
</body>
</html>
Table Align
Align attribute of Table can positioning Tables and their contents in
relation to other elements on the Web page. Align attributes can be set
in two levels, Table Alignment and the alignment of content inside the
Table Cells.
How to Align HTML Table
Alignment of the Html Table element defines the horizontal alignment
of the Table inside the next outer container. This does not control the
alignment of the contents inside the cells of a Table. You can Align an
HTML Table left, right, and center horizontally inside the next outer
container.
In the above picture you can see three tables align left, center, and right
respectively.
HTML Source Code :
<html>
<body >
<tr><td>Table 1</td></tr>
<tr>
<td>Align Left</td>
</tr>
</table>
<br><br><br>
<tr><td>Table 2</td></tr>
<tr>
<td>Align Center</td>
</tr>
</table>
<br>
<tr><td>Table 3</td></tr>
<tr>
<td>Align Right</td>
</tr>
</table>
</body>
</html>
In the above picture there are two tables , first table has three columns
and each column Align horizontally left, right and center respectively.
The second table has three columns and each column vertically align
Top, Bottom and Middle respectively.
HTML Source Code :
<html>
<body >
<tr>
<td align=Right>Right</td>
<td align=Center>Center</td>
</tr>
</table>
<br><br>
<tr height=50>
<td valign=bottom>Bottom</td>
<td valign=middle>Middle</td>
</tr>
</table>
</body>
</html>
You can also control the alignment of contents of the cells in an entire
row by using the Align and VAlign attributes to < tr > tag.
<tr align=Left>
The above code horizontally Align the contents of the cells in an entire
row to Left of the Cell.
<tr valign=middle>
The above code Vertically Align the contents of the cells in an entire
row to Middle of the Cell.
Note: The align attribute is supported in all major browsers, but it is
deprecated in HTML 4.01.
<td >
<img src="picture1.png">
</td>
</tr>
The src attribute's value can be any valid URL of an image on the Web ,
local or remote.
How to add a Link inside a Table Cell
Hyperlink is a pointer from one HTML document to another one, the
target may be same website location or some other location on the
Internet. You can add links inside the Table Cells.
<tr>
<td>
<a href="www.mywebsite.com/about.html>About</a>
</td>
</tr>
<td>
<ul>
<li>VB.Net</li>
<li>Csharp</li>
<li>Asp.Net</li>
</ul>
</td>
</tr>
<td>
<p> HTML stands for Hyper Text Mark-up Language, one of the document formats of the World
Wide Web. </p>
</td>
</tr>
Put all together inside a Table.
The above image shows a Table contains Image, Link, List and
Paragraph inside the Table Cells.
HTML Source Code :
<html>
<body >
<table border="2">
<tr >
<td>
<p> This table contain Image , link, <br> List and
Paragraph tags </p>
</td>
<td>
<img src="image.png">
</td>
</tr>
<tr>
<td>
<ul>
<li>VB.Net</li>
<li>Csharp</li>
<li>Asp.Net</li>
</ul>
</td>
<td>
<a href="www.mywebsite.com/about.html">About</a>
</td>
</tr>
</table>
</body>
</html>
How to Nested Tables in HTML
Nesting tables simply means making a Table inside another Table.
Nesting tables can lead to complex tables layouts, which are both
visually interesting and have the potential of introducing errors depends
on its nesting nature.
Tables within Tables
Nested Table always need to be placed between < td > ... < /td > tags of
the outer container Table. You can format nested tables as you would
format any other HTML Table.
The following HTML code create a Table with one row and two column
and inside the second column again create another table (nested table)
with two rows.
In the above picture the outer table with red colors and Inner table with
Green color.
HTML Source Code :
<html>
<body >
<tr>
<td>
</td>
<td>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
In the above picture the outermost table with color Red and nested table
with color Green , Yellow and Blue respectively.
HTML Source Code :
<html>
<body >
<tr><td>
<tr><td>
<tr><td>
<tr><td>
</td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>
You can format or placed other HTML Tags inside nested tables as you
would do any other HTML Table. The following HTML code create an
outer Table with two rows and each row has two columns. Each nested
table add other HTML tags like Image, Links , List , Text etc.
<body >
<tr>
<td >
<table >
<tr><td>
</td></tr>
</table>
</td>
<td >
<table >
<tr><td>
<ul>
<li>VB.Net</li>
<li>Csharp</li>
<li>Asp.Net</li>
</ul>
</td></tr>
</table>
</td>
</tr>
<tr>
<td >
<table >
<tr><td>
<a href="www.mywebsite.com/about.html">About</a>
</td></tr>
</table>
</td>
<td>
<table >
<tr><td>
<img src="image.png">
</td></tr>
</table>
</td>
</tr>
</table>
</body>
</html>
The more tables you have nested inside one another, the slower the page will load. It gets
more complicated for the browser to render, and so the page loads more slowly.
The above picture shows a common website layout. The top part is
using for display Website Name or Website Logo. Left side part is using
for Menu and right side is for Advertisements. The center position is
using for display your website content and the bottom part is for Footer
like copy right information , sitemap , about etc.
The following HTML code give you what exactly shows in the picture.
Copy and paste the following html code and save it as an html file.
HTML Source Code :
<html>
<body >
<br><br>
WEBSITE NAME
<br><br>
</td></tr>
<tr>
MENU
</td>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
</td>
ADVT
</td>
</tr>
<br><br>
FOOTER
<br><br>
</td></tr>
</table>
</body>
</html>
The above HTML code create a simple website layout and has no
nested tables. The content area is filled with HTML < br > (line break)
tag for display purpose, so you can replace < br > tags and write your
content inside the content area.
In the picture the top part is for display website logo and left down is the
content area and right side you can put Menu there and as usual footer is
the bottom location.
When you creates a website layout , it is better to avoid nested tables.
Nesting tables can lead to complex tables layouts and increase in page
load time.
The following HTML code give you what exactly shows in the picture.
Copy and paste the following html code and save it as an html file.
HTML Source Code :
<html>
<body >
<br><br><br>
WEBSITE NAME
<br><br><br>
</td></tr>
<tr>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
</td>
MENU
</td>
</tr>
<br>
FOOTER
<br>
</td></tr>
</table>
</body>
</html>
The content area is filled with HTML < br > (line break) tag for display
purpose, so you can replace < br > tags and write your content inside the
content area.
The top part is for displaying website logo and just after that display
menu as horizontally. After that a wide content area part and bottom
side is for footer area.
When you creates a website layout , it is better to avoid nested tables.
Nesting tables can lead to complex tables layouts and increase in page
load time.
The following HTML code give you what exactly shows in the picture.
Copy and paste the following html code and save it as an html file.
HTML Source Code :
<html>
<body >
<tr><td align=center>
<br><br>
WEBSITE NAME
<br><br>
</td></tr>
<tr><td align=center>
<br>
MENU
<br>
</td></tr>
<tr>
<td align=center>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
<br>
FOOTER
<br>
</td></tr>
</table>
</body>
</html>
The frame attribute of the TABLE element specifies which sides of the
outer border are displayed.
<table frame="void">
Example
The rules attribute of the TABLE element specifies which rules (inner
borders) are displayed.
<table rules="none">
Example
<thead>
<tr>
<th colspan="3">Table Header</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">Table Footer</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Row1 - Col1</td>
<td>Row1 - Col2</td>
<td>Row1 - Col3</td>
</tr>
<tr>
<td>Row2 - Col1</td>
<td>Row2 - Col2</td>
<td>Row2 - Col3</td>
</tr>
<tr>
<td>Row3 - Col1</td>
<td>Row3 - Col2</td>
<td>Row3 - Col3</td>
</tr>
</tbody>
</table>
Output
Table Header
Table Footer
Row1 - Col1 Row1 - Col2 Row1 - Col3
Row2 - Col1 Row2 - Col2 Row2 - Col3
Row3 - Col1 Row3 - Col2 Row3 - Col3