views:

37

answers:

4

the following code is not work in safari, but properly work in IE.

<style>
.my
{
background:url("../../myproject/images/add.gif");
width:100%;
height:22px;
background-repeat: no-repeat;
}
</style>
<span class="my">

</span>
+2  A: 

If IE works and Safari (and Firefox) doesn't, that probably means IE is not conforming to standard.

span, for one thing, cannot have width and height. some earlier IE version actually let you have width and height.

so you can change your span to a div to see.

also, always use a modern doctype, such as HTML 4.01 or XHTML 1.0, so that IE will try to use the more standard rendering methods.

動靜能量
LOL - great answer!
JZ
+3  A: 

By default a span element is display: inline so the height and width properties do not apply to it.

Since it has no explicit dimensions, and no content, it is rendered as being 0 by 0. This doesn't leave any room for it to have a visible background.

Use a different element (one which is block by default), or change the display property to one that height and width apply to to get this to work.

That said, if you are just creating a box with no content, there is a very good chance you should just be using an img element in the first place.

David Dorward
Some people put images as background elements so they don't get selected with the text around them. It's a bit overkill but whatever.
animuson
+2  A: 

use backgound-image: url("../../myproject/images/add.gif"); or else use the background shortcut starting with a color:background:transparent url(...) no-repeat;

And adding display:block; would allow you to set the height and width of the span element, but it will no longer line with other text.

Nilloc
Using background with only an image will clear any background color that it might inherit... I use backgrounds with only an image all the time.
animuson
@animunson Your right, though if you have a tranparent png or gif it's a bit safer to define a transparent color for the background shortcut. Incase the browser doesn't (Safari sets background-color to "initial" whereas FF sets it to "transparent" if it isn't defined).
Nilloc
@animunson, I take that back *curses webkit inspector* the computed value is in fact transparent when it isn't defined.
Nilloc
A: 

You should use background-image: url(...); see here

RC
Did you read this: `It does not matter if one of the property values are missing, as long as the ones that are present are in this order.` Typing 'background' is shorter than typing 'background-image' and it makes absolutely no difference.
animuson
Yeah, that's W3Schools being crap again. It is a dreadful resource, and that is just one more error in it. (Using background-image won't help in this situation, but there **are** differences between using the shorthand version (which resets the unspecified properties) and the individual properties).
David Dorward
@David Dorward: I wouldn't consider that an error, just missing information that they probably should add.
animuson
Since they say "It does not matter" and since it **does** matter, then I would consider it an error.
David Dorward