THis is three pic that i use input and innerHTML to show the value on the website.
but when the value on different digit,it couldn’t that the value still keep in center.
Here is the code:
const inputRange = document.querySelector("#inputRange");
const dotDisplay = document.querySelector(".dotDisplay-show"); inputRange.addEventListener("input", (e) => {
const V = inputRange.value;
console.log(V);
dotDisplay.innerHTML = V;
});
.underline{
position:absolute;
width:35px;
border-bottom:2px solid #000;
left:245px;
top:105px;
}
.dotDisplay {
color:#32608D;
position:absolute;
font-size: 20px;
font-weight: bold;
}
.dotDisplay-show{
left:255px;
top:80px;
}
<input type="range" min="0" max="200" step="1" value="0" id="inputRange">
<div class="underline"></div>
<div class="dotDisplay dotDisplay-show">--</div>
3
Answers
You could directly add the border on the element that displays the number, with a certain amount of padding.
The following solution sets
margin-left
andmargin-right
toauto
to center the 35px<div>
andtext-align: center
to center the number within it.Ok, before answer your question with a ready to copy-paste code I’ll explain what you have done wrong, so that in the next it not happens.
Basically you have two elements that are strongly related: The div ‘underline’ and the div ‘dotDisplay’.
If they are strongly related you automatically need to think that they probably need to be inside a container:
Ok, but this will not solve the problem. To solve the problem we need to change the CSS too, because the positioning code of the counter and its underline will need to be placed on the container style:
And the last thing that we need to change is the order of the elements on HTML. On your example the underline element is one line above the display element, but in the screen the UNDERline needs to be beneath. Obviously you can change this with CSS but let’s do things simply:
Now your code is right. Other things that I would change is the container positioning because now you are using absolute values, and this is a bad practice if you are thinking (and you should) on responsive web sites.
=)