I have this function that increases the fontsize of a container depending on the scrollwidth. But When I print the scrollWidth and clientWidth in the while loop, it stays the same.
function adjustFontSize(textField) {
let fontSize = 10; // Starting font size
textField.style.fontSize = fontSize + "px";
// Increment font size until textField no longer fits
while (textField.scrollWidth <= textField.clientWidth) {
fontSize++;
textField.style.fontSize = fontSize + "px";
}
}
This is my html. I am trying to resize the direct children of playerInfoContainer (the playerInfo classes).
<div id="playerInfoContainer">
<div class="playerInfo winner">
<div class="playerInfoName">Sané</div>
<div class="playerInfoPercentage">9 (40,9%)</div>
</div>
<div class="playerInfo">
<div class="playerInfoName">Hamm</div>
<div class="playerInfoPercentage">8 (36,4%)</div>
</div>
<div class="playerInfo">
<div class="playerInfoName">Gulászci</div>
<div class="playerInfoPercentage">5 (22,7%)</div>
</div>
</div>
This is the css. The parent container is a flexbox but even if I change it to grid it doesn’t help.
#playerInfoContainer {
position: absolute;
top: 172px;
left: 0;
width: 267px;
height: 599px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
.playerInfo {
width: 200px;
}
.playerInfoName {
font-family: UberEatsFat;
}
.playerInfoPercentage {
font-size: 25px;
font-family: UberEatsRegular;
}
I tried to set the childrens flex-basis to 0px and flex-shrink to 0, so that the width stays the same.
When I am looking in devTools the scrollwidth of the containers that exceed the clientWidth is actually greater so I am only getting the wrong readings when executing my code.