22

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
  • 204,283
  • 31
  • 260
  • 272
hucmarcot
  • 311
  • 1
  • 2
  • 9

2 Answers2

61

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
  • 204,283
  • 31
  • 260
  • 272
  • 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
  • thank you, more on `inline-block` at the excellent CSS-Tricks site: https://css-tricks.com/when-do-you-use-inline-block/ – Kai Carver Jan 30 '23 at 06:38
-1
 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
  • 772
  • 5
  • 12