You cannot make a row of characters exactly the same width as some given width, such as the entire available width, which is what you probably mean by “screen width”. But I think you meant to ask to write as many “.” characters as possible on a single line, within the constraints of available space. The constraint is really the parent element’s width, so we can achieve this by repeatedly appending “.” until we exceed that constraint, then take one step back:
<span id=dots style="color:red"></span>
<script>
var dots = document.getElementById('dots');
dots.style.visibility = 'hidden';
var width, savedContent;
do {
savedContent = dots.innerHTML;
dots.innerHTML += '.';
} while(dots.offsetWidth < dots.parentNode.offsetWidth);
dots.innerHTML = savedContent;
dots.style.visibility = 'visible';
</script>
The example uses span
instead of label
, since such text is hardly suitable for use as a label for a control. However, this choice of element does not affect this issue.
Note that the string, once created, will stay the same, even if the browser window width is changed in a manner that changes the parent element’s width (e.g. when our element is a child of body
and we have no width settings in CSS).