tags:

views:

252

answers:

6

Hello guys.

I need to make following code stretchable with predefined height

<style>
.title{
   background: url(bg.gif) no-repeat bottom right;
   height: 25px;
}
</style>

<span class="title">This is title</span>

But since span is inline element, "height" property won't work.

I tried using div instead, but it will expand up to the width of upper element. And the width should be flexible.

Can anyone suggest any good solution for this?

Thanks in advance.

A: 

Why do you need a span in this case? If you want to style the height could you just use a div? You might try a div with display: inline, although that might have the same issue since you'd in effect be doing the same thing as a span.

Seth Petry-Johnson
Or, ignore my ramblings altogether and use henasraf's answer :)
Seth Petry-Johnson
A: 

Give it block properties to give it size:

.title{
 display: block;
 background: url(bg.gif) no-repeat bottom right;
 height: 25px;
}
Nick Craver
That would make it the same as a div. He needs to use `display:inline-block`
henasraf
+4  A: 

Give it a display:inline-block in CSS - that should let it do what you want.

henasraf
For what it's worth, IE7 doesn't support `inline-block`.
Scott Cranfill
Great job henasraf!Thanks a lot ;)
Kelvin
Np. @Scott: I don't see that on W3 -- here's what they say: `Note: No versions of Internet Explorer (including IE8) support the property values "inline-table", "run-in", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-row", or "table-row-group".`
henasraf
@henasraf - @Scott's correct, I can't use it because of quicks as well: http://www.quirksmode.org/css/display.html
Nick Craver
Quote: `IE 6/7 accepts the value only on elements with a natural display: inline.`
henasraf
Another workaround is to maybe have a `padding-bottom:?` to it?
henasraf
Ahah, I guess it will work if applied to a `span` then. Thanks for the info, henasraf.
Scott Cranfill
+4  A: 

Use

.title{
  display: inline-block;
  height: 25px;
}

The only trick is browser support. Check if your list of supported browsers handles inline-block here.

Jon Galloway
+2  A: 

Assuming you don't want to make it a block element, then you might try:

.title  {
    display: inline-block; /* which allows you to set the height/width; but this isn't cross-browser, particularly as regards IE < 7 */
    line-height: 2em; /* or */
    padding-top: 1em;
    padding-bottom: 1em;
}

But the easiest solution is to simply treat the .title as a block-level element, and using the appropriate heading tags <h1> through <h6>.

David Thomas
You are right, I switched to heading now.
Kelvin
+1  A: 

this is to make display:inline-block work in all browsers:

Quirkly enough, in IE (6/7) , if you trigger hasLayout with "zoom:1" and then set the display to inline, it behaves as an inline block.

.inline-block {
    display: inline-block;
    zoom: 1;
    *display: inline;
}
I.devries
Thanks I.devries, I used your code. Hope it will work for IE6/7.
Kelvin