Results 1 to 3 of 3

Thread: Table Tutorial, Learn the basics of tables

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Table Tutorial, Learn the basics of tables

    Short introduction to tables:

    Tables can be used for arranging contents on a website. It's a handy toy to arrange on an easy way the contents on a website.

    Creating a simple table:
    You always start a table with the <table> tag and you always end a table with the </table> tag.
    The next thing you need to do is to create a table row. This do you do to start a table row with the <tr> tag and to end with the </tr> tag. This row contains one or more cells of information.
    The next thing you need to do is to create the cells in the table row. This do you do to start with the <td> tag and to end with the </td> tag. You place the contents of the cell between the <td> tag and the </td> tag.
    You can create as many cells you want, but each row in a table should have the same number of columns as the other rows. So if you put 3 cells in a table row you should also have 3 cells in the following table rows.



    Code:
    <table>
    <tr>
    <td>Contents of cell 1 of row 1</td>
    <td>Contents of cell 2 of row 1</td>
    <td>Contents of cell 3 of row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    <td>Contents of cell 3 of row 2</td>
    </tr>
    </table>


    You can place virtually any other HTML element into a table cell. However, tags used in one cell don't carry over to other cells, and tags from outside the table don't apply within the table.



    Code:
    <font size="5">Just some content
    <table>
    <tr>
    <td><font size="4">Contents of cell 1 of row 1</font></td>
    <td>Contents of cell 2 of row 1</td>
    <td>Contents of cell 3 of row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    <td>Contents of cell 3 of row 2</td>
    </tr>
    </table>
    </font>
    Explanation of the code:
    The font size that is set to 5 before the table isn't used in the table. There you only see that the first cell of row 1 has a font set to 4. The font size of 5 is only set for outside the table, not for inside the table. To set the font inside the table you need to put the font size in every cell you use in the table.



    How to size a table:
    You can size a table by height and by width. The size can be set by pixels or by percentage.

    Example of a table set by pixels/percentage:

    Code:
    <table height="300" width="100%">
    Explanation of the code:
    The table height is set to 300 pixels and the table width depends on the screen size. It's always set to 100% of the screen size. So if the screen has a width of 480 pixels, the table has a width of 480 pixels. If the screen has a width of 600 pixels, the table has a width of 600 pixels.

    You can also set the size of a cell. This can also be set by pixels or by percentage.


    Code:
    <table>
    <tr>
    <td width="30%">Contents of cell 1 of row 1</td>
    <td width="70%">Contents of cell 2 of row 1</td>
    </tr>
    <td>Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    </table>
    Explanation of the code:
    The width of cell 1 of row 1 is set to 25%. The width of cell 2 of row 1 is set to 75%. The width of cell 1 of row 2 is set automatically to 25% and of cell 2 of row 2 to 75%.



    If you size the cells of a table and you have also set the size of the table, make sure that the sizes are equal to each other. So if you have set the table width to 100%, make sure that the total of the cell widths is also 100%.

    Aligning and spanning a table:
    By default, anything you place inside a table cell is aligned to the left and vertically centered. You can align the contents of table cells both horizontally and vertically with the ALIGN (horizontally) and VALIGN (vertically) attributes.
    You can apply these attributes to either the <tr> or <td> tags. Alignment attributes assigned to <tr> tags apply to all cells in that row. Alignment attributes assigned to <td> tags apply only to that cell. So if you want all the cells in a row to have a vertical alignment at the top you can save yourself some coding to set the vertical alignment in the <tr> tag instead of the <td> tags of every individual cell.
    You can align a cell vertically to the top, middle or bottom of a cell. You can align a cell horizontally to the left, center or right of a cell.

    Example of a table with VALIGN and ALIGN:

    Code:
    <table>
    <tr valign="top">
    <td align="center">
    Contents of cell 1 of row 1</td>
    <td align="right">Contents of cell 2 of row 1</td>
    </tr>
    <tr align="center">
    <td valign="bottom">Contents of cell 1 of row 2</td>
    <td>
    Contents of cell 2 of row 2</td>
    </tr>
    </table>
    Explanation of the code:
    The vertically alignment of all the cells in row 1 is set to the top of the cells. The vertically alignment of cell 1 of row 2 is set to the bottom of the cell. The horizontally alignment of cell 1 of row 1 is set to the center of the cell. The horizontally alignment of cell 2 of row 1 is set to the right of the cell. The horizontally alignment of the cells in row 2 are set to the center of the cells.


    Using backgrounds and spacing in a table:
    You can give an entire table or each individual row or cell in a table its own background, distinct from any background you might use on the web page itself. You do this by placing a BGCOLOR or BACKGROUND attribute in the <table> or <tr> or <td> tags.

    Example of how to give an entire table a background colour:

    Code:
    <table bgcolor="blue">

    Example of how to give an entire table a background image:

    Code:
    <table background="image.jpg">

    Example of how to give a row of a table and a cell of a table a background colour:

    Code:
    <table>
    <tr bgcolor="green">
    <td>Contents of cell 1 of row 1</td>
    <td>Contents of cell 2 of row 1</td>
    </tr>
    <tr>
    <td bgcolor="red">Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    </tr>
    </table>

    Explanation of the code:
    The background colour of the first row is set to green. The background colour of the first cell of the second row is set to red.


    Example of how to give a row of a table and a cell of a table a background image:

    Code:
    <table>
    <tr background="image1.jpg">
    <td>Contents of cell 1 of row 1</td>
    <td>Contents of cell 2 of row 1</td>
    </tr>
    <tr>
    <td background="image2.jpg">Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    </tr>
    </table>
    Explanation of the code:
    The background image of the first row is set to image1.jpg. The background image of the first cell of the second row is set to image2.jpg. It's possible that you don't see the image of row 1, because not every browser does support that use of the background tag.

    Using cellpadding and cellspacing:
    You can control the space around the borders of a table with the CELLPADDING and CELLSPACING attributes. The CELLSPACING attribute sets the amount of space (in pixels) between table borders and between table cells themselves. The CELLPADDING attribute sets the amount of space (in pixels) around the edges of information in the cells. Setting the CELLPADDING value to zero causes all the information in the table to align as closely as possible to the table borders, possibly even touching the borders. CELLPADDING and CELLSPACING give you a good overall control of the table's appearance.

    Example of the use of cellpadding and cellspacing:

    Code:
    <table border="1" cellpadding="10" cellspacing="5">
    <tr>
    <td>Contents of cell 1 of row 1</td>
    <td>Contents of cell 2 of row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    </tr>
    </table>
    Explanation of the code:
    The amount of space between the table border and the table cells is set to 5 pixels. The amount of space between the contents in the cells and the borders of the cells is set to 10 pixels.

    Using nested tables:
    You can place an entire table inside a table cell, and that separate table can possess any and all the qualities of any table you might want to create.

    Example of a nested table:

    Code:
    <table>
    <tr>
    <td>Contents of cell 1 of row 1</td>
    <td>Contents of cell 2 of row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 1 of row 2</td>
    <td><table>
    <tr>
    <td>Contents of cell 1 of row 1 of the nested table</td>
    <td>Contents of cell 2 of row 1 of the nested table</td>
    </tr>
    </table></td>
    </tr>
    </table>
    Explanation of the code:
    If you look at cell 2 of row 2 you see that there is a nested table in that cell. That nested table exists of 1 row with 2 cells.


    Using rowspan and colspan:
    If you want a table with in 1 row just one cell and in the next row 2 cells then you need to use the colspan attribute to make the table act naturally.

    Example of a table with colspan:

    Code:
    <table>
    <tr>
    <td colspan="2">Contents of the only cell at row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 1 of row 2</td>
    <td>Contents of cell 2 of row 2</td>
    </tr>
    </table>

    Explanation of the code:
    If you look at the table then you see that there is just one cell in the first row and 2 cells in the second row.

    See here an example of the table as described above.

    If you want a table with 2 columns and in the first columns just one cell and in the next column 2 cells then you need to use the rowspan attribute to make the table act naturally.

    Code:
    <table>
    <tr>
    <td rowspan="2">Contents of the only cell in this column</td>
    <td>Contents of cell 2 of row 1</td>
    </tr>
    <tr>
    <td>Contents of cell 2 of row 2</td>
    </tr>
    </table>
    Explanation of the code:

    If you look at the table you see that there is just 1 cell in the first column and 2 cells in the second column.

    I hope that you enjoyed reading this tutorial about tables.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2005
    Location
    AT DORRS NEAR HEAVEN
    Posts
    2,074

    Default

    i enjoyed a lot here

    but y u hav stopped just with it.
    hope u add more.

  3. #3
    Join Date
    Nov 2005
    Location
    Hell
    Posts
    253

    Default

    nokia add the caption,tbody,th tags too

    they are as useful as others here

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •