1

Hi there can someone help me with this progress html element I need to be able to put a text value on the right side of the progress and depending the progress if decreases the text to go with that

progress {
  -webkit-appearance: none;
}

progress::-webkit-progress-bar {
   background-color: orange;
}
<progress value="60" max="100"></progress><br/>
<progress value="90" max="100"></progress>

I need it like this on the photo enter image description here

lilo
  • 185
  • 1
  • 4
  • 17
  • Do you use jquery? It may be useful here. With pure CSS, I did not find solution for it. Or generate it to inline from the backend if this is a static HTML. – DiabloSteve Mar 19 '19 at 09:34
  • Mr. Lilo .If you found the solution with my answer mark as valid to assist other people. – Saravana Mar 26 '19 at 01:11
  • Does this answer your question? [How to display data label inside HTML5 progress bar? (cross-browser)](https://stackoverflow.com/questions/41429906/how-to-display-data-label-inside-html5-progress-bar-cross-browser) – Flimm Feb 10 '22 at 15:55

4 Answers4

2

You should use a custom progress bar or a library that has an option for that. here is an example of a custom progress bar. Use Javascript to control the width and the content of the before pseudo-element

.progress{
  height:30px;
  width:300px;
  background-color:orange;
  position:relative;
}

.progress::before{
  position:absolute;
  height:30px;
  background:green;
  content:'50%';// hrere you should add the text
  top:0;
  left:0;
  width:50%;
  display:flex;
  color:white;
  align-items:center;
  justify-content:flex-end;
  padding-right:10px;
}
<div class="progress"></div>
Prakhar Thakur
  • 1,209
  • 3
  • 13
  • 38
  • If the code needs the progress bar tag and the value and max attribute, this is not a good solution I guess. – DiabloSteve Mar 19 '19 at 09:38
  • @DiabloSteve, I agree. This solution will work with some custom javascript code which depends on the situation. There seems to be no way to gracefully mutate a default progress element in HTML. the only way left to do so is using some javascript. and I think a whole js library need not be added to achieve. – Prakhar Thakur Mar 19 '19 at 09:48
2

I have updated the progress bar using pure css. Lets try with this example and comment for further information.

progress {
    -webkit-appearance: none;
}

progress::-webkit-progress-bar {
    background-color: orange;
}

.progress-bar span {
    position: absolute;
    display: inline-block;
    color: #fff;
    text-align: right;
}

.progress-bar {
    display: block;
    position: relative;
    width: 100%;
}

progress {
    width: 100%;
}
<div class="progress-bar">
    <span data-value="60" style="width: 60%;">60</span>
    <progress value="60" max="100"></progress>
</div>
<div class="progress-bar">
    <span data-value="90" style="width: 90%;">90</span>
    <progress value="90" max="100"></progress>
</div>
Saravana
  • 3,389
  • 4
  • 21
  • 37
0

You have to use bootstrap latest version and it has in-built css :

Example :

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="progress">
  <div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div>
</div>
<br><br>
<div class="progress">
  <div class="progress-bar" role="progressbar" style="width: 15%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100">10%</div>
  <div class="progress-bar bg-success" role="progressbar" style="width: 30%" aria-valuenow="30" aria-valuemin="0" aria-valuemax="100">20%</div>
  <div class="progress-bar bg-info" role="progressbar" style="width: 20%" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">30%</div>
</div>
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
0

You can do it with little bit of js and css. I would personally avoid the progress element cause of cross-browser styling issues.

Third party libraries are good but they come with a significant cost. The bloated css and js are sometimes not worth it.

Hope this helps.

<style>
.progress {
  width: 100%;
  position: relative;
  background-color: orange;
}

.bar {
  width: 0%;
  height: 30px;
  background-color: green;
  text-align: center;
  line-height: 30px;
  color: black;
}

.bar-text {
  position: absolute;
  top: 0;
  width: 100%;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: black;
}

</style>

<script>
function init() {
  var bar = document.getElementById("bar");   
  var width = 0;
  var id = setInterval(frame, 10);
  function frame() {
    if (width >= 100) {
      clearInterval(id);
    } else {
      width++; 
      bar.style.width = width + '%'; 
      document.querySelector('.bar-text').innerHTML = width * 1  + '%';
    }
  }
}
</script>

<div class="progress" id="progress">
  <div class="bar" id="bar"></div>
  <div class="bar-text">10%</div>
</div>
<br>
<button onclick="init()">Click Me</button>
Priyesh Diukar
  • 2,032
  • 1
  • 15
  • 19