I’m running a JS script with a counter which works fine. However, in order to reduce the size of the data-val
from 60,000
to 60k
the script can’t understand anything that is not an integer.
I’ve tried to add an additional <span>K</span>
to convert the numeric value so the users understand we are talking about thousands.
Because my container has a flex-direction: column
all elements I add are being stacked vertically, which is not what I want to achieve.
Have tried using display: inline-block
and also flex-direction: row
but not really working here either. I’m not sure if I could override the container CSS just for this element or if a completely different approach shall be pursued.
let valueDisplays = document.querySelectorAll(".num-itemcounter");
let interval = 1500;
valueDisplays.forEach((valueDisplay) => {
let startValue = 0;
let endValue = parseInt(valueDisplay.getAttribute("data-val"));
let duration = Math.floor(interval / endValue);
let counter = setInterval(function() {
startValue += 1;
valueDisplay.textContent = startValue;
if (startValue == endValue) {
clearInterval(counter);
}
}, duration);
});
.wrapper-itemcounter {
margin: auto;
width: auto;
padding: 10px;
position: relative;
display: flex;
justify-content: space-around;
border-radius: 10px;
}
.container-itemcounter {
width: 22vmin;
height: 22vmin;
display: flex;
flex-direction: column;
justify-content: space-around;
padding: 1em 0;
position: relative;
font-size: 16px;
border-radius: 0.5em;
border-bottom: 10px solid #00ccbd;
box-shadow: 3px 5px 5px 0px #8c9c9c;
}
<div class="wrapper-itemcounter">
<div class="container-itemcounter">
<span class="num-itemcounter" data-val="60">00</span>
<span class="metric-itemcounter">K</span>
<span class="text-itemcounter">Happy Partners</span>
</div>
</div>
3
Answers
You have to wrap the number and k span in a div and give that div flex-direction of row.
Happy Coding 😉
I saw the other answer up there, you would take theirs for CSS, but using a little trick with HTML, we can use the
<article>
tag to enduce non-breaks unless specified with<br>
.Hope this helps!
See this modification with
flex-wrap: wrap
andflex-grow: 1
: