{"id":4711,"date":"2018-06-16T01:08:01","date_gmt":"2018-06-16T01:08:01","guid":{"rendered":"http:\/\/w3bits.com\/?p=4711"},"modified":"2021-09-17T06:13:17","modified_gmt":"2021-09-17T06:13:17","slug":"css-sprites","status":"publish","type":"post","link":"https:\/\/w3bits.com\/css-sprites\/","title":{"rendered":"CSS Sprites: Creating PNG Sprites and Using with CSS"},"content":{"rendered":"<p>Often times our designs carry elements like icons, image roll-overs, and decorated CTAs. <strong>CSS sprites<\/strong> come in very handy to achieve all those things.<\/p>\n<p>This guide covers how to use CSS sprites, but before that we&#8217;ll also learn what is an image sprite and how to create one.<\/p>\n<h2>What is a CSS Sprite<\/h2>\n<p>We need to know about an image sprite before we start talking about CSS sprites. An image sprite is a compilation of different image assets that we want to use on our web application.<\/p>\n<p>These images could fit in any of the below given cases&#8230;<\/p>\n<ul>\n<li>Icon assets like social media, fancy bullets etc.<\/li>\n<li>Different states for a button roll-over<\/li>\n<li>A fixed background eg. a logo<\/li>\n<\/ul>\n<p>After generating a spritesheet, we can use it further in our UI with the help of some simple CSS properties.<\/p>\n<p>It&#8217;s also that using image sprites doesn&#8217;t fit in the modern-day web designer&#8217;s workflow. People now consider using icon fonts or <a href=\"\/\/w3bits.com\/svg-sprites\/\" target=\"_blank\" rel=\"noopener noreferrer\">SVG sprites<\/a> rather than using CSS image sprites.<\/p>\n<h2>Why use CSS Sprites?<\/h2>\n<p>Whenever you open a website in your web browser, all its files eg. HTML, JavaScript, images etc. start to load up.<\/p>\n<p>More the files, more will be the number of requests made to load the website in the browser. <\/p>\n<p>More the requests, more will be the load time of the website. Now, this high load time is the enemy of UX and SEO.<\/p>\n<p>Using separate images for site&#8217;s assets would result in increased number of requests. Therefore, we need to create a combined version of all those images files to cut down the number of request to one. Hence, the net load time would also improve.<\/p>\n<p>This one combined PNG version of all those different images would be our image sprite. People also call an image sprite as spritesheet sometimes.<\/p>\n<p>Individual images are for screenshots, photos, diagrams, graphs, avatars. PNG sprites are for non-repeatable backgrounds.<\/p>\n<h3>Summarizing the need of CSS sprites<\/h3>\n<ul>\n<li>Load time of a web page is proportional to the number of requests sent while loading<\/li>\n<li>Most of the requests are in form of images<\/li>\n<li>It is always better to have the number of requests on web as low as possible\u2013to make a website load faster<\/li>\n<\/ul>\n<h2>How to create a PNG Sprite in Photoshop<\/h2>\n<p>Find it difficult to use Photoshop? Here are <a href=\"#alternatives\">some easier alternatives<\/a>.<\/p>\n<p>I have 4 icons to demonstrate the sprite creation, each of 100&#215;100 pixels dimensions. We are going to create a horizontal sprite image, i.e. a sprite in which the graphics flow from left to right.<\/p>\n<ol>\n<li>Import all the graphics in one PSD using  File > Scripts > Load files into stack; choose files<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/load-files-into-stack-photoshop.jpg\" alt=\"Loading files into stack in Photoshop\"><\/figure>\n<\/li>\n<li>Now, since we have 4 icons each of 100&#215;100 pixels, select Image > Canvas Size and make the canvas width of 400px (4&#215;100)<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/load-layers-photoshop.jpg\" alt=\"Photoshop's Load layers screen\"><\/figure>\n<\/li>\n<li>Using the New Guide Layout option in the View, make a guide layout with the following settings<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/canvas-size-photoshop.jpg\" alt=\"Altering the canvas size in Photoshop\"><\/figure>\n<\/li>\n<li>Select &#8220;Move&#8217;; make sure &#8220;Snap&#8221; and &#8220;Snap To Document Bounds&#8221; are enabled<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/view-snap-to-photoshop.jpg\" alt=\"Changing view's snap settings in Photoshop\"><\/figure>\n<\/li>\n<li>Align each icon accordingly by holding the shift key and the left mouse key<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/new-guide-layout-photoshop.jpg\" alt=\"Creating a new guide layout for CSS sprite in Photoshop\"><\/figure>\n<\/li>\n<li>Export the file as a PNG-28 image<br \/>\n<figure><img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/final-sprite.jpg\" alt=\"The final PNG \/ CSS sprite created in Photoshop\"><\/figure>\n<\/li>\n<\/ol>\n<p>And we are done creating sprite. Here is <a href=\"\/\/w3bits.com\/files\/img\/sprite.png\" target=\"_blank\" rel=\"noopener noreferrer\">how it looks like<\/a>.<\/p>\n<h2 id=\"code\">The code part<\/h2>\n<p>Let&#8217;s write some CSS first.<\/p>\n<p>The sprite we created above relies completely on CSS background properties. The basic set-up goes as given below&#8230;<\/p>\n<pre class=\"prettyprint\"><code>.sprite {\r\n  background-image: url(\/path\/to\/sprite.png);\r\n  background-size: 400% 100%;\r\n  background-repeat: no-repeat;\r\n}<\/code><\/pre>\n<p>What we did above is set our sprite sheet as our icon&#8217;s background with CSS background properties. The catch here is to fit the icon part of the sprite to the icon element.<\/p>\n<p>Since we have 4 icons in the sprite, the size of our icon element&#8217;s background should be 4-times the regular size, i.e. 400%.<\/p>\n<p>But it still doesn&#8217;t make much sense unless it carries some size.<\/p>\n<pre class=\"prettyprint\"><code>.sprite {\r\n  background-image: url(\/path\/to\/sprite.png);\r\n  background-size: 400% 100%;\r\n  background-repeat: no-repeat;\r\n  <strong>width: 75px;\r\n  height: 75px;<\/strong>\r\n}<\/code><\/pre>\n<p>What about making it full-proof for smaller screens? Is doing a responsive CSS sprite possible? Let&#8217;s see what we can do.<\/p>\n<pre class=\"prettyprint\"><code>.sprite {\r\n  background-image: url(\/path\/to\/sprite.png);\r\n  background-size: 400% 100%;\r\n  background-repeat: no-repeat;\r\n  width: 75px;\r\n  height: 75px;\r\n  <strong>max-width: 100%;<\/strong>\r\n}<\/code><\/pre>\n<p>We want a selector for each of our icons which could render the respective icon whenever it is used. We&#8217;ll be using CSS backround-position property to shift our PNG sprite to the right spot.<\/p>\n<p>Now, what could be the right position for the cat, frog, lion, and the mouse icon? If we look for four equidistant values between 0 and 100, we&#8217;ll end up into these values: 0, 33.33, 66.66, 100.<\/p>\n<h3>The final CSS<\/h3>\n<p>Because our sprite image rolls from left to right, we don&#8217;t need to worry about the y-coordinate at all. All the four values that we calculated above will act as the x-coordinate for the positioning.<\/p>\n<pre class=\"prettyprint\"><code>.sprite {\r\n  background-image: url(\/path\/to\/sprite.png);\r\n  background-size: 400% 100%;\r\n  background-repeat: no-repeat;\r\n  width: 75px;\r\n  height: 75px;\r\n  max-width: 100%;\r\n}\r\n\r\n<strong>.sprite--cat {\r\n  background-position: 0 0;\r\n}\r\n\r\n.sprite--frog {\r\n  background-position: 33.333% 0;\r\n}\r\n\r\n.sprite--lion {\r\n  background-position: 66.666% 0;\r\n}\r\n\r\n.sprite--mouse {\r\n  background-position: 100% 0;\r\n}<\/strong><\/code><\/pre>\n<h3>The HTML<\/h3>\n<p>The markup part is going to be simple, four divisions with CSS sprite classes to each. It&#8217;d look something like the below HTML.<\/p>\n<pre class=\"prettyprint\"><code>&lt;div class=\"sprite sprite--cat\"&gt;&lt;\/div&gt;\r\n&lt;div class=\"sprite sprite--frog\"&gt;&lt;\/div&gt;\r\n&lt;div class=\"sprite sprite--lion\"&gt;&lt;\/div&gt;\r\n&lt;div class=\"sprite sprite--mouse\"&gt;&lt;\/div&gt;<\/code><\/pre>\n<p>With the above markup and styles combined, here is <a href=\"\/\/w3bits.com\/demo\/css-sprite\/\" target=\"_blank\" rel=\"noopener noreferrer\">how it looks like<\/a>.<\/p>\n<p><a href=\"\/\/w3bits.com\/labs\/css-sprite\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">A rather prettier CSS Sprites demo<\/a><\/p>\n<hr>\n<h3>In conclusion<\/h3>\n<p>Wrapping up, that was our responsive CSS sprite sheet implementation. The demo works well on all modern web browsers, and of course on mobile phones too. <\/p>\n<p>In case you are not so okay with the Photoshop part, here are some online CSS sprite generators to make it easy for you.<\/p>\n<ul id=\"alternatives\">\n<li><a href=\"\/\/css.spritegen.com\/\" rel=\"external noreferrer noopener\" target=\"_blank\">CSS Spritegen<\/a><\/li>\n<li><a href=\"\/\/icomoon.io\/\" rel=\"external noreferrer noopener\" target=\"_blank\">Icomoon App<\/a><\/li>\n<li><a href=\"#code\">Jump up to the sprite code part<\/a>\n<\/ul>\n<p>The above mentioned online services allow you to create PNG sprites hassle-free. You can upload your graphics on Spritegen and then save the graphic and code within a few moments.<\/p>\n<p>On the other hand, Icomoon provides you with a variety of preloaded icons to choose from. After selecting icons, you can download the code and the sprite in a zipped archive.<\/p>\n<p>Your precious thoughts, questions, and suggestions are always invited. Icons used in this article are provided by <a href=\"https:\/\/www.flaticon.com\/authors\/freepik\" title=\"Freepik\">Freepik<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A beginner&#8217;s guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.<\/p>\n","protected":false},"author":1,"featured_media":4761,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-4711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-css"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CSS Sprites: Creating PNG Sprites and Using with CSS<\/title>\n<meta name=\"description\" content=\"A beginner&#039;s guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/w3bits.com\/css-sprites\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Sprites: Creating PNG Sprites and Using with CSS\" \/>\n<meta property=\"og:description\" content=\"A beginner&#039;s guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/w3bits.com\/css-sprites\/\" \/>\n<meta property=\"og:site_name\" content=\"W3Bits\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-16T01:08:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-17T06:13:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rahul\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@w3bits_\" \/>\n<meta name=\"twitter:site\" content=\"@w3bits_\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rahul\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/w3bits.com\/css-sprites\/\",\"url\":\"https:\/\/w3bits.com\/css-sprites\/\",\"name\":\"CSS Sprites: Creating PNG Sprites and Using with CSS\",\"isPartOf\":{\"@id\":\"https:\/\/w3bits.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/w3bits.com\/css-sprites\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/w3bits.com\/css-sprites\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg\",\"datePublished\":\"2018-06-16T01:08:01+00:00\",\"dateModified\":\"2021-09-17T06:13:17+00:00\",\"author\":{\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac\"},\"description\":\"A beginner's guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.\",\"breadcrumb\":{\"@id\":\"https:\/\/w3bits.com\/css-sprites\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/w3bits.com\/css-sprites\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/w3bits.com\/css-sprites\/#primaryimage\",\"url\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg\",\"contentUrl\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg\",\"width\":200,\"height\":200,\"caption\":\"CSS Sprites\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/w3bits.com\/css-sprites\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/w3bits.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Sprites: Creating PNG Sprites and Using with CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/w3bits.com\/#website\",\"url\":\"https:\/\/w3bits.com\/\",\"name\":\"W3Bits\",\"description\":\"Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/w3bits.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac\",\"name\":\"Rahul\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g\",\"caption\":\"Rahul\"},\"description\":\"I'm Rahul, a web enthusiast and the person responsible for all the content you read here on W3Bits.\",\"url\":\"https:\/\/w3bits.com\/author\/meadminrahul9\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Sprites: Creating PNG Sprites and Using with CSS","description":"A beginner's guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/w3bits.com\/css-sprites\/","og_locale":"en_US","og_type":"article","og_title":"CSS Sprites: Creating PNG Sprites and Using with CSS","og_description":"A beginner's guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.","og_url":"https:\/\/w3bits.com\/css-sprites\/","og_site_name":"W3Bits","article_published_time":"2018-06-16T01:08:01+00:00","article_modified_time":"2021-09-17T06:13:17+00:00","og_image":[{"width":200,"height":200,"url":"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg","type":"image\/jpeg"}],"author":"Rahul","twitter_card":"summary_large_image","twitter_creator":"@w3bits_","twitter_site":"@w3bits_","twitter_misc":{"Written by":"Rahul","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/w3bits.com\/css-sprites\/","url":"https:\/\/w3bits.com\/css-sprites\/","name":"CSS Sprites: Creating PNG Sprites and Using with CSS","isPartOf":{"@id":"https:\/\/w3bits.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/w3bits.com\/css-sprites\/#primaryimage"},"image":{"@id":"https:\/\/w3bits.com\/css-sprites\/#primaryimage"},"thumbnailUrl":"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg","datePublished":"2018-06-16T01:08:01+00:00","dateModified":"2021-09-17T06:13:17+00:00","author":{"@id":"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac"},"description":"A beginner's guide to start using CSS sprites, why they are useful for UX and SEO, and free tools to create PNG sprite sheets.","breadcrumb":{"@id":"https:\/\/w3bits.com\/css-sprites\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/w3bits.com\/css-sprites\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/w3bits.com\/css-sprites\/#primaryimage","url":"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg","contentUrl":"https:\/\/w3bits.com\/wp-content\/uploads\/css-sprites.jpg","width":200,"height":200,"caption":"CSS Sprites"},{"@type":"BreadcrumbList","@id":"https:\/\/w3bits.com\/css-sprites\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/w3bits.com\/"},{"@type":"ListItem","position":2,"name":"CSS Sprites: Creating PNG Sprites and Using with CSS"}]},{"@type":"WebSite","@id":"https:\/\/w3bits.com\/#website","url":"https:\/\/w3bits.com\/","name":"W3Bits","description":"Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/w3bits.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac","name":"Rahul","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/w3bits.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g","caption":"Rahul"},"description":"I'm Rahul, a web enthusiast and the person responsible for all the content you read here on W3Bits.","url":"https:\/\/w3bits.com\/author\/meadminrahul9\/"}]}},"_links":{"self":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/4711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/comments?post=4711"}],"version-history":[{"count":27,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/4711\/revisions"}],"predecessor-version":[{"id":4768,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/4711\/revisions\/4768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/media\/4761"}],"wp:attachment":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/media?parent=4711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/categories?post=4711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/tags?post=4711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}