Click here to Skip to main content
11,802,442 members (70,030 online)
Click here to Skip to main content

HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows

, 16 Jun 2015 CPOL 145.2K 56
Rate this:
Please Sign up or sign in to vote.
Novel HTML5/CSS3 formatting technique applicable to web Tables and ASP.NET GridView


Tables represent quintessentially important elements in HTML5 and all previous versions as well, serving as the cornerstone data visualization tool in data-centric applications since the early days of Internet evolution. GridView is rather important data-aware web control pertinent to ASP.NET framework; essentially, it renders the underlying data structures as HTML table. Following examples demonstrate novel CSS3 styling techniques applicable to HTML5 Table element and its sections.

HTML5 Tables typically contain multiple row tags tr, which could contain either standard cell tags td, or header cell tags th. In addition to this, Tables could be enhanced with section elements: thead and tfoot tags used for the purpose of grouping header/footer content, correspondingly and tbody tag for grouping together main content of the table. Each of these sections could be styled differently via CSS, thus are adding more “granularity” to formatting options.


For the purpose of didactic clarity, the proposed solution implements exclusively HTML5 and CSS3 features; nor JavaScript, neither jQuery is used.

Proposed solution DOES NOT use any graphic files (like .jpg, .png, .bmp, etc.), thus resulting in very small digital footprint and fast web page load. The entire solution is encapsulated in a single .htm file less than 8kb in size, which is well below the typical size of any medium-quality picture file (file size could be even further reduced by removing the comment lines added for the clarity/readability).

Last but not least: solution implements pure client-side techniques without any need for round-trip to the server, thus it could run autonomously in off-line mode, pretty much like any standard downloaded application. Any major web browser on the client’s machine could serve as a platform, providing almost universal portability ranging from typical desktop – to notebooks – to tablets – to mobile devices (smart phones).


The full-functional DEMO is available at [1]: click on the image below to open Demo Table formatted via advanced CSS3 technique:

Fig 1. DEMO Table formatted using advanced CSS3 styling techniques


As mentioned above, the entire solution is encapsulated in a single text file. You can copy-paste the following code snippet (see Listing 1) using, for example, Notepad text editor, then store the file with .htm extension, and voilà - that's it! Open the file in any major browser and see it up and running.

Listing 1
<!DOCTYPE html>
<html xmlns="">
    <title>Advanced Table CSS formatting</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Author" content="Alexander Bell" />
    <meta http-equiv="Copyright" content="2011-2015 Infosoft International Inc" />
    <meta http-equiv="Expires" content="0" />
    <meta http-equiv="Cache-control" content="no-cache" />
    <meta name="Robots" content="all" />
    <meta name="Distribution" content="global" />

    <style type="text/css">
    #divContainer {
        max-width: 600pt;
        width: 90%;
        margin: 0 auto;
        margin-top: 10pt;
        font-family: Calibri;
        padding: 0.5em 1em 1em 1em;
        /* rounded corners */
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
        border-radius: 10px;
        /* add gradient */
        background-color: #ababab;
        background: -webkit-gradient(linear, left top, left bottom, from(#909090), to(#ababab));
        background: -moz-linear-gradient(top, #909090, #a0a0a0);
        /* add box shadows */
        -moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
        -webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
        box-shadow: 5px 5px 10px rgba(0,0,0,0.3);

    #divContainer h2 { color: #efefef; font-size: 1em; }

    table.formatHTML5 {
        width: 100%;
        border-collapse: collapse;
        text-align: left;
        color: #606060;

    table.formatHTML5 td {
        vertical-align: middle;
        padding: 0.5em;

    table.formatHTML5 thead tr td {
        background-color: White;
        vertical-align: middle;
        padding: 0.6em;
        font-size: 0.8em;

    table.formatHTML5 thead tr th,
    table.formatHTML5 tbody tr.separator {
        padding: 0.5em;
        background-color: #909090;
        background: -webkit-gradient(linear, left top, left bottom, from(#909090), to(#ababab));
        background: -moz-linear-gradient(top, #909090, #ababab);
        color: #efefef;

    table.formatHTML5 tbody tr:nth-child(odd) {
        background-color: #fafafa;

    table.formatHTML5 tbody tr:nth-child(even) {
        background-color: #efefef;

    table.formatHTML5 tbody tr:last-child {
        border-bottom: solid 1px #404040;

    table.formatHTML5 tbody tr:hover {
        cursor: pointer;
        background-color: #909090;
        background: -webkit-gradient(linear, left top, left bottom, from(#909090), to(#ababab));
        background: -moz-linear-gradient(top, #909090, #ababab);
        color: #dadada;

    table.formatHTML5 tfoot {
        text-align: center;
        color: #303030;
        text-shadow: 0 1px 1px rgba(255,255,255,0.3);
    <!-- CENTERED-->
    <div id="divContainer">


        <table class="formatHTML5">
            <!-- TABLE HEADER-->
                <tr><td colspan="3">DISCLAIMER: 
                    <th>Inflation Rate</th>

            <!-- TABLE BODY: MAIN CONTENT-->
                    <td>Coke&reg; Inflation Index</td>
                    <td>Yeah, it's about $2/bottle now</td>
                    <td>Gas Inflation Index</td>
                    <td><a href="" 
                    title="Yeah, pain at the pump!">Inflation Calculator</a></td>
                    <td>NY subway fare Inflation Index</td>
                    <td><a href="" 
                    title="Take a virtual Bus ride">NYC Realtime Bus Map</a></td>
                    <td>Oil (WTI) Inflation Index estimated</td>
                    <td>Post Stamp Inflation Index</td>
                    <td><a href="" 
                    target="_blank" title="You can see Paris 
                    from here">Online Geocoder</a></td>
                    <td>PC RAM (memory) Inflation Index</td>
                    <td><a href="" 
                    target="_blank" title="Rejoyce 'Intel 
                    Inside'!">Rejoyce, "Intel Insides"!</a></td>

                <tr class="separator">
                    <td colspan="3">Precious metals Price Index: shown for comparison</td>
                    <td><a href=
                    target="_blank" title="Read the article on">
                    Read the article</a></td>
                    <td>AU is Golden!</td>
                    <td>-1.86% </td>
                    <td>Element Pd</td>

            <!-- TABLE FOOTER-->
                <tr><td colspan="3">Copyright 
                &#9400 2011-2015 Infosoft International Inc</td></tr>

Design Note

In case of lengthy table, it could be recommended to put tfoot tag before tbody, so the browser would be able to render the foot before processing the rest of the rows. More reading on CSS Table Formatting available at [2,3].


  1. Advanced Table CSS Formatting (DEMO)
  2. Click/Select row in ASP.NET GridView or HTML 5 Table
  3. Fixed Table Header Atop Scrollable GridView in ASP.NET


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

President Infosoft International Inc
United States United States
Dr. A. Bell has 20+ years of Software and Electrical Engineering experience: Win/Web veteran, published 300+ articles and authored 37 inventions, credited for 10+ Enterprise level projects (>250k code lines); currently focused on .NET/WPF/C#, Javascript/jQuery, 'Big Data', AI, IoT and Mobile apps. Participated in App Innovation Contest (AIC 2102/2013) with several winning submissions. Sample projects/pubs follow:
  1. WebTV Project: Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
  2. Edumatter M12: School Math Calculators and Equation Solvers (contest winner)
  3. Engineering Calculator VOLTA-2013 (contest winner)
  4. Online 3 Fractions Calculator (#1 on Goog)
  5. Engineering Calculator VOLTA-814 for Windows
  6. Real-time NY Bus monitoring app
  7. Inflation Calculator
  8. PaydayNY-2015 Payroll Tax Calculator (Win)
  9. Multilingual Geocoder with Interactive Map
  10. Semantic Analyzer (Concordance Calculator)
  11. Prime Factoring Calculator

You may also be interested in...

Comments and Discussions

GeneralRe: what? Pin
stian.net14-Dec-11 2:27
memberstian.net14-Dec-11 2:27 
GeneralRe: Yes, in HTML4, but this example is HTML5 (see doctype) and i... Pin
stian.net14-Dec-11 2:31
memberstian.net14-Dec-11 2:31 
GeneralLike Pin
Ahmad.M.J. Alyan12-Dec-11 20:24
memberAhmad.M.J. Alyan12-Dec-11 20:24 
GeneralRe: Many thanks! Pin
DrABELL14-Dec-11 3:30
memberDrABELL14-Dec-11 3:30 
Many thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 16 Jun 2015
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid