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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:42:47+00:00 2026-06-18T06:42:47+00:00

Im having trouble with managing custom colored elements on the page. For example, we

  • 0

Im having trouble with managing custom colored elements on the page.

For example, we have 100 navigation squares on the page, each one has its own color, cant think of any way except of creating css classes for each type of color. Which will produce LOTS of css code.

Need some help with this,
Thanks

*added javascript & jquery tags as one of the possible ways of solving this question

Update:
Thanks for responses guys, feeling like I need to get into details.
Im having squared category navigation on my search page, colors can go from server side or can be stored in client’s js.
Im getting list of categories from server (lets assume im getting color for each one too)
Then Im building all squares (they are white by default, but on :hover they change their color)
So I would go for such solution:

<ul id="squares">
<li class="greencolor"></li>
<li class="redcolor"></li>
<li class="bluecolor"></li>
</ul>

with css:

#squares li.redcolor:hover{
 background:red;
}
#squares li.greencolor:hover{
 background:green;
}
#squares li.bluecolor:hover{
 background:blue;
}

Hopefully now you can see what I was talking about referring tons of css code for 100 elements.

And yes, I understand that I can go for such solution:

var colorsMap={'redcolor':'red','greencolor':'green'};     
$('#squares li').on('hover',function(e){

    $(this).css('background-color', colorsMap[$(this).attr('class')];

});

but this doesnt sound as an elegant solution to me and Im trying to find way to make it through css, not inline css changes by js

  • 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-18T06:42:49+00:00Added an answer on June 18, 2026 at 6:42 am

    Although I recommend to use CSS to achieve it, but there’s still a solution better than inline style:

    var selector = '#squares li'
      , css = []
      , style = document.createElement( 'style' )
      , colorsMap= {
          'redcolor': 'red',
          'greencolor': 'green',
          'bluecolor': 'blue'
        }
    $( selector ).each( function() {
      css.push( selector + 
                '.' + 
                // recommand to use data-attr to store color info
                // assuming `className == 'bluecolor'`
                this.className + 
                ':hover' +
                '{' +
                'background:' +
                colorsMap[ this.className ] +
                '}'
                )
    })
    style.textContent = css.join('')
    document.head.appendChild( style )
    

    By dynamic insert CSS into <head>, you still get the benefits from normal CSS, demo.

    And, you also can generate dynamic CSS file in back-end side, it’s more easier to manage colors, by a configuration form or something else.

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

Sidebar

Related Questions

Having trouble with each function... Will try to explain by example... In my code,
Having trouble figuring this one out.. I currently have a frmMain and a frmLoading..
Having trouble finding a python solution to matching elements of one list against elements
I'm working on a fairly large site and am having trouble managing z-indexes. Is
Having trouble framing this assertion in Rspec for my rails app. A User has
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I'm having trouble managing my .dll references in projects in Visual Studio. All the
Having trouble with some select statements. I have 2 tables. sms_log and sms_messages. sms_log
Having trouble with Codekit/Compass. Know lots of people have had issues with this but
My question deals with this UI sample. Having trouble with approach to managing the

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.