{"id":51,"date":"2008-03-10T07:00:47","date_gmt":"2008-03-10T12:00:47","guid":{"rendered":"http:\/\/cssnewbie.com\/combating-classitis\/"},"modified":"2008-03-10T07:00:47","modified_gmt":"2008-03-10T12:00:47","slug":"combating-classitis","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/combating-classitis\/","title":{"rendered":"Combating Classitis with Cascades and Sequential Selectors"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/stethoscope-500.jpg' alt='stethoscope' title='Photo by Deney. Used under a Creative Commons license.' \/><\/p>\n<p>There is a disease out there in the CSS world. It can afflict anything from the meanest weblog (or the nicest ones too, I suppose) to the greatest of corporate websites. It\u2019s called Classitis, and I\u2019ve encountered it far too often in my professional work. Perhaps you\u2019ve seen it too. It looks a little something like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">body {\n\tfont-family: Arial; }\nh1 {\n\tfont-family: \"Times New Roman\"; }\nh2 {\n\tfont-family: \"Times New Roman\"; }\np {\n\tfont-family: Arial; }\np.ArialRed {\n\tfont-family: Arial;\n\tcolor: red; }\np.ArialRedBig {\n\tfont-family: Arial;\n\tcolor: red;\n\tfont-size: 150%; }\nul {\n\tfont-family: Arial; }\nul.ArialRed {\n\tfont-family: Arial;\n\tcolor: red; }\nstrong.redArial {\n\tfont-family: Arial;\n\tcolor: red; }<\/pre>\n<p>This particular snippet of (completely fictional) horribly afflicted CSS suffers from multiple problems which could all probably be considered symptomatic of Classitis, even when they don\u2019t directly involve classes. I\u2019ll go through the five of the most glaring problems and how to remedy them below.<\/p>\n<p><strong>The Cascading ability of CSS is ignored.<\/strong> Notice how many times the \u201cfont-family: Arial;\u201d rule is applied? This is a waste of space, and ignores the fantastic cascading ability of CSS (which I\u2019ve <a href=\"\/what-is-this-css-thing-anyway\/\">covered elsewhere<\/a>). Once the appropriate font family had been applied to the body tag (the first rule in our example), all of the additional \u201cArial\u201d family declarations, because they were identical and were inherited from this first declaration, became redundant.<br \/>\n<strong><br \/>\nSimple and identical rules are repeated. <\/strong>Take a look at the rules for the &lt;h1&gt; and &lt;h2&gt; tags in the example above. They use a different font-family than the body tag, so it\u2019s appropriate that they have a rule making that change. But why repeat the rule twice? With CSS, you\u2019re allowed to list sequential selectors, separated by a comma. Instead of the rule above, we could\u2019ve written this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">h1, h2 {\n\tfont-family: \"Times New Roman\"; }<\/pre>\n<p><strong>Different classes have an identical effect.<\/strong> Consider the rules \u201cArialRed\u201d and \u201cRedArial\u201d above. They do exactly the same thing, but the designer was in such a hurry, they accidentally created two rules that mirror one another. Always stop and consider when you\u2019re writing your CSS if there\u2019s any way to consolidate your rules. If you have to write the same rules multiple times, the answer is probably \u201cyes.\u201d<\/p>\n<p><strong>Classes are applied to multiple elements.<\/strong> There are two instances of the \u201cArialRed\u201d class in the example above. One is being applied to a paragraph tag, while the other is being applied to an unordered list. There\u2019s no need for this redundancy. By eliminating the \u201cp\u201d and \u201cul\u201d portions of the two selectors, those classes can be consolidated into a single instance.<\/p>\n<p><strong>Class names are super long. <\/strong>This is the classic symptom of Classitis from whence the name derives, much like how chicken pox is named after the chicken-shaped virus that causes it (no? Well, you figure it out then). Designers sometimes get in a hurry and start building classes for every possible design difference they encounter. This can most easily be combated by remembering two things:<\/p>\n<p>First, as I\u2019ve mentioned before, <a href=\"\/css-rules-multiplicity\/\">elements can have multiple classes<\/a>. Instead of having a class of \u201cRedBig,\u201d why not break it into two classes? Then you can style elements that need both classes like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;p class=\"red big\"&gt;\n  This text is now red and big.\n&lt;\/p&gt;<\/pre>\n<p>Second, classes should describe what an element <em>does<\/em>, not what it <em>looks like<\/em>. A class of \u201cred\u201d is great when you\u2019re first building a site, but what happens three months down the road when you decide that you want your emphasized text to be blue? A class of \u201c.highlight\u201d or something similar to indicate the text should stand out in some way would have been more appropriate. Of course, even seasoned developers tend to break this rule every once in a while.<\/p>\n<p>So, after we apply all of the advice offered above, what are we left with? Something that looks a little like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">body {\n\tfont-family: Arial; }\nh1, h2 {\n\tfont-family: \"Times New Roman\"; }\n.highlight {\n\tcolor: red; }\n.shout {\n\tfont-size: 150%; }<\/pre>\n<p>Ahh&#8230; nice, healthy CSS, without all of the bloating Classitis inevitably causes. <\/p>\n<p>Of course, these are just a few of the pitfalls of redundancy and excess that designers can fall into when developing their CSS. If you can think of others, share them in the comments.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>There is a disease out there in the CSS world. It can afflict anything from the meanest weblog (or the nicest ones too, I suppose) to the greatest of corporate websites. It\u2019s called Classitis, and I\u2019ve encountered it far too [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/combating-classitis\/\" title=\"Click to read 'Combating Classitis with Cascades and Sequential Selectors'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[182,192],"tags":[220,221,149,365],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}