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

  • Home
  • SEARCH
  • 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 6158653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:59:37+00:00 2026-05-23T20:59:37+00:00

CSS is not really my thing and when I see position relative, static or

  • 0

CSS is not really my thing and when I see position relative, static or absolute my head starts to spin 🙁

So the question is: I have a DIV and inside it I need to put an arbitrary number of <div>s aligned horizontally. This will be done in runtime since I dont know the amount of DIVs nor the width for each one at design time.
For example lets say I have the following array of DIVs to add:

_aDIVs = [[40, '#red'], [10, '#green'], [40, '#blue'], [10, '#brown']];
  • First DIV needs to fill from 0% to 40% of parent DIV and the BG color is red
  • Second from 41% to 50% with BG color green
  • Third from 51% to 90%, blue
  • Fourth from 91% to 100%, brown

Each DIV will have a different background color, the % are know only at runtime. It’s important to use % because otherwise the filling will break when the browser window re-sizes. The idea is something like this:

|--------------------- PARENT DIV ---------------------|

|------RED------||-GREEN-||-----BLUE-----||-BROWN-|

(sorry for my drawing skills!)

Right now, the parent DIV is using position absolute, here’s the exact declaration:

div id="slider-addon" style="height: 5px; position: absolute; bottom: 0px; width: 100%; background-color: red; ">

I don’t care if there is an obscure trick where the first DIV fill 100% but then it get overlapped by the next <div> and so on, as long the trick is cross-browser compatible (need to work on IE, FF & Chrome)

Also, if there is a way to have multiple background colors horizontally aligned on the parent DIV that will accomplish the same effect as long as the fill % can be specified.

Thanks!

UPDATE:
Solved with ShankarSangoli method, here’s the solution with a minor correction:

var _aDIVs = [[40, '#3399FF'], [10, '#33CC33'], [40, '#FFCC00'], [10, '#CC66FF']];

var $sliderDiv = $("#slider-addon");

$.each(_aDIVs, function(i, val) {

$sliderDiv.append(

        $('<div></div>').css({ 'float': 'left', 'width': val[0] + '%', 
              'background-color': val[1], 'height': '5' })

);

});

Thank you 🙂

  • 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-05-23T20:59:37+00:00Added an answer on May 23, 2026 at 8:59 pm

    You cannot have multiple background colors to any element. To accomplish your goal you can try something like this. To set the background color and other styles you can use css as below

    #red{
      background-color: red;
    }
    
    #blue{
      background-color: blue;
    }
    
    var $sliderDiv = $("#slider-addon");
    var sliderWidth = $sliderDiv.width();
    
    $.each(_aDivs, function(i, val){
       $sliderDiv.append($(val[1]).css({float:"left":width:parseInt((sliderWidth*val[0])/100)+"%"}));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The CSS syntax highlighting in vim is not entirely optimal. For example: div.special_class stops
I'm pretty new to Jquery and i want to have a hidden div css:
CSS is not my strong suit, and I am not sure what's going on
Honestly I'm very bad at CSS and not good at dealing with color /
I just wonder if it is possible to only use CSS but not javascript
Is it OK if everything looking OK but HTML and CSS are not valid
I am not a CSS expert (mainly because I haven’t had the need to
What is the practical benefit to keep CSS 100% validated (not using any hacks),
I'm not expert about css menus. But I know basic system to make css
I know XHTML CSS but PHP knowledge is not much and i want to

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.