Text width not applying in span

前提是你 提交于 2019-12-12 23:27:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!