Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set fixed width for span

Tags:

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>
like image 211
hucmarcot Avatar asked Feb 20 '16 04:02

hucmarcot


People also ask

Can a span have fixed width?

The main thing to remember about a span element is that it is an inline element, which means that you will not be able to set the width or height of it as is.

How do you fix a span width?

So, how can we fix that? Well, the only solution to this problem is changing the block type of the span tag. That is, to set the width or height of a span, we have to either make it a block or inline-block level element.

How do you find the width of a span element?

By default a span element has no dimension because its CSS display property is inline. If you want to give it a width, you should change that property to inline-block. Now you can play with its dimensions.

What is a span width?

n. 1. The extent or measure of space between two points or extremities, as of a bridge or roof; the breadth. 2. The distance between the tips of the wings of an airplane.


1 Answers

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).

like image 137
j08691 Avatar answered Sep 23 '22 15:09

j08691