Neal Grosskopf Dot Com A NG Designs Site

Neal Grosskopf Dot Com Return To Neal Grosskopf's Home Page a NG Designs Site

Toolbar

  • Skip To
    • Menu
    • Content
  • Validate
    • HTML
    • CSS
    • WAI
  • Font Size
    • Small
    • Medium
    • Large
    • Extra Large
  • Account
    • Create Account
    • Login

Interact

Navigation

  • Home
  • Interact
    • Blog
    • Message Board
    • Open Links
    • Open Videos
    • Photo Gallery
    • Polls
    • Reviews
    • Geek Speak
  • Designs
    • View All Designs
    • Professional
    • Corporate
    • Templates
    • Personal
    • Club
    • NG Designs
    • Web Guides
  • Files
    • Artwork
    • Wallpapers
    • My File Downloads
  • Extras
    • Bookmarklets
    • The Magic 8-Ball
    • Career Builder RSS Feed Generator
    • Monster.com RSS Feed Generator

CSS Image Preload - :hover Pseudo Selector Parent Of display: none Elements

  • Actions
    • View All
    • View Article Statistics
    • View User Statistics
    • Search
    • Subscribe to RSS Feed

Recent Articles:

  • Tracking Outgoing Links in Google Analytics Using Event Tracking and jQuery
  • Tracking Internal Website Campaigns With Google Analytics and PHP
  • Everything You Need to Know About jQuery's .data() Method
  • Google vs. China: How It Will Affect Web Workers
  • Don't Like The New Facebook Home Page? Fix It With 5 Lines of CSS
View All

Subscribe To Feed
loading Subscribers

I'm On Twitter loading

Oct 22

Lately at my job, our internet connection has slowed down dramatically. This means that every http request over our internet connection is much more noticeable. I often refer back to my own website while at work, to steal some code or see how I did something in the past. One of the things I've noticed with our slow internet connection is my main navigation menu's background-image is really laggy. The image on the drop down which has a value of display: none; would have a 3 second delay before displaying.

Annoyed by the extreme delay in my menu's background-image I started tinkering to solve the problem. My first tool of choice was using the Firefox Live HTTP Headers addon. Using this I could load the page, and then proceed to hover over elements to see if the browser was hitting the server for images or other files. I noticed on my navigation that when I hovered over it, it needed to retrieve my background-image from the server. This made finding a solution much easier.

What CSS Properties Will Do A CSS Image Preload?

The following methods will download the images from the web server upon page load. This assumes the following code which might be some element within your site's template:

<div id="preload"></div>

These Methods Successfully Downloaded The CSS Preload Image Upon Page Load

#preload { background-image: url(image.png); position: absolute; left: -9999px; } #preload { background-image: url(image.png); visibility: hidden; } #preload { background-image: url(image.png); background-repeat: no-repeat; background-position: -9999px -9999px; }

I consider the background-repeat: no-repeat; background-position: -9999px -9999px; to be the most practical of these methods

These Methods Did Not Download The CSS Preload Image Upon Page Load

#preload { background-image: url(image.png); display: none; } #preload:hover { background-image: url(image.png); } #preload:active { background-image: url(image.png); } #preload:focus { background-image: url(image.png); }

The browser will not download the images upon page load if the element has a display: none; property (interesting that they will download the image if it's visibility is set to hidden). The browser will also not download images that have a :hover, :active or :focus in it's selector.

CSS Sprites?

Nope, sorry, before you even bother commenting "why not use CSS sprites?" In my situation that was not an option. The element I wanted to preload the image for had :hover for it's parent element and display: none; set, talk about a double whammy CSS sprites only work for elements that are visible on page load. I should digress that I could have bundled these images with some other images in my site's template but semantically they had no useful purpose with any other background-image.

The Solution?

I ended up using the background-repeat: no-repeat; background-position: -9999px -9999px; method. The trick is I needed to place my CSS background-image on some unused element on the page. This is not the best solution for CSS maintainability but it was a necessary evil if I wanted my hidden background-image to load before it was displayed on :hover

How It's Done

I had two elements on my page (in close proximity or parents of my elements). I decided to place these selectors in their own section of my stylesheet so that they would stand out a bit.

#nav { background: url(/files/images/template/menu_bg-trans.png) no-repeat -9999px -9999px; } #header { background: url(/files/images/template/helper_a_bg-trans.png) no-repeat -9999px -9999px; }

Optimal Solution?

No, not really but It fixed a problem I had. One could also create specific DIVs just for image preloading and absolutely position them outside of the viewport, but that just adds extra markup to the page and isn't really semantically correct. Feel free to leave a comment if you can think of a better way.

Tags: css, html ( Add Tag )

Bookmark This Page:
  • Digg loading
  • Delicious loading
  • Re-Tweet loading
  • StumbleUpon loading
  • DZone loading
  • Snipplr
  • Script & Style

Replies:

  • Eric Lightbody
    Eric Lightbody
    #1

    Thu. October 30, 12:43:58 PM

    Nice! I did something similar here (http://www.speedqueen.com/comm/vend/features.asp?model=SWT/SWL_Series&series=SWT-SWL) after having delays when using display: none. Very nice that you put it all together.

    Just so you know, your link button is not working.

  • Neal Grosskopf
    Neal Grosskopf
    #2

    Thu. October 30, 1:54:58 PM

    Yeah, the whole toolbar only works in IE. I've never got around to fixing it. Anyways most visitors just enter in the html...lol.

  • jjj jhj
    jjj jhj
    #3

    Fri. June 4, 8:50:10 AM


Reply:

B U I Link Font Color Edit Preview ?
Characters: 0 Textbox Size: [+] [-]
   

  • New Comment
    #4

    Sat. July 31, 1:12:10 PM

Font Size:

  • A
  • A
  • A
  • A

Search:

Places:

  • Blog
  • Message Board
  • Chat Room
  • Geek Speak
  • Open Videos
  • Reviews
  • Tag Cloud
 

Site Links

Site

  • About
  • Contact Me
  • Site Map
  • Site Search

Interact

  • Blog
  • Geek Speak
  • Message Board
  • Open Links
  • Open Videos
  • Photo Gallery
  • Polls
  • Reviews

Random Stuff

  • Hosting by FatCow
  • © Copyright 2005 - 2010
  • Modified: Friday, July 16, 2010
  • Load Time: 0.04 Seconds

Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes