HighDots Forums  

Cross Browser CSS Sprites Alignment

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss Cross Browser CSS Sprites Alignment in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Milan Adamovsky
 
Posts: n/a

Default Cross Browser CSS Sprites Alignment - 04-11-2009 , 04:44 AM






If you are like me and you try to avoid having to maintain one CSS
file for IE and another for non-IE, here is a new little trick I
figured out that may be helpful in regards to sprites. For this
article to make sense you must have a preliminary understanding of CSS
sprites, furthermore I've only tested it in IE7 and FF3.

The challenge is to have an image of a sprite to get positioned
equally on IE and and FireFox let's say. Normally the dirty solution
would be to code an exception. The one problem is that the <i>!
important</i> is not recognized in some IE7 versions. So while it is
easy to trick the browser in IE6 with <i>!important</i>, no such luck
in IE7. The other constraint is that we want to minimize maintenance
cost, so we want to try to keep only one CSS file.

Let's examine the problem by example.

<code>
.class {
background: white url('/images/sprite.gif') no-repeat;
background-position: 0px -82px !important;
background-position: 0px -79px;
}
</code>

The above is what we would normally be inclined of doing. We might
even want to replace the dimensions to <i>em</i> with same undesired
results.

The solution is actually quite simple: We change the dimensions to a
<b>percentage</b>!

That's right, we will simply have to change the unit to <i>%</i> and
surprisingly it yields same results in both browsers. The only caveat
is that positive percentage moves the image up, and negative
percentage moves the image down - in other words the opposite to the
usual behavior when <i>px</i> or <i>em</i> is used.

A look at the new code would look something like:

<code>
.class {
background: white url('/images/sprite.gif') no-repeat;
background-position: 0px 105%;
}
</code>

I hope this will prove helpful as it has helped me.

Milan Adamovsky
http://www.perlscript.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.