If the width of the scroll bar is fixed (300 pixels) and the width of the text (text, not the element) is more or less fixed (about 85 pixels - from 1% to 100%), set the text as an absolutely positioned child of the .progress element and set it width and max-width :
width: calc(100% + 100px); max-width: 300px;
If you align the text to the right, it will appear after the line until max-width is reached.
var progressBar = document.querySelector('.progress'); function progress() { var minmax = [0, 100]; var step = 1; const iterate = (current) => { progressBar.style.width = `${current}%`; progressBar.setAttribute('data-percentage', current); if(current !== minmax[1]) { setTimeout(() => iterate(current + step), 40); } else { minmax = minmax.reverse(); step = -step; setTimeout(() => iterate(minmax[0]), 500); } } iterate(minmax[0]); } progress();
section#progressish { padding: 20px; width: 300px; } div#progressbar { background-color: #d1d1d1; height: 10px; margin-bottom: 15px; width: 100%; } div#progressbar>.progress[data="bar"] { position: relative; background-color: #111111; height: 10px; margin-bottom: 15px; width: 0%; } .progress::before { position: absolute; top: -20px; width: calc(100% + 85px); max-width: 300px; text-align: right; white-space: nowrap; content: attr(data-percentage)"% avklarat"; }
<section id="progressish"> <div id="progressbar"> <div class="progress" data="bar" data-percentage></div> </div> </section>
Ori Drori
source share