I know this is an old question, but I wanted to add another solution that I used before I found out about mix-blend-mode .
The idea is to duplicate information in two layers: background and foreground, where the background and foreground have different background and text colors. They are identical in size and text. Between them, I use the div trim box to crop the top layer to the desired width, showing the top layer where it is not trimmed, and showing the background layer outside the crop window.
This is similar to the Two Divisions solution in the accepted answer, but uses an extra clipping field. This makes it easy to center the text, if necessary, and a simple, direct selection of colors.
HTML:
<div class='progress' id='background'> <span></span> <div class='progress' id='boundbox'> <div class='progress' id='foreground'> </div> </div> </div>
CSS (I apologize for the confusing identifier names, "background" and "foreground"):
.progress { display: block; margin: 0; padding: 10px; height: 28px; } #background { position: relative; border: 1px solid lightgray; text-align: center; font-family: Calibri, "Sans Serif"; font-size: 16pt; width: 75%; background-color: white; color: black; } #foreground { position: absolute; left: 0; top: 0; background-color: navy; color: white; } #boundbox { position: absolute; left: 0; top: 0; overflow: hidden; }
I use jQuery to programmatically set the percentage of completion and make sure that the width of the foreground matches the width of the background and that they have the same text. It is also easy to do with pure Javascript.
// Set foreground width to background width // Do this after DOM is ready $('#foreground').width($('#background').width()) // Based upon an event that determines a content change // you can set the text as in the below example percent_complete = 45 $('#foreground').text('${percent_complete}% complete') $('#background span').text($('#foreground').text()) $('#boundbox').css('width', '${percent_complete}%')
And here is the violin: Progress Bar .

I tested this in Chrome, Firefox and Microsoft Edge.
lurker Jul 04 '19 at 18:51 2019-07-04 18:51
source share