如何设置span的固定宽度

时间:2016-02-20 04:33:51

标签: html css

看我的截图,即使我设置宽度,跨度宽度仍然是“自动”

enter image description here

这是我的

<div class="container">
  <div class="row"><span style="width:60px;background-color: red;">prefix1</span><span>prpr</span>
  </div>
  <div class="row"><span style="width:60px;background-color: red;">pre2</span><span>prpr</span>
  </div>
</div>

2 个答案:

答案 0 :(得分:30)

将跨距的显示属性设置为内联块,如:

&#13;
&#13;
.container span {
  display: inline-block;
}
&#13;
<div class="container">
  <div class="row"><span style="width:60px;background-color: red;">prefix1</span><span>prpr</span>
  </div>
  <div class="row"><span style="width:60px;background-color: red;">pre2</span><span>prpr</span>
  </div>
</div>
&#13;
&#13;
&#13;

内联元素仅占用定义内联元素(MDN)的标记所限定的空间。

答案 1 :(得分:0)

 Just use 

<div class="container">
<div class="row" style="width:auto;background-color: red;"><span>prefix1</span><span>prpr</span></div>
<div class="row" style="width:auto;background-color: red;"><span>pre2</span><span>prpr</span></div>
</div>