ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Create a HTML Table

Updated on October 11, 2011

What a Table is

Tables are one of the most common elements used in Web page design. A table may be used to display data or as a layout template for a Web page. Students in my introductory programming classes often try to create tables without understanding the design or layout of a table and ask "why doesn't it look right?"

To understand how a table's look on a page relates to the code of a table, one must first understand what a table is. A table is a collection of data items displayed in rows and columns; the intersection of a row and column makes up a cell. A familiar representation of a table is a spreadsheet, such as a Microsoft Excel Spreadsheet, which addresses each data element with a cell reference..

Students of my introductory programming class, which uses HTML and JavaScript as demonstration languages for lab assignments, often get confused when inserting tables by trying to make the task too complicated. Hence, I wrote this hub to reduce the confusion.

The Simplest HTML Table

In HTML, a programmer creates a table using the HTML <table> ... </table> tag pair. As mentioned in the hub Essential HTML Tags, HTML is a tag based language so the table element contains whatever content the programmer places between the <table></table> tag pair.

Since a table is a collection of rows and columns, the programmer needs some method to identify those rows and columns. To identify those rows and columns, use the HTML <tr> ... </tr> and <td> ... </td> tag pairs. The <tr></tr> tag pair represents a table row and the <td></td> tag pair represents table data, which contains the content of a specific table cell.

The programmer places the <td></td> tag pair inside the <tr></tr> tag pair. According to the above explanation, the simplest HTML table would resemble the following code snippet:

<table>
  <tr>
    <td>My One Cell Table</td>
  </tr>
</table>

The above code when inserted in a Web page will render a very simple table containing one row with one data element as pictured to the right.

However, the table does not look like a table but like a simple page with one line of text. There are four reasons for this representation:

  • the table rendered flush to the left
  • the table only contains one element
  • the table contains no grid-lines
  • the table contains no header


Please Note: All the code snippets in this hub only contain the elements being explained. If you need help displaying the examples in a browser, consult the hub Essential HTML Tags.

Position the Table

Let us clean up the look of the above table a bit by positioning the table. One method to position the table is to use the HTML <center>... </center> tag pair. This tag pair centers all the elements between the tags so if we encapsolate our simple table code within the <center> tag pair, the table will render in the center of the browser screen.

As long as we are cleaning up the look of the table, how about we add another data element to the table to begin to meat out the content somewhat? You like the idea; very well. We'll add a little more content by adding another data element using the <td> tag pair and we will center the table using the <center> pair.

<center>
  <table>
    <tr>
      <td>My One Cell Table</td><td>or maybe two cells</td>
    </tr>
  </table>
</center>

As you can see, the table to the right is horizontally centered within the display and contains a little more data. However, although coded as a table, what we see displayed appears to be a simple statement.

Tables comprise both rows and columns so adding some more rows with begin to transform the table from a simple statement to a presentation of data. We will use the <tr> tag pair to add more rows to the table and the <td> tag pair to place some data within the rows.

Table Grid Lines

These steps alone will add more content to the table but the table will still lack in presentation. Simply adding more rows and columns will appear to stack the items above one another in rows and columns but the table still needs some form of separation.

We will add table grid lines in the form of a border around the cells to provide the separation and provide a more defined appearance to our table. In order to add borders around the the table and cells we will add the border attribute to the <table> tag in the table definition along with the added rows and columns as follows:

<center>
  <table border>
    <tr>
      <td>My One Cell Table</td><td>or maybe two cells</td>
    </tr>
    <tr>
      <td>or three</td><td>or four</td>
    </tr>
    <tr>
      <td>kinda</td><td>sounds like</td>
    </tr>
    <tr>
      <td>Dr.</td><td>Seuss</td>
    </tr>
  </table>
</center>

HTML Table Headers

Now our table is starting to look like a table. The table contains multiple rows and a couple of columns and we defined the borders of the cells. Simply adding the borders ads focus to the table's presentation and helps isolate individual cells.

To further define the content of the table, we alos added a couple of headers using HTML <th></th> tag pairs. We inserted these tags within individual rows represented by the <tr> tag pairs at the top of the table.

In the next step, we will also add a little depth to the table by increasing the size of the border, which gives the table a 3d appearance. To do this, we will add the size attribute to the border attribute in the table tag.

<center>
  <table border="10">
    <tr>
      <th colspan="2">Two Column Table</th>
    </tr>
    <tr>
      <th>1st Column</th>
      <th>2nd Column</th>
    </tr>
    <tr>
      <td>My One Cell Table</td><td>or maybe two cells</td>
    </tr>
    <tr>
      <td>or three</td><td>or four</td>
    </tr>
    <tr>
      <td>kinda</td><td>sounds like</td>
    </tr>
    <tr>
      <td>Dr.</td><td>Seuss</td>
    </tr>
  </table>
</center>

Header Background Color

Now our table has some depth of field. It looks like a table and presents a 3d look. You can increase the size of the table by adding more rows and columns within the HTML <td> and <tr> tags if you wish. Also notice that we added another header at the top of the table and that the header flows across both columns. We spanned the row across the columns by adding the 'colspan="2"' attribute to the opening <tr> tag. This added header defines the table or adds a title.

But suppose we would like our table to really stand out and look more organized. To do this, we could add a background color to the header rows and possibly center the data within the cell elements. There are deprecated methods to do this within the HTML <th> and <td> tags themselves but these methods would not pass validation checks for newer versions of HTML code (more on versions to come in another hub). However, we can accomplish the task by using styles within the HTML document.

Using Style to Format HTML Tables

In order to use style to format the appearance of a HTML table, you must add some code to the header section of the HTML file within the HTML <head> .. </head> tag pair just below the HTML </title> closing tag for the title. The hub Essential HTML Tags illustrates the location of those tags within the HTML file.

For our table, we will format two types of table elements, which are the table header and table data elements; represented by the HTML <th> .. </th> and <td> .. </td> tag pairs respectively. However, we must first direct the browser to use our style for formatting. The HTML <style> ... </style> tag pair directs the browser to use a particular style, identified by the specification 'rel="stylesheet" type="text/css".' Thus, the code for the style is located between the <style> tag pair and the type of style is CSS, which stands for Cascading Style Sheet.

<style rel="stylesheet" type="text/css">
  th {
    height: 30px;
    font-weight: bold;
    text-align: center;
    background-color: aqua;
  }
  td {
    text-align: center;
  }
</style>

In the code above on line 2, the "th" identifies the table header element as the element that this block of style code controls. The block of code is everything between the left and right curly braces "{ .. }".

The "td" on line 8 identifies the table data element and it's block of code, identified in the same manner as above.

The attributes within the blocks controls the formatting so once the styles have been added to the header, those styles apply to every one of the identified elements within the Web page. Therefore, you only have to write the code for the styles one time in the header and those styles will apply to every table you create in the table.

Within the above th style, the height: attribute specifies the height of the header row in pixels, the font-weight: attribute controls the appearance of the text (bold in this case), text-align: specifies where to position the text within a cell, and the background-color:attribute controls the background color of the element. Colors can be specified using the name of a color (as long as the identified color is one recognized by web browsers) or the color values for each of red, blue, and green may be used. For instance, #ffffff would specify white, ff0000 would specify red, 00ff00 would specify blue, and 0000ff would specify green. Shades are created by adjusting each of the pair values between 00 and ff.

Our table finally exhibits some real character.

Note: There are many other style attributes that may be used for many other elements and there other ways to apply style using Cascading Style Sheets (CSS) but those methods are outside the scope of this hub.

Want to learn more? continue on to discover how to create a HTML form.

Was this hub interesting, Id love to hear from you,

Please leave a comment.


Comments

    0 of 8192 characters used
    Post Comment

    • Dumbledore profile imageAUTHOR

      This Old Guy 

      7 years ago from Somewhere in Ohio

      Thank you, thelyricwriter,

      I have been trying to find a niche to write about and in the mean time I have been publishing hubs dealing with introductory programming. More of these to come.

    • thelyricwriter profile image

      Richard Ricky Hale 

      7 years ago from West Virginia

      Voted up pal. I am marking your page. You have a lot of info on computers. A person can learn a bunch of neat stuff with your hubs. Good stuff.

    • Dumbledore profile imageAUTHOR

      This Old Guy 

      7 years ago from Somewhere in Ohio

      sohamraiker,

      You are very welcome. I will be publishing more hubs with basic HTML tips.

    • sohamrailkar profile image

      sohamrailkar 

      7 years ago from Dahanu

      Its very helpful!

      thanks dumbledore!

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)