{"id":2263,"date":"2011-04-03T19:14:35","date_gmt":"2011-04-03T19:14:35","guid":{"rendered":"http:\/\/sunpig.com\/mt-entry-2263.html"},"modified":"2012-03-27T14:34:16","modified_gmt":"2012-03-27T14:34:16","slug":"css-21-is-the-new-hotness","status":"publish","type":"post","link":"https:\/\/sunpig.com\/martin\/2011\/04\/03\/css-21-is-the-new-hotness\/","title":{"rendered":"CSS 2.1 is the new hotness"},"content":{"rendered":"<p>Forget for a moment about <code class=\"css\">border-radius<\/code>, <code class=\"css\">box-shadow<\/code>, and all the other funky CSS3 properties you can use in modern browsers. The <a href=\"http:\/\/ie6countdown.com\/\">(slow) death of IE6<\/a> is causing a quiet revolution at the <em>other<\/em> end of the browser spectrum.<\/p>\n<p>IE6 does not natively understand many significant features of CSS2.1, such as the child (<code class=\"css\">&gt;<\/code>), adjacent sibling (<code class=\"css\">+<\/code>), and attribute (<code class=\"css\">[attr]<\/code>) selectors. Because of this, many web developers (myself included) often ignore them, and work with a more highly-compatible subset of CSS. But if you have the luxury of ignoring IE6&#8211;and unfortunately it <em>is<\/em> still a luxury&#8211;then all of these cool features (and more!) suddenly come into play, and they can make your code a <em>lot<\/em> cleaner.<\/p>\n<p>My favourite illustration of CSS2.1 hotness is <strong>nested modules<\/strong>. Here&#8217;s some typical HTML:<\/p>\n<pre class=\"code\"><code class=\"html\" data-language=\"html\">&lt;div class=\"mod mod-blog\"&gt;\n  &lt;div class=\"hd\"&gt;My awesome blog&lt;\/div&gt;\n    &lt;div class=\"bd\"&gt;\n    ...\n      &lt;div class=\"mod mod-blogentry\"&gt;\n      &lt;div class=\"hd\"&gt;My awesome breakfast&lt;\/div&gt;\n      &lt;div class=\"bd\"&gt;\n        ...\n      &lt;\/div&gt;\n      &lt;div class=\"ft\"&gt;&lt;\/div&gt;\n    &lt;\/div&gt;\n  ...\n  &lt;\/div&gt;\n  &lt;div class=\"ft\"&gt;&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<p>If you use a descendant selector to style the <code class=\"html\">.hd<\/code> of the outer module, you have to be careful to override any styles for the <code class=\"html\">.hd<\/code> of the inner module, otherwise the outer styles will apply to it, too:<\/p>\n<pre class=\"code\"><code class=\"css\" data-language=\"css\">.mod-blog .hd {\n  font-size:167%;\n  font-weight:bold;\n}\n\n.mod-blogentry .hd {\n  font-size:100%;\n  font-weight:normal;\n}<\/code><\/pre>\n<p>Alternatively, you end up decorating the <code class=\"html\">.hd<\/code> element with additional classnames so that you can target the elements more specifically in your CSS (e.g. <code class=\"html\">&lt;div class=\".hd .hd-blog\"&gt;<\/code>).<\/p>\n<p>By using child a child selector, you can be much more specific in which elements you target. In this example, you can ensure that the &#8220;big and bold&#8221; style only gets applied to the header of the <em>outer<\/em> module by using the child selector:<\/p>\n<pre class=\"code\"><code class=\"css\" data-language=\"css\">.mod-blog &gt; .hd {\n  font-size:167%;\n  font-weight:bold;\n}<\/code><\/pre>\n<p>And boom, your code is a lot cleaner, and your modules can be coded in a much more modular style&#8211;without having to worry quite so much about nesting issues and inherited rules.<\/p>\n<p>Of course, even if you <em>do<\/em> have to support IE6, there is nothing to stop you from using these selectors anyway.  You can use a polyfill like <a href=\"http:\/\/selectivizr.com\/\">Selectivizr<\/a> to implement the missing support in IE, or you can provide IE6-specific overrides in a separate stylesheet, like so:<\/p>\n<pre class=\"code\"><code class=\"css\" data-language=\"css\">.ie6 .mod-blogentry .hd {\n  font-size:100%;\n  font-weight:normal;\n}<\/code><\/pre>\n<p>I don&#8217;t recommend doing this, though, because IE6 is already the slowest mainstream browser on the block. Adding additional cruft is just going to make it even slower, and give your site&#8217;s visitors an even worse experience than they already have.<\/p>\n<p>But seriously: if you&#8217;ve been drooling over rounded corners and gradients without background images, you owe it to yourself to remind yourself of these older features as well. You&#8217;ll be very pleasantly surprised by what you can do with them.<\/p>\n<h2>Further reading<\/h2>\n<ul>\n<li><a href=\"http:\/\/selectivizr.com\/\">Selectivizr<\/a>, a CSS selectors polyfill<\/li>\n<li>PPK&#8217;s <a href=\"http:\/\/www.quirksmode.org\/css\/contents.html\">CSS compatibility tables<\/a><\/li>\n<li>Microsoft&#8217;s <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc351024%28v=vs.85%29.aspx\">Internet Explorer CSS compatiblity tables<\/a><\/li>\n<li><a href=\"http:\/\/www.w3.org\/TR\/CSS2\/\">The W3C CSS2.1 specification<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Forget for a moment about border-radius, box-shadow, and all the other funky CSS3 properties you can use in modern browsers. The (slow) death of IE6 is causing a quiet revolution at the other end of the browser spectrum. IE6 does not natively understand many significant features of CSS2.1, such as the child (&gt;), adjacent sibling &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sunpig.com\/martin\/2011\/04\/03\/css-21-is-the-new-hotness\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CSS 2.1 is the new hotness&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2263","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/2263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/comments?post=2263"}],"version-history":[{"count":0,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/2263\/revisions"}],"wp:attachment":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/media?parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/categories?post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/tags?post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}