Guy like you put CSS in the question tag I’ll answer you with CSS only.
Notice that the main point here is to use the width
with the size in CH (Character). Then a width:2ch
would be the width of 2 caractéres.
Option 1:
Using direction:rtl
and overflow:hidden
in the parent you align the text to the right in a "window" in the box with 2ch
width. Need almost nothing of css.
.box {
width: 2ch;
background-color: #f00;
direction: rtl;
overflow: hidden;
font-size:3rem;
}
<div class="box">
<span class="credit-font" id="valYearCD">2036</span>
</div>
Option 2:
With this in mind you can only cover the first two digits with one element ::after
for example.
OBS: Note that it is only a css for any font size, because regardless of the font size the red box width is always 2ch
span {
display: inline-block;
width: 4ch;
position: relative;
}
span::after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 2ch;
background-color: #f00;
}
<span class="credit-font" id="valYearCD">2036</span>
<br><br>
<span style="font-size: 3rem;" class="credit-font" id="valYearCD">2036</span>
<br><br>
<span style="font-size: 6rem;" class="credit-font" id="valYearCD">2036</span>
It only changes the click? It can come by default already?
– Felipe Viero Goulart
Just change the function of onclick for onload for example.
– LeAndrade
@Felipegoulart Altered!
– Sam
@Felipegoulart I put the button with just click to illustrate
– Sam