Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:24:19+00:00 2026-06-06T14:24:19+00:00

I have a piece of CSS like so: .formTable:nth-child(odd) { background-color: #eeb; } I

  • 0

I have a piece of CSS like so:

.formTable:nth-child(odd)
{
    background-color: #eeb;
}

I also have a form with the following basic structure (obviously not the complete HTML). Of note is the dynTable1 that is loaded with an AJAX call in certain circumstances.

<table class="formTable" id="table1" />
<table class="formTable" id="table2" />
<table class="formTable" id="table3" />
<table class="formTable" id="table4" />
<div id="divDynamic1" class="fadeIn">
    <table class="formTable" id="dynTable1" />
</div>

the thing that doesn’t make sense to me is, the nth-child selector will key against table2, table4 and dynTable1 on Firefox and IE9 (I did not try others). I would have expected dynTable1 to not have the style applied to it. Clearly my understanding of how nth-child works is lacking, but while my Google-fu uncovered many examples of how to use nth-child, I had difficulty finding a simple explanation of how it worked in this sort of case. So, I turn to SO.

Why does nth-child behave like this, and how can I make it alternate strictly between table elements of class formTable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:24:21+00:00Added an answer on June 6, 2026 at 2:24 pm

    :nth-child(odd) is applied to elements that are 1st, 3rd, 5th, etc children of their own parent container, not their children. I’m guessing there are some other elements before the .formTables in their parent container? That’s why every second table matches the rule, and #dynTable1 matches because it is the 1st child of #divDynamic1.

    EDIT: Now that I understand what you’re trying to achieve, I can suggest something like this:

    .wrapper > div > .formTable
    {
        background-color: #ccc;
    }
    
    .wrapper > div:nth-child(odd) > .formTable:nth-child(odd)
    {
        background-color: #eeb;
    }
    
    .wrapper > div:nth-child(even) > .formTable:nth-child(even)
    {
        background-color: #eeb;
    }
    ​
    

    What it does is reset the background color of the tables inside the divs, then specifies new alternation rules for the tables inside them – starting from gray if the div is an even child, and yellow if it is an odd child.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following piece of code: $(document).ready(function(){ $(#cont).hide(); $(#slide).show(); $('#slide').click(function(){ $(#cont).slideToggle(); $(#slide).css(background-image, url('img/slideup.png')); });
I have the following piece of code: $(document).ready(initialize); function initialize() { $('#btnPoint').css('width', $('#btnPoint').width()); }
I have the following CSS, What im trying to do is have the background
I have a small working piece. HTML: <div id=it>X</div> CSS: #it { background: blue;
I have the following piece of jQuery $(document).ready(function(){ var vH=$('#background').height(); var vW=$('#background').width(); var vT=$('#background').offset().top;
I have a piece of code like this: <div id=container runat=server> <div id=parent runat=server>
I have a custom piece of Javascript which I would like to run on
I have four tabs created by a piece of coding and I would like
I have a piece of code working in knockout.js like this: <div> ... some
I have some code in HTML and CSS where HTML looks like this: <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.