midstack
midstack

Reputation: 2123

IE9/8/7 css sprite position slip issue

I want to css sprite (sprite image total width:45px and total height:15px consists of three image ) but there is a problem in IE9/8/7. link and hover work but when click the button (active) sprite image slipping to left 1px. issue for only IE 9/8/7.How can I fix this?

CSS:

body{
    margin:0;
    padding:0;
}

.button{
    background:url(sprite-image.png) no-repeat 0 0;
    width:15px;
    height:15px;
    cursor:pointer;
}

.button:hover{
    background:url(sprite-image.png) no-repeat -15px 0;
}

.button:active{
    background:url(sprite-image.png) no-repeat -30px 0;
}

.cont{
    width:200px;
    height:200px;
    float:left;
    margin:50px 0 0 100px;
}

HTML:

 <body>
  <div class="cont">
     <div class="button">&nbsp;</div>
  </div>
 </body>

"link" and "hover" and "active" FF,Chrome,Safari,Opera like this;

enter image description here

but IE 9/8/7 active look like this;

enter image description here

I concretized above images to make it look better . My sprite image;

enter image description here

Upvotes: 16

Views: 7373

Answers (6)

The AV
The AV

Reputation: 639

You can use below trick which will help to give manual value

For IE7 (underscore before the value)

.button:hover {
    background-position: _-15.5px 0; 
}

Or IE-7 & IE-8(need to add\9)

.button:hover {
    background-position: -15.5px\9; 
}

For IE8only (\0/)

*+.button:hover {
    background-position: -15.5px\0/;
}

Upvotes: 3

webextensions.org
webextensions.org

Reputation: 739

I have faced similar issues with IE8 before but IE9 worked fine in my case (not sure about IE7 but it must be like IE8 for this thing).

It can be resolved/improved by one of these 2 approaches:

1) Modify the image (maybe in resolution, color combination etc.) and try if it works. Why this might work? Because in your example, IE appears trying to do some image manipulations "intelligently" which unfortunately go wrong at times (especially for small images/pixel perfect cases) and you can just hope that it doesn't fail badly for your new images.


2) Use background-position accuracy of 0.5px units.

    Note "background-position: -15.5px 0;" in the following code. This solution will reduce your frustration by at least 50% :-) I am afraid that you might need to provide IE specific CSS for this solution but that should be fine ... You can add the browser identifier class name on tag using JavaScript or with technique mentioned @ http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ and then use those class names when you write browser specific CSS.

The solution:

.button {
    background:url(images/sprite.png) no-repeat 0 0;
    width: 15px;
    height: 15px;
    cursor:pointer;
}

.button:hover {
    background-position: -15.5px 0;
}

.button:active {
    background-position: -30px 0;
}

.cont {
    width: 200px;
    height: 200px;
    float: left;
    margin: 50px 0 0 100px;
}

Upvotes: 3

myradon
myradon

Reputation: 421

Why not use IE-conditional comments;

 <!doctype html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!-- Consider adding a manifest.appcache: h5bp.com/d/Offline -->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

And then write eg CSS-rules like .lt-ie9 .someclass{}to target a IE-version. I use them to fix some IE-specific css-stuff. No dirty hacks, no hastle just css. Did you check with eg Firebug Lite what happens?! outline: 0 none?

Upvotes: 8

Riskbreaker
Riskbreaker

Reputation: 4791

I created a fake sprite using your graphic to see what you are seeing but looking good in my fiddle in all IE 7-9 (note i just change positioning and made it construsive (less):

http://jsfiddle.net/Riskbreaker/Rr8p2/

body{
    margin:0;
    padding:0;
}

.button{
    background:url(images/sprite.png) no-repeat 0 0;
    width:14px;
    height:15px;
    cursor:pointer;
}

.button:hover{
    background-position:0px -27px;
}

.button:active{
    background-position:0px -27px;
}

.cont{
    width:200px;
    height:200px;
    float:left;
    margin:50px 0 0 100px;
}

Remember the positioning I made up so you can adjust. I never had the active IE issue before...but let me know what you are seeing....if the issue persist and you don't want another file then do this:

IE7: *.button:active{background-position:0px -28px;} (or whatever the correct position is )...

IE8: .button:active{background-position:0px -28px\9;}.........

IE9....not sure your latest but it should not have any issues (latest)

Upvotes: 3

karacas
karacas

Reputation: 2132

Try this change in css:

.button{
    background: url(sprite-image.png) no-repeat left 0;
    width: 15px;
    height: 15px;
    cursor: pointer;
}
.button:hover{
    background: url(sprite-image.png) no-repeat center 0;
}
.button:active{
    background: url(sprite-image.png) no-repeat right 0;
}

Upvotes: 2

Amyth
Amyth

Reputation: 32949

Add a Internet explorer specific stylesheet to the <head></head> section.

<!--[if lt IE 9]>
<link rel="stylesheet type="text/css" href="/css/ie.css" />
<![endif]-->

and in ie.css do something like:

.button:active{
    background:url(sprite-image.png) no-repeat -29px 0 !important;
}

(There's Always an issue with ie , phew !)

Upvotes: 3

Related Questions