{"id":24,"date":"2008-02-11T07:00:40","date_gmt":"2008-02-11T13:00:40","guid":{"rendered":"http:\/\/cssnewbie.com\/css-rules\/css-rules-multiplicity\/"},"modified":"2008-02-11T07:00:40","modified_gmt":"2008-02-11T13:00:40","slug":"css-rules-multiplicity","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/css-rules-multiplicity\/","title":{"rendered":"The 4 CSS Rules of Multiplicity"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/02\/multiplicity.jpg' alt='' \/><\/p>\n<p>One quick and easy way to keep your CSS clean and well-structured is to remember (what I\u2019m going to title) the four CSS Rules of Multiplicity. They are:<\/p>\n<ul>\n<li>Multiple declarations can live in a single rule.<\/li>\n<li>Multiple selectors can preface the same rule set.<\/li>\n<li>Multiple rules can be applied to the same selector.<\/li>\n<li>Multiple classes can be set on a single element.<\/li>\n<\/ul>\n<p><strong>Multiple declarations<\/strong> in a single rule is the most fundamental of the four CSS Rules of Multiplicity. Simply stated, it means you can have as many CSS declarations as you want between your opening and closing braces, like so:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">body {\n\tproperty-1: value;\n\tproperty-2: value;\n\t...\n\tproperty-infinity: value;\n}<\/pre>\n<p><strong>Multiple selectors<\/strong> can really help keep your CSS clean by grouping your rules together. Consider the following example: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p {\n\tfont-size: 110%;\n\tcolor: #333;\n}\nul {\n\tfont-size: 110%;\n\tcolor: #333; \n}<\/pre>\n<p>When rules are identical like this, you can combine them by using sequential selectors, like so: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p, ul {\n\tfont-size: 110%;\n\tcolor: #333;\n}<\/pre>\n<p>But what happens when you have two selectors that have very similar, but not quite identical, properties? That\u2019s when <strong>multiple rules<\/strong> come into play. Here\u2019s an example: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p {\n\tfont-size: 110%;\n\tcolor: #333;\n}\nul {\n\tfont-size: 110%;\n\tcolor: #333;\n\tfont-weight: bold;\n}<\/pre>\n<p>The paragraph and unordered list share a few properties in common, but not all. But you\u2019re allowed to refer to the unordered list more than once, so you could write something like this: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p, ul {\n\tfont-size: 110%;\n\tcolor: #333;\n}\nul {\n\tfont-weight: bold;\n}<\/pre>\n<p>Another way to tackle having multiple rules in common across elements is to create <strong>multiple classes<\/strong>. For example, you could create a rule like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">.container {\n\tpadding: 5px;\n\tborder: 2px solid #ccc;\n\tbackground-color: #f2f2f2;\n}<\/pre>\n<p>But then a while later, you decide you want something that is almost the same as your container class, but the text within is larger and boldfaced. You might be tempted to write a completely new class that includes the padding, border, background, and font size and weight that you need. But instead, you could just create a second class containing just the differences between the two, like this: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">.strong {\n\tfont-size: 150%;\n\tfont-weight: bold;\n}<\/pre>\n<p>And then apply both classes to your XHTML element thusly:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;div class=\"container strong\"&gt;\n\tThis text is in our container,\n\tbut is also big and bold.\n&lt;\/div&gt;<\/pre>\n<p>By using all four of these examples of multiplicity together, you can keep your CSS looking lean and clean. And luckily, unlike <a href=\"http:\/\/imdb.com\/title\/tt0117108\/\">the movie version of Multiplicity<\/a>, Michael Keaton doesn\u2019t get dumber the more often you use them.<\/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>One quick and easy way to keep your CSS clean and well-structured is to remember (what I\u2019m going to title) the four CSS Rules of Multiplicity. They are:<\/p>\n<p>Multiple declarations can live in a single rule.<br \/>\nMultiple selectors can preface the same [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/css-rules-multiplicity\/\" title=\"Click to read 'The 4 CSS Rules of Multiplicity'\">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":[168,173,182],"tags":[261],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/24"}],"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=24"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}