91

I use <span> tags in my module titles, e.g.

<span>Categories</span>.

I specify the span's background color/image, width and height in css.

But the span's width depends on its content/text.

So, if I do <span></span>, with just a background image/color in css, nothing appears.

Of course, I want something to appear.

How can I resolve this?

3
  • 4
    I'd just like to point out that this isn't a very good use of span. It's meant to mark short ranges of content within a larger whole, so it's intentionally not a block element by default. For titles, one of the h# tags would be best. Failing that, a div is still usually better than a span.
    – Chuck
    Commented Mar 7, 2009 at 6:47
  • thanks for your answers.. :D i find this a silly q'n to begin with.lol..so many years ago already. = ))
    – mars-o
    Commented Jun 11, 2014 at 21:50
  • Fun fact: when measuring inline elements (like span, by default), you can get different results across browsers, Safari seems to round up to full pixels while Chrome does not. Setting display: inline-block prior to measuring unifies the results. Commented Mar 6 at 11:39

8 Answers 8

130

spans default to inline style, which you can't specify the width of.

display: inline-block;

would be a good way, except IE doesn't support it

you can, however, hack a multiple browser solution

3
  • 2
    I believe that IE supports inline-block since at least version 6. I think it only supports inline-block if the element its applied to is naturally inline (which <span> is). Commented Mar 7, 2009 at 6:14
  • 3
    Yep, IE does support inline-block. Its support is subtly different, but it would work in this case. Commented Mar 7, 2009 at 6:54
  • Awesome! Starting to understand the real differences in display: Commented Feb 26, 2014 at 16:09
119

You could explicitly set the display property to "block" so it behaves like a block level element, but in that case you should probably just use a div instead.

<span style="display:block; background-color:red; width:100px;"></span>
4
  • thanks, i see. that happens because span is inline. i now see the difference bet. inline & block displays. yes, div is appropriate.:D..thanks
    – mars-o
    Commented Mar 7, 2009 at 6:18
  • 4
    Using a DIV inside paragraphs, headers (h1,h2 etc) would break validation if you care about that. Another possible sollution is to use a span, display: block; and float: left; :) Commented Mar 9, 2009 at 11:22
  • thanks, and the answer below was critical to get the width working
    – Satchel
    Commented Feb 17, 2010 at 2:36
  • 3
    it is better to use inline-block instead of block Commented Nov 27, 2017 at 14:47
6

You can't specify the width of an element with display inline. You could put something in it like a non-breaking space ( ) and then set the padding to give it some more width but you can't control it directly.

You could use display inline-block but that isn't widely supported.

A real hack would be to put an image inside and then set the width of that. Something like a transparent 1 pixel GIF. Not the recommended approach however.

5

You could try width: fit-content

source: https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content

4

Like in other answers, start your span attributes with this:

display:inline-block;  

Now you can use padding more than width:

padding-left:6%;
padding-right:6%;

When you use padding, your color expands to both side (right and left), not just right (like in widht).

3

I would use the padding attribute. This will allow you add a set number of pixels to either side of the element without the element loosing its span qualities:

  • It won't become a block
  • It will float as you expect

This method will only add to the padding however, so if you change the length of the content (from Categories to Tags, for example) the size of the content will change and the overall size of the element will change as well. But if you really want to set a rigid size, you should do as mentioned above and use a div.

See the box model for more details about the box model, content, padding, margin, etc.

2

Use the attribute 'display' as in the example:

<span style="background: gray; width: 100px; display:block;">hello</span>
<span style="background: gray; width: 200px; display:block;">world</span>
1

Having fixed the height and width you sholud tell the how to bahave if the text inside it overflows its area. So add in the css

overflow: auto;

Not the answer you're looking for? Browse other questions tagged or ask your own question.