Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add label inside a progress bar tag?

Tags:

I want to add a label in my progress bar tag like in this beautiful example:

enter image description here

Assuming the blue is the value and the red is the max, how can I add a label inside there like my "35"?

like image 235
Brained Washed Avatar asked Sep 17 '12 02:09

Brained Washed


People also ask

How do I add labels in progress bar?

Add text inside a w3-container element to add a label to the progress bar. Use the w3-center class to center the label. If omitted, it will be left aligned.

How do I display the progress bar in HTML?

Use the <progress> tag to create a progress bar in HTML. The HTML <progress> tag specifies a completion progress of a task. It is displayed as a progress bar. The value of progress bar can be manipulated by JavaScript.


1 Answers

Use CSS's position:relative to manoeuvre the text onto the bar.

A quick and dirty solution for a short bar is:

position: relative;
left: -100px;

where left forces the text that would be next to the bar on top of it.

In the comments, Matthias made the point that left doesn't work if the progress bar is 100% width.

It doesn't work because it forces the text below the bar. It'll fail for any width of bar sufficient to force the text below.

Instead you have to get a bit more clever and do something like:

    position: relative;
    top: -1.5em;
    margin-left: 50%;

Here the top:-1.5em replaces the left adjustment in the jsFiddle. The margin-left is an attempt to centre the text. It should actually be a little less than 50%.

If someone has a better solution to centre the text on the bar that's less hacky than the hand-wavy 50%ish margin feel free to comment on it and I'll adjust this again.

like image 97
Philip Whitehouse Avatar answered Oct 14 '22 06:10

Philip Whitehouse