{"id":19,"date":"2011-11-30T13:48:16","date_gmt":"2011-11-30T13:48:16","guid":{"rendered":"https:\/\/www.peopleperhour.com\/engineering\/?p=19"},"modified":"2014-12-15T15:54:44","modified_gmt":"2014-12-15T15:54:44","slug":"sprites-and-the-before-element","status":"publish","type":"post","link":"https:\/\/www.peopleperhour.com\/engineering\/2011\/11\/30\/sprites-and-the-before-element\/","title":{"rendered":"Sprites and the :before element"},"content":{"rendered":"<p>Lets suppose you have a big sprite with icons place not only vertically, but also horizontaly, effectively creating a complex grid. Now lets suppose you want to create an &lt;a&gt; tag with an icon on the left side, and some text after that.<\/p>\n<p>If you were to use a single icon, and not a sprite, you code would look something like this (px values are random):<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"hello\">&lt;a&gt; class=\"with-bg-icon\"&gt;Hello there!&lt;\/a&gt;<\/pre>\n<pre class=\"lang:css decode:true\" title=\"with-bg-icon\">.with-bg-icon {\r\npadding-left: 16px;\r\nbackground: url('icons\/sprite.png') no-repeat 0 -32px;\r\n}<\/pre>\n<p>If you try this with a complex sprite like the one described above, it wont work, as you&#8217;ll see all the icons placed on the right of the icon you need. The solution is to use the :before pseudo-element, and finally have something like this:<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"hello\">&lt;a&gt; class=\"with-bg-icon\"&gt;Hello there!&lt;\/a&gt;<\/pre>\n<pre class=\"lang:css decode:true\" title=\"with-bg-icon\">.with-bg-icon:before {\r\nwidth:16px;\r\nheight:14px;\r\nbackground: url('icons\/sprite.png') no-repeat 0 -32px;\r\ndisplay: inline-block;\r\ncontent:\"\\0020\";\r\nmargin-right: 3px;\r\n}<\/pre>\n<p>What :before does, is actually create an element inside the &lt;\/a&gt; tag, before anything else. We just set that element to inline-block and adjust the dimensions and everything looks great.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets suppose you have a big sprite with icons place not only vertically, but also horizontaly, effectively creating a complex grid. Now lets suppose you want to create an &lt;a&gt; tag with an icon on the left side, and some text after that. If you&#8230;<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[5,3,4],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-css-2","tag-background","tag-css","tag-sprites"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2CA4w-j","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":12,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/19\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}