Thursday, September 11, 2008

IE Background Image Problem

Imagine that, you have a blog and you are designing a new rating system, where you will use the golden stars to rate your entries, then you made an HTML prototype to create the layout of your new rating system, and here is the code you have written:

  1 <html> 
  2     <body> 
  3         <h1> 
  4             <div id="stars"> 
  5             </div> 
  6             How is the BG IMG fixed in IE 
  7         </h1> 
  8     </body> 
  9     <style> 
 10       #stars{ 
 11         height: 20px; 
 12         width: 100px; 
 13         float: right; 
 14         background: url("star_on.png") repeat; 
 15       } 
 16     </style> 
 17 </html>

The "star_on" image you are using is 20 x 20 px, the hieght of the stars DIV is 20px to fit with the image, the image is set to repeat and the width is set to 100px so that you will see 5 stars.

So far we have done coding, the next step is to test this on the browsers to be supported, in different browsers it is working well ( tested on safari, opera, FF and chrome ) and here is how it is displayed:

Good Rating

When it was the turn for the lovely IE and its unique rendering engine, it decided to render the markup as seen below:

Bad Rating

Note that the problem is that the image is repeated vertically and go beyond the 20px high DIV it should display within...

Lets start with the fix and then see why IE decided to extend the image,

The Fix is to adjust the font-size property for the rating DIV :

  1 #stars{ 
  2   height: 20px; 
  3   width: 100px; 
  4   float: right; 
  5   background: url("star_on.png") repeat; 
  6   font-size: 1pt; 
  7 }

But why on earth the font-size is related to the BG image ?!

In fact, the problem is when IE pass by an empty DIV in the markup it assume anon-breaking-space inside the DIV, then accordingly it applies to it the nearest inherited font-size, in our case the H1 font-size; which is larger than the 20px hieght so the BG image found some more space to sneak into.

So to solve this you had to override this font-size and adjust it to have your backgroung image displayed correctly!

6 comments:

Anonymous said...

Generally I do not post on blogs, but I would like to say that this post really forced me to do so! really nice post.

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

I will not agree on it. I regard as polite post. Particularly the designation attracted me to be familiar with the unscathed story.

Anonymous said...

Opulently I assent to but I contemplate the brief should prepare more info then it has.

Anonymous said...

Good brief and this enter helped me alot in my college assignement. Thank you seeking your information.

Anonymous said...

Well I to but I about the collection should secure more info then it has.