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 7920031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:04:13+00:00 2026-06-03T16:04:13+00:00

Is it possible to apply one CSS declaration to more than one HTML element

  • 0

Is it possible to apply one CSS declaration to more than one HTML element using nth-child?

For example I have a nested list and I want to apply the background colour red to list item 1, green to list items 2 to 4, blue to list items 5 to 8 and yellow to list item 9 using only 4 CSS declaration.

<ul id="nav">
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a>
      <ul>
         <li><a href="#">Our Team</a></li>
         <li><a href="#">Our Goal</a></li>
      </ul>
  </li>
  <li><a href="#">Media</a>
      <ul>
         <li><a href="#">NLC in the News</a></li>
         <li><a href="#">Photos</a></li>
         <li><a href="#">Videos</a></li>
      </ul>
  </li>
  <li><a href="#">Events</a></li>
</ul>
  • 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-03T16:04:13+00:00Added an answer on June 3, 2026 at 4:04 pm

    I see 4 outer li elements here (Home, About, Media, Events). If these correspond to the 4 CSS declarations (actually rules) you’re referring to, then :nth-child() is only part of the solution:

    #nav > li:nth-child(1) {
        background-color: red;
    }
    
    #nav > li:nth-child(2), #nav > li:nth-child(2) li {
        background-color: green;
    }
    
    #nav > li:nth-child(3), #nav > li:nth-child(3) li {
        background-color: blue;
    }
    
    #nav > li:nth-child(4) {
        background-color: yellow;
    }
    

    If you were looking for a formula to apply to the nth outer li and all its sub lis, then this would be it:

    /* Of course, substitute n with the actual number */
    #nav > li:nth-child(n), #nav > li:nth-child(n) li
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to apply 2 box-shadows to a HTML element at once? I
In C# and Java, it's possible to create constant strings using one or more
Is it possible to have one CSS class reference another? Instead of rewriting all
Is it possible to apply the OpenStack using 3 machines doc, to One Node
Is it possible to apply one shadow for menu and page itself? Here is
Is it possible to apply a css(3) style to a label of a checked
Is it possible to apply (and remove) Windows group policy settings using .NET? I
Is it possible to apply a CSS(3) effect when the browser has scrolled? I
Today I faced one question in interview. Is it possible to apply inheritance concept
I'm using the WPF themes available on http://wpf.codeplex.com but I'd like to apply one

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.