问题
I can't see how to fix the span width so it doesn't wobble:
<!DOCTYPE html>
<html>
<script>
function go() {
x='this message will self-destruct in five seconds.......';
xl=x.length;
x+=x;
n=0;
setInterval('ticker.textContent=x.slice(n++,n+10);if (n>xl-10) n=0;',100);
}
</script>
<body onload='go();'>
<span style='background-color:red;width:100pt' id='ticker'></span>
</body>
</html>
A link to JSFiddle.
回答1:
the span
element is an inline
element, this means its width
and height
are controlled by the content, you need to use either display:inline-block
or display:block
depending on the wanted interaction with other elements.
回答2:
display as "block":
<span style='background-color:red; width:65pt; display: block' id='ticker'></span>
回答3:
You can wrap it in a containing div also and apply the styling to the div..
http://jsfiddle.net/w9ccq96x/1/
<div style="background-color:red;width:100px;overflow:hidden;"><span id='ticker'></span></div>
来源:https://stackoverflow.com/questions/28653505/text-width-not-applying-in-span