20

see my screenshot, even I set width, the span width still be "auto"

enter image description here

here is my

<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>
j08691
  • 197,815
  • 30
  • 248
  • 265
hucmarcot
  • 291
  • 1
  • 2
  • 9

2 Answers2

56

Set the display property of the spans to inline-block like:

.container span {
  display: inline-block;
}
<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>

An inline element occupies only the space bounded by the tags that define the inline element (MDN).

j08691
  • 197,815
  • 30
  • 248
  • 265
  • That's just the way inline elements are designed to work. You can get more details at the link I posted in my answer as well as at https://www.w3.org/TR/CSS2/visudet.html#inline-width – j08691 Feb 20 '16 at 14:14
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>
Ashish Patel
  • 774
  • 5
  • 12