{"id":8,"date":"2007-12-27T10:56:17","date_gmt":"2007-12-27T16:56:17","guid":{"rendered":"http:\/\/cssnewbie.com\/2007\/12\/how-to-write-a-css-rule\/"},"modified":"2007-12-27T10:56:17","modified_gmt":"2007-12-27T16:56:17","slug":"how-to-write-a-css-rule","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-write-a-css-rule\/","title":{"rendered":"How to Write a CSS Rule"},"content":{"rendered":"<p>So if you\u2019ve read \u201cWhat is this CSS thing, anyway?\u201d (which I\u2019d recommend doing), you have a basic understanding as to what CSS is on a theoretical level. And I\u2019m sure you\u2019re a better person for it. But what about the practical details? How do you actually put together a CSS rule that will style your HTML? Read on, dear friend. Read on.<\/p>\n<p class=\"pullquote\">The styntax of CSS is extremely simple to understand.<\/p>\n<p><strong>The syntax of CSS<\/strong> is extremely simple to understand. A CSS file is essentially a list of rules. And each of those rules is comprised of two basic parts: a selector and one or more declarations. Each declaration also consists of two parts: a property and a value. In the end, every rule ends up looking like this:<\/p>\n<p><code>selector {<br \/>\nproperty: value;<br \/>\nproperty: value;<br \/>\n...<br \/>\nproperty: value;<br \/>\n}<br \/>\n<\/code><\/p>\n<div class=\"sidead\">\n<script type=\"text\/javascript\"><!--\ngoogle_ad_client = \"pub-3282985160484862\";\n\/\/Left Side, 120x600\ngoogle_ad_slot = \"0737619794\";\ngoogle_ad_width = 120;\ngoogle_ad_height = 600;\n\/\/--><\/script><br \/>\n<script type=\"text\/javascript\"\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\n<\/script>\n<\/div>\n<p>Easy, right? You lead off with your selector, open up a curly brace to contain all your declarations, give a property, followed by a colon, followed by one or more values that you want attributed to that property, and then end the declaration with a semicolon to signal you\u2019re done.  Then you close the whole thing with a closing curly brace and move on to your next selector. You don\u2019t even have to format your text the way I have above: the spaces are purely to make it easier for we humans to read. Your HTML page would do just fine if all your rules were on the same line. I wish my grocery list were this straightforward.<\/p>\n<p>That\u2019s all there is to know about the syntax of CSS. The rest is largely learning how selectors work, figuring out which properties to use, and learning which values create which results. I\u2019ll offer a few pointers below.<\/p>\n<p><strong>Selectors:<\/strong> These can be page elements (like \u2018strong\u2019 for the &lt;strong&gt; tag), IDs, classes, and even pseudo-elements or any combination of these (watch for an article explaining how to use IDs and classes in the near future). This means that selectors can get a little complex at times:<\/p>\n<p><code><br \/>\ndiv.message p#alert strong a:hover {<br \/>\ncolor: red;<br \/>\n}<br \/>\n<\/code><\/p>\n<p>But all this rule is saying is that if there is a div with a class of \u201cmessage,\u201d containing a paragraph with an ID of \u201calert,\u201d containing a strong tag, containing an anchor tag that is being hovered over (the cursor is over the link), then that link should be red. Once you understand the basic principles behind reading a CSS selector, rules like this become very easy to create.<\/p>\n<p><strong>Declarations:<\/strong> Unlike the infinite variety possible in selectors, there are a finite number of properties and values, which are determined by the <a href=\"http:\/\/www.w3.org\/\">World Wide Web Consortium (W3C)<\/a> as part of the CSS Specification. In \u201cUnderstanding the CSS Box Model,\u201d we covered the properties of border, background, padding, width, and margin. To see a full list of CSS properties, values, and how they interact, check out this excellent list of printable PDF cheat sheets.<\/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>So if you\u2019ve read \u201cWhat is this CSS thing, anyway?\u201d (which I\u2019d recommend doing), you have a basic understanding as to what CSS is on a theoretical level. And I\u2019m sure you\u2019re a better person for it. But what about [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-write-a-css-rule\/\" title=\"Click to read 'How to Write a CSS Rule'\">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":[173,175],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/8"}],"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=8"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}