该问题的CSS解决方案:
版本 2
#a{
width: 400px;
height: 120px;
font-family: sans-serif;
text-transform: uppercase;
color:#fff;
background: #222;
padding: 200px 5px 0 5px;
}
#a>span{
display: block;
float:left;
font-size: 20px;
background: rgba(255,111,111,0.7);
padding: 10px;
margin: 5px 0;
}
将每个单词包装在 span 标签中
<div id="a">
<span>word</span>
<span>word</span>
<span>word</span>
<span>word</span>
<span>word</span>
<span>word</span>
<span>word</span>
</div>
可以用一点 JS 来完成
最简单的代码,给你的想法:
'<span>'+text.split(' ').join('</span><span>')+'</span>'
版本 1
它在换行符处缺少填充,但接近您想要的:
#a{
width: 400px;
height: 400px;
background: #222;
}
#a>h1 {
font-family: sans-serif;
display: inline;
font-size: 20px;
line-height: 50px; /*this does the trick*/
text-transform: uppercase;
color:#fff;
background: rgba(255,111,111,0.7);
padding: 10px;
}
和html:
<div id="a">
<h1>Lol'em ipsum doll or e-man and again lol'em ipsum doll or e-man</h1>
</div>