html - Lining up percentage based divs within a horizontal bar graph? -


i trying create horizontal html bar graph , having issues wrapping head around layout. want have div background image 10, evenly spaced, vertical lines , have several divs within div percentage-based widths.

functionally, working expected, having difficult time figuring out how bars line vertical lines in background image.

for instance, if bar has width of 60% should align 6th vertical line.

html:

<div class="chart">     <div class="bar-container">         <div data-percentage="70" class="bar">product 1</div>         <div data-percentage="30" class="bar">product 2</div>         <div data-percentage="90" class="bar">product 3</div>     </div> </div> 

css:

.chart {     width:320px;     height:200px;     background-image:url('http://s15.postimg.org/ku83p3fvf/bars.png');     background-repeat:no-repeat;     background-size:contain;     position: relative; }     .chart .bar-container {         position:absolute;         bottom:30px;         width:100%;     }         .chart .bar-container .bar {             background:#55565a;             margin-top:5px;             color:#fff;             font-size:16px;             padding:2px 0;         } 

javascript:

$('.bar').each(function() {     percentage = $(this).attr('data-percentage');     $(this).css('width',percentage+'%'); }); 

jsfiddle

any ideas i'm overlooking here?

you have background image division of 9 bars, not ten. such, have math make widths match up. or, can make image 10 bars instead of nine. heres fixed jsfiddle, notice math done in jquery part

$('.bar').each(function() {     percentage = $(this).attr('data-percentage');     $(this).css('width',percentage / .9 +'%'); }); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -