Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » HTML » Table tags in HTML

Table tags in HTML

In this article you will learn that tables have many uses in HTML.

Author Rank :
Page Views : 2897
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



In this article you will learn that tables have many uses in HTML. The most obvious is a table of data, but tables can also be used to place graphics on a page at just the right spot or to format text and form input boxes, or simulate columns.

Table format is widely used in webpage designing and provide you with greater control over displaying of information on web page. They allow you to create boundaries that make positioning easier.

Tables

<table> </table>

Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.

Row Tag

Each table has at least one row and within that row are columns or what is also referred to as cells.

<tr>...</tr>

You can have N number of rows in your table.

Table Heading Tag

A table can have heading. Headings in a table are defined with the help of <th> tag.

<th>...</th>

Column/Cell tag

You have columns in your table where you can have your text, image etc.

<td>...</td>

HTML coding to create a simple table:

<html
>
<
head>

    <title>Tables in HTML</title>

</
head>
<
body>

    <h1>
        How to create table in HTML</h1
>
    <table
>
        <tr
>
            <th
>
                Names
            </th
>
            <th
>
                Designation
            </th
>
            <th
>
                Salary
            </th
>
        </tr
>
        <tr
>
            <td
>
                Rocky
            </td
>
            <td
>
                Assistant
            </td
>
            <td
>
                $2300
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Peter
            </td
>
            <td
>
                Manager
            </td
>
            <td
>
                $5700
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Samantha
            </td
>
            <td
>
                Teacher
            </td
>
            <td
>
                $7600
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Catherine
            </td
>
            <td
>
                President
            </td
>
            <td
>
                $4500
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Michael
            </td
>
            <td
>
                Author
            </td
>
            <td
>
                $3700
            </td
>
        </tr
>
    </table>

</
body>
</
html>

Adding Table Borders

You can create border for the table. You have include border attribute in the opening table tag. You can also add color attribute to the border. So in the above table the code would be adjusted accordingly:

<html
>
<
head>

    <title>Tables in HTML</title>

</
head>
<
body>

    <h1>
        How to create table in HTML</h1>
<table border="2" bordercolor="blue">
        <tr
>
            <th
>
                Names
            </th
>
            <th
>
                Designation
            </th
>
            <th
>
                Salary
            </th
>
        </tr
>
        <tr
>
            <td
>
                Rocky
            </td
>
            <td
>
                Assistant
            </td
>
            <td
>
                $2300
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Peter
            </td
>
            <td
>
                Manager
            </td
>
            <td
>
                $5700
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Samantha
            </td
>
            <td
>
                Teacher
            </td
>
            <td
>
                $7600
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Catherine
            </td
>
            <td
>
                President
            </td
>
            <td
>
                $4500
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Michael
            </td
>
            <td
>
                Author
            </td
>
            <td
>
                $3700
            </td
>
        </tr
>
    </table>

</
body>
</
html>

Adjusting Table Cell Spacing and Cell Padding

You can increase the space within the table cells and the space between the cells by using the cell padding and cell spacing attributes.

The cell spacing attribute adjusts the space between the cells and cell padding adjusts the space within (around) the cell.

<
table border="2" bordercolor="red" cellspacing="5" cellpadding="5">

Implement this line code into your previous code and see the change:
<html
>
<
head>

    <title>Tables in HTML</title>

</
head>
<
body>

    <h1>
        How to create table in HTML</h1>
<table border="2" bordercolor="red" cellspacing="5" cellpadding="5">
        <tr
>
            <th
>
                Names
            </th
>
            <th
>
                Designation
            </th
>
            <th
>
                Salary
            </th
>
        </tr
>
        <tr
>
            <td
>
                Rocky
            </td
>
            <td
>
                Assistant
            </td
>
            <td
>
                $2300
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Peter
            </td
>
            <td
>
                Manager
            </td
>
            <td
>
                $5700
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Samantha
            </td
>
            <td
>
                Teacher
            </td
>
            <td
>
                $7600
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Catherine
            </td
>
            <td
>
                President
            </td
>
            <td
>
                $4500
            </td
>
        </tr
>
        </tr
>
        <tr
>
            <td
>
                Michael
            </td
>
            <td
>
                Author
            </td
>
            <td
>
                $3700
            </td
>
        </tr
>
    </table>

</
body>
</
html>

Specifying a Table Width

You can specify the width of a table by using either a percentage or a pixel width.

<table
width="100%" border="2" bordercolor="red" cellspacing="5" cellpadding="5">

When you set the width of the table as 100% it will take the entire screen space and the columns will be adjusted evenly. Use the previous code and make the changes and see the output.

Specifying a Table Height

You can also set the table height by adding the height tag to the table code.

<table width="100%" height="500" border="2" bordercolor="red" cellspacing="5" cellpadding="5">

You can specify the table width and height in percentage of pixel. Mostly people prefer percentage because it will adjust accordingly irrespective of the monitor size.

Setting Column Widths

It is not always necessary that you would want all the columns of same size. You can change the column width of individual column.

<td width="30%">Rocky</td> <td width="50%">Assistant</td> <td width="20%">$2300</td>
Add the above code in one row and see the output changes. You can specify the width either in percentage or pixel.


Column/cell tag and its attributes

You can also use other attributes in the column/cell tag, such as:

align: sets your text or image to left, center or right

valign:
sets your content vertically to top, center or bottom

rowspan:
expands over multiple rows

colspan:
expands across multiple columns/cells

Align attribute

Leaving your text or image to the left of your column is usually pretty boring, so the align attribute lets you center things, using "center" or place them to the right, using "right".

<th align="center">Names</th> <th align="right">Designation</th> <th align="left">Salary</th>
In the above code I have include all three horizontal alignment left, right and center. By default the alignment is left.

Valign attribute

When placing copy in a column, the browser will automatically place it in the center of the column. You can change this default by using the valign
 attributes of "top", "center" or "bottom".

<td valign="top">Rocky</td> <td valign="bottom">Assistant</td> <td valign="center">$2300</td>
In the above code I have include all three vertical alignment top, bottom and center. By default the vertical alignment is center.

Row Span

rowspan is used to expands over multiple rows

<td rowspan="2" width="30%" valign="top">Rocky</td> <td width="50%" valign="bottom">Assitant</td> <td width="20%" align="center">$2300</td>
Column Span
colspan expands across multiple columns/cells

<td colspan="2">Michael</td> <td>Author</td> <td>$3700</td>

Hope this article would have helped you in understanding table and now you can use it to full effectiveness.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
good!!article by Liu On June 26, 2009
good!!1
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.