InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

jQuery tutorials

    jQuery Ajax

    • jQuery JSONP
    • jQuery .post()
    • jQuery .load()
    • load script with cache enabled
    • jQuery .ajax() file upload

    jQuery CSS

    • change table cells color based on value
    • check if element is visible in viewport
    • display auto fade out message
    • fitvids.js for automatically resizing videos
    • get class list of an element
    • jQuery toggleClass() examples
    • make a div stick at top on scroll
    • table with alternate color rows

    jQuery Events

    • Javascript/jQuery - disable right click
    • jQuery - text input field - change keyup and paste events
    • trigger click using jQuery or Javascript

    jQuery DOM

    • jQuery html() vs text()
    • Check if an element exists
    • find total DOM elements
    • jQuery encode string to html entities
    • jQuery - read and modify iframe content
    • jQuery - create iframe with content
    • jQuery - find select element selectedIndex, value and text
    • jQuery - get checkbox value and checked state
    • jQuery - get javascript object

    jQuery Cookbook

    • get jQuery version
    • jQuery - add text to existing div
    • jQuery sliding effect - slideUp, slideDown, slideToggle

    jQuery UI

    • jQuery UI make element movable
    • jQuery ui slider and input text box - one way binding
    • jQuery ui slider and input text box - two way binding
     
    • Home
    • > Tutorials
    • > Javascript
    • > jQuery

    HTML table with alternate color rows

    By admin on Dec 12, 2015

    We often need to display a table with alternate rows having different colors. We can do it either using CSS3 nth-child selector or using jQuery.
    table-with-alternate-color-rows

    Alternate color rows using CSS3 nth-child selector

    We’ll using nth-child(odd) and nth-child(even) selector.

    tr:nth-child(odd) {background-color: gray;}
    tr:nth-child(event) {background-color: lightgray;}

    Here is the code snippet with a simple table and css using nth-child selector for creating alternate color rows table.

    <style type="text/css" media="screen">
      #t1 tr:nth-child(odd) {background-color: gray;}
      #t1 tr:nth-child(even) {background-color: lightgray;}
    </style>
    <table id="t1">
    <tr><th>header1</th><th>header2</th>
    <tr><td>val11</td><td>val12</td>
    <tr><td>val21</td><td>val22</td></tr>
    <tr><td>val31</td><td>val32</td></tr>
    </table>
    refresh done
    try it online

    Alternate color rows using jQuery

    Here is the code snippet using jQuery.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {
        $("#t1 tr:odd").css("background-color", "gray");
        $("#t1 tr:even").css("background-color", "lightgray");
      });
    </script>
    <table id="t1">
    <tr><th>header1</th><th>header2</th>
    <tr><td>val11</td><td>val12</td>
    <tr><td>val21</td><td>val22</td></tr>
    <tr><td>val31</td><td>val32</td></tr>
    </table>
    refresh done
    try it online

    Comparing css3 vs jQuery approach

    1. CSS3 approach may not work with old browsers which do not support CSS3 (e.g. IE8). This is not a big concern though as most popular browsers support CSS3.
    2. jQuery approach has to be re run in case a row is added dynamically in the middle.
    3. Row with th (header) is also considered as a row when deciding even/odd.

    Suggested posts:

    1. Mac – open Finder from terminal – quick tip
    2. How to detach Chrome developer tools window
    3. CSS border-width – set element border width of four sides
    4. CSS color values
    5. wget handy commands
    6. CSS color
    7. Jquery – change table cells color based on value
    8. CSS outline – create outline around an element
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS3, HTML, Javascript, jQuery, Tutorials, Web Development
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress