{"id":5184,"date":"2009-12-30T06:15:45","date_gmt":"2009-12-30T13:15:45","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=5184"},"modified":"2009-12-30T06:15:45","modified_gmt":"2009-12-30T13:15:45","slug":"css-only-image-preloading","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/css\/css-only-image-preloading\/","title":{"rendered":"CSS Only Image Preloading"},"content":{"rendered":"

One big reason to use image preloading is if you want to use an image for the background-image of an element on a mouseOver or :hover event. If you only apply that background-image in the CSS for the :hover state, that image will not load until the first :hover event and thus there will be a short annoying delay between the mouse going over that area and the image actually showing up.<\/p>\n

Technique #1<\/h3>\n

Load the image on the element’s regular state, only shift it away with background position. Then move the background position to display it on hover.<\/p>\n

#grass { background: url(images\/grass.png) no-repeat -9999px -9999px; }\r\n#grass:hover { background-position: bottom left; }<\/code><\/pre>\n

Technique #2<\/h3>\n

If the element in question already has<\/em> a background-image applied and you need to change<\/em> that image, the above won’t work. Typically you would go for a sprite here (a combined background image) and just shift the background position. But if that isn’t possible, try this. Apply the background image to another page element that is already in use, but doesn’t have a background image.<\/p>\n

#random-unsuspecting-element { background: url(images\/grass.png) no-repeat -9999px -9999px; }\r\n#grass:hover { background: url(images\/grass.png) no-repeat; }<\/code><\/pre>\n

The idea create new page elements to use for this preloading technique may pop into your head, like #preload-001, #preload-002, but that’s rather against the spirit of web standards. Hence the using of page elements that already exist on your page.<\/p>\n

I had the idea to try to and use the same element, only use the :after pseduo-class to load the image, so you didn’t need to rely on there being enough extraneous background-free images on your page to work with, but for whatever reason didn’t want to preload them properly.<\/p>\n

More<\/h3>\n

Check this article<\/a> out for some more techniques, including JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"

One big reason to use image preloading is if you want to use an image for the background-image of an element on a mouseOver or :hover event. If you only apply that background-image in the CSS for the :hover state, that image will not load until the first :hover event and thus there will be […]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3222,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-snippet.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":345532,"url":"https:\/\/css-tricks.com\/almanac\/selectors\/b\/backdrop\/","url_meta":{"origin":5184,"position":0},"title":"::backdrop","date":"August 3, 2021","format":false,"excerpt":"The ::backdrop CSS pseudo-element creates a backdrop that covers the entire viewport and is rendered immediately below a

or any element that enters fullscreen mode using the Fullscreen API. dialog::backdrop { background-color: darkorange; } The backdrop is selectable as a pseudo-element when an element is opened in the browser\u2019s\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/07\/backdrop-diagram.png?fit=975%2C527&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":376849,"url":"https:\/\/css-tricks.com\/wordpress-global-styles-reference-tables\/","url_meta":{"origin":5184,"position":1},"title":"WordPress Global Styles Reference Tables","date":"January 31, 2023","format":false,"excerpt":"We\u2019ve covered a lot of ground in this series. So much so that I thought it would be helpful to condense all the various block theme settings and styles from theme.json into a single page right here.","rel":"","context":"In \"json\"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2023\/02\/guide-wordpress-block-theme-css.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3898,"url":"https:\/\/css-tricks.com\/snippets\/jquery\/fade-one-image-to-another-menu\/","url_meta":{"origin":5184,"position":2},"title":"Fade One Image to Another Menu","date":"September 6, 2009","format":false,"excerpt":"Make a CSS sprite image, with the top half and the bottom half being the two images you want to animate between. The jQuery adds a tag, and adds the bottom half of the sprite image as its background. As you hover on and off, the span animates between\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":372892,"url":"https:\/\/css-tricks.com\/almanac\/selectors\/f\/file-selector-button\/","url_meta":{"origin":5184,"position":3},"title":"::file-selector-button","date":"August 30, 2022","format":false,"excerpt":"The ::file-selector-button in CSS is a pseudo-element that selects in HTML. input::file-selector-button { background-color: #f8a100; } You've undoubtedly come across file upload inputs \u2014 they're very common, especially for including file attachments in a form, like a PDF or image. CodePen Embed Fallback Syntax selector::file-selector-button { } You\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/css-tricks-logo-gradient-outline.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":196143,"url":"https:\/\/css-tricks.com\/almanac\/properties\/b\/background-image\/","url_meta":{"origin":5184,"position":4},"title":"background-image","date":"February 17, 2015","format":false,"excerpt":"The background-image property in CSS applies a graphic (e.g. PNG, SVG, JPG, GIF, WEBP) or gradient to the background of an element. There are two different types of images you can include with CSS: regular images and gradients. Images Using an image on a background is pretty simple: body {\u2026","rel":"","context":"With 17 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":376823,"url":"https:\/\/css-tricks.com\/defining-global-styles-in-wordpress\/","url_meta":{"origin":5184,"position":5},"title":"Defining Global Styles","date":"January 31, 2023","format":false,"excerpt":"Let\u2019s move to the other top-level section of theme.json where we can configure the CSS of a block theme: styles. We\u2019ll learn what it is exactly and how we can use it to override and apply the preset settings values we covered in Part 2.","rel":"","context":"In \"WordPress\"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2023\/02\/guide-wordpress-block-theme-css.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/5184"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=5184"}],"version-history":[{"count":3,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/5184\/revisions"}],"predecessor-version":[{"id":5187,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/5184\/revisions\/5187"}],"up":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3222"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=5184"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=5184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}