spacer

Table Width and Alignment

 
<table
width="pixels or percentage"
align="left, center or right">
...</table>
 
spacer
Sets the width of your table
Where to position your table
 


By default, web browsers will define the width of a table as the minimum width required to hold the table's contents. You can, however, apply the width attribute to the table element to explicitly set the width of your table. Also, providing the table does not fill or exceed the width of the available space (e.g. your web page), you can use the align attribute to set its alignment. These attributes are detailed below:

  • width="percentage or pixels" ~ Using the width attribute, you can explicitly set the width of your table. You may define either a fixed width in pixels or a relative width which will size your table according to a percentage of the available space (typically the entire width of your web page).

    When defining a width in pixels, keep in mind the available space on your web page with respect to the screen resolution. For example, a table using a width="900" attribute-value pair thus displaying it 900 pixels wide will outsize all screens using a resolution of 800 x 600. This will necessitate the insertion of horizontal scroll bars at the bottom of the user's web browser and may create an unattractive display.

    To avoid this predicament, you should either specify a fixed width that can fit within the screen resolution most visitors to your web page are likely to be using or you should define your tables in terms of a percentage of available space. When using percentages as values for the width attribute, web browsers will automatically resize your table according to available screen space defined by the user's screen resolution. Thus a width="50%" attribute-value pair will display a table at approximately 400 pixels in width on 800 x 600 screen resolution while displaying a table approximately 512 pixels in width on 1024 x 768 screen resolution. Using percentages creates what is known as a 'liquid' display which accomodates itself to all screen resolutions.

  • align="left, center or right" ~ Assuming that your table does not fill or exceed all the available space on your web page (see above), you can use the align attribute to position it to the left, in the center or to the right on your web page. When aligning a table to the left or right, the block element containing the table will wrap text around it accordingly (please see Aligning Graphics for a complete discussion on how text flows around elements using the align attribute).


    Example 1 defines a table that represents 50% of the available space and is aligned to the right (for demonstrational purposes, the width of the boxes containing the results represents the width of available space on your web page):

    Example 1 - SOURCE CODE

    <table border="5" width="50%" align="right">
       <tr><td>Cell content... </td></tr>
    </table>



    Example 1 - RESULT

    Cell content...





    Example 2 defines a table that is 300 pixels in width and is aligned to the center:

    Example 2 - SOURCE CODE

    <table border="5" width="300" align="center">
       <tr><td>Cell content... </td></tr>
    </table>



    Example 2 - RESULT

    Cell content...





There are even more attributes that you can use to configure the display of your table which apply specifically to table cells. For example, you can control the amount of blank space that appears in and about your cells and you can also vertically or horizontally align the content of each cell...