105

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.

What's a good solution for this?

starball
  • 20,030
  • 7
  • 43
  • 238
Kelvin
  • 8,813
  • 11
  • 38
  • 36
  • 1
    You should use heading tags (h1, h2, h3, ...) for titles. It's more semantically correct. – Pickels Feb 26 '10 at 18:50
  • 1
    Yes, you are right Pickels. Thanks everyone for your help. This is my final css. It works great for me: – Kelvin Feb 26 '10 at 19:10

9 Answers9

164

Give it a display:inline-block in CSS - that should let it do what you want.
In terms of compatibility: IE6/7 will work with this, as quirks mode suggests:

IE 6/7 accepts the value only on elements with a natural display: inline.

casraf
  • 21,085
  • 9
  • 56
  • 91
  • For what it's worth, IE7 doesn't support `inline-block`. – Scott Cranfill Feb 26 '10 at 18:48
  • 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".` – casraf Feb 26 '10 at 18:50
  • 2
    @henasraf - @Scott's correct, I can't use it because of quicks as well: http://www.quirksmode.org/css/display.html – Nick Craver Feb 26 '10 at 18:54
  • 2
    Quote: `IE 6/7 accepts the value only on elements with a natural display: inline.` – casraf Feb 26 '10 at 18:56
  • Another workaround is to maybe have a `padding-bottom:?` to it? – casraf Feb 26 '10 at 18:59
  • 1
    Ahah, I guess it will work if applied to a `span` then. Thanks for the info, henasraf. – Scott Cranfill Feb 26 '10 at 19:07
27

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
  • 52,327
  • 25
  • 125
  • 193
13

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
  • 8,747
  • 1
  • 27
  • 29
6

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
  • 249,100
  • 51
  • 377
  • 410
0

span { display: table-cell; height: (your-height + px); vertical-align: middle; }

For spans to work like a table-cell (or any other element, for that matter), height must be specified. I've given spans a height, and they work just fine--but you must add height to get them to do what you want.

mike s
  • 1
0

Another option of course is to use Javascript (Jquery here):

$('.box1,.box2').each(function(){
    $(this).height($(this).parent().height());
})
Ukuser32
  • 2,147
  • 2
  • 22
  • 32
0

In some case you may want to adjust a SPAN height without changing display : inline.

To solve this, you can add a top and/or bottom border property, setting the required width parameter to your needs, and a transparent color to hide that border :

.myspan {
   border-top : solid 3px transparent;
   border-bottom : solid 3px transparent;
}
Dr Fred
  • 939
  • 8
  • 15
0
.my-span {
    border: solid 1px;
    border-color: gray;
    border-radius: 6px;
    padding-left: 5px;
    padding-right: 5px;
    height: 17px;
    padding-top: 6px;
    display: inline-block;
    line-height: 0px;
}

enter image description here

M Komaei
  • 7,006
  • 2
  • 28
  • 34
-3

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
  • 11,845
  • 7
  • 49
  • 69
  • 3
    Or, ignore my ramblings altogether and use henasraf's answer :) – Seth Petry-Johnson Feb 26 '10 at 18:44
  • 1
    Down voting comments just discourages those who want to legitimately contribute. Stop it! Just offer some constructive feedback instead, or probably more to the point, your own opinion on the matter. –  May 28 '16 at 17:40