tags:

views:

3296

answers:

4

i use tag 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 it's 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?

+1  A: 

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.

cletus
+6  A: 

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

cobbal
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).
Ken Browning
Yep, IE does support inline-block. Its support is subtly different, but it would work in this case.
thomasrutter
+1  A: 

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>
Antonio
+4  A: 

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>
cxfx
bgreen1989
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; :)
Arve Systad
thanks, and the answer below was critical to get the width working
Angela