{"id":157,"date":"2011-04-04T17:18:26","date_gmt":"2011-04-04T17:18:26","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=157"},"modified":"2011-04-04T17:18:26","modified_gmt":"2011-04-04T17:18:26","slug":"css-sprites-the-elegant-way","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/css-sprites-the-elegant-way\/","title":{"rendered":"CSS Sprites &#8211; the elegant way"},"content":{"rendered":"<p>I was trying to come up with a way to use image sprites for background images, without needing to specify a ton of class names.<\/p>\n<p>The usual method would be:<\/p>\n<p><code>&lt;ul&gt;<br \/>\n\t&nbsp; &lt;li class=&quot;class1&quot;&gt;&lt;a href=&quot;&quot;&gt;Item 1&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&nbsp; &lt;li class=&quot;class2&quot;&gt;&lt;a href=&quot;&quot;&gt;Item 2&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&nbsp; &lt;li class=&quot;class3&quot;&gt;&lt;a href=&quot;&quot;&gt;Item 3&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&lt;\/ul&gt;<br \/>\n\t<\/code><\/p>\n<p><code>&lt;style&gt;<br \/>\n\tul li a { background: url(&#39;icons.png&#39;) no-repeat; }<br \/>\n\tul li.class1 a { background-position: 0 -30px; }<br \/>\n\tul li.class2 a { background-position: 0 -60px; }<br \/>\n\tul li.class3 a { background-position: 0 -90px; }<br \/>\n\t&lt;\/style&gt;<br \/>\n\t<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>But, by leveraging the next sibling CSS selector, we can avoid the need for classes or ids to identify our elements.<\/p>\n<p><code>&lt;ul&gt;<br \/>\n\t&nbsp; &lt;li&gt;&lt;a href=&quot;&quot;&gt;Item 1&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&nbsp; &lt;li&gt;&lt;a href=&quot;&quot;&gt;Item 2&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&nbsp; &lt;li&gt;&lt;a href=&quot;&quot;&gt;Item 3&lt;\/a&gt;&lt;\/li&gt;<br \/>\n\t&lt;\/ul&gt;<\/code><\/p>\n<p><code>&lt;style&gt;<br \/>\n\tul li a { background: url(&#39;icons.png&#39;) no-repeat; }<br \/>\n\tul li a { background-position: 0 -30px; }<br \/>\n\tul li + li a { background-position: 0 -60px; }<br \/>\n\tul li + li + li a { background-position: 0 -90px; }<br \/>\n\t&lt;\/style&gt;<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to come up with a way to use image sprites for background images, without needing to specify a ton of class names.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[12,14],"tags":[74,73],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/157"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}