views:

224

answers:

5

Background: I need to have an inline element to which I can apply width and height via CSS. AFAIK, img is the only way to have this behavior, currently.

I would rather not have the image point to a transarent pixel GIF on the server. It may be cached, but browsers queue it nevertheless, slowing down overall page speed. Other clients might not be caching at all.

PS: No, floating div is not sufficient, it behaves differently from inline elements.

EDIT: Sorry, I should have inserted the term "cross browser" somewhere. It must at least be working for FF>=2, IE>=7 and current Safari.

+1  A: 

Can you set:

display:inline-block;
width:50px;
height:10px;

IIRC, images are an "inline block" element, thus they can be rendered inline in text strings, but still have block-like properties.

scunliffe
inline-block may have strange behavior in IE6/7: http://www.quirksmode.org/css/display.html
naivists
Thanks for you input... Always thought inline-block would not be working in IE7, as I reviewed it, I noticed it does (at least it does in a simple example)
PeterP
+5  A: 

You could use the "data:" URI scheme to embed an image.

Other replaced elements might also work. Setting display to "inline-block" might also be worth looking into.

outis
+1 this is the rightest and validest way.
Pekka
looks good, however it seems not to be crossbrowser enough for my purposes (IE7) - sorry for not specifying this right away
PeterP
However, I reviewed inline-block (thought it would not be working in IE7) which seems to work quite alright. Thanks for the hint.
PeterP
@PeterP: what are your purposes, exactly?
outis
@outis: purpose is to have interactive elements which can be reused in an inline context too (like an image). after all, it is about accomplishing a certain layout without (mis-)using spacer images and likewise.I used inline-block now, had some fun with IE7/6 (did no longer work fine when it got more complex), works fine now.Thanks for your inspiration :)
PeterP
+1  A: 

I guess it will be valid in the W3C validator sense, because the validator does not check whether the link is a resource or not.

However, valid in the broader sense, I would say it is not. An src attribute is required in the IMG tag, and I would say must point to a valid image resource.

I find outis`s "data: URI" idea the best way.

If that doesn't work, a transparent image is your best bet. It's one call, it's a few bytes at best, and will be cached by most clients.

Pekka
Thanks for your input, I'll try inline-block first :)
PeterP
A: 
Joel Etherton
But that's not inline. You either need to add absolute positioning or have the element float. Both solutions would not work for me.
PeterP
A: 

Use a <span> tag with a &nbsp; in it - totally valid - then set it's width and height in CSS, also apply display: block;

Using an empty <span> tag means it will be ignored by screen readers and it won't show up as a broken image when styles are disabled.

It'll also not use up any more bandwidth.

:-D

Jonny Haynes
Thanks for your proposal, however, block is not inline, which is an important difference in my case.
PeterP