{"id":42,"date":"2008-03-03T07:00:30","date_gmt":"2008-03-03T13:00:30","guid":{"rendered":"http:\/\/cssnewbie.com\/organized-style-sheet\/"},"modified":"2008-03-03T07:00:30","modified_gmt":"2008-03-03T13:00:30","slug":"organized-style-sheet","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/organized-style-sheet\/","title":{"rendered":"5 Steps to a More Organized Style Sheet"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/organized-css.jpg' title='Photo by yvestown. Used under a Creative Commons license.' alt='organized bookcase' \/><\/p>\n<p>One of the nice things about languages like CSS is that you don\u2019t have to write them in any specific way. For example, you could place all the CSS rules for your entire website on a single line of text, and assuming you had some brackets and semicolons stuck in there at appropriate intervals, your website would render without a hitch.<\/p>\n<p>Of course, finding a single, specific rule in that amalgam of CSS soup would be a bit difficult, and that starts to negate the benefits of using CSS in the first place. Luckily, we have the opportunity to use the flexibility inherent in writing CSS for good instead of evil (or at least preparedness instead of poor planning). Here are a few tips that can make your CSS much easier to read \u2013 and therefore edit \u2013 in the long run.<\/p>\n<h3>Pick a Formatting Style<\/h3>\n<p>When I\u2019m writing example CSS for someone to follow, I tend to use a more traditional formatting for my rules, like so:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">body {\n\tfont-size: small;\n\tcolor: #333;\n}\np {\n\tmargin: 1em 0;\n}<\/pre>\n<p>However, for all its ubiquity, I don\u2019t really care for this style. My eye tends to get distracted by the closing bracket on a line all by itself, and I consider it a bit wasteful to spend an entire line to say, \u201cOkay, I\u2019m done now,\u201d when CSS lets us put that bracket wherever we darn well please. So when I write CSS for my own projects, I write it like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">body {\n\tfont-size: small;\n\tcolor: #333;}\np {\n\tmargin: 1em 0;}<\/pre>\n<p>The only real change here is that I\u2019ve moved the closing brackets up to the preceding line, but it helps me a lot. Now my eye knows that anything in the far left is a selector, and anything indented is a property. I\u2019ve also saved myself two lines of code. Now, I\u2019m not saying this style is right for everyone, but the main idea here is to find a style that works well for you and <strong>stick with it<\/strong>. Once your eye gets used to seeing your CSS written in a specific way, scanning your code gets a lot easier.<\/p>\n<h3>Section Your Code<\/h3>\n<p>CSS has built-in comment tags, which look like this: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">\/* This is a comment! *\/\np {\n\tmargin: 1em 0;}<\/pre>\n<p>Anything that appears before the opening comment tag and the closing tag, no matter how many lines it spans, is considered a comment. These little tags are great for putting up little notes to define sections of your code. I personally use comments as heading tags to define sections of my website. For example, I might have one section titled \/* Sidebar *\/, where I put all of my sidebar-related styles, or one called \/* Article *\/, where all my article-specific CSS resides. <\/p>\n<p>You can get as general or as granular as you want or need with this technique, depending on the size and complexity of your stylesheet. If I\u2019m the only one who\u2019s ever likely to edit the CSS, I tend to get away with fewer comment tags than I would if I knew that other people would be trying to decipher my code.<\/p>\n<h3>Use a Table of Contents<\/h3>\n<p>If you end up creating a massively large and complex stylesheet, containing several hundred or even a couple thousand lines of code, simply breaking that code up into a dozen or so sections might not be enough. On my biggest projects, I tend to create a Table of Contents at the top of my CSS to help me remember how my CSS is organized, and help me (or someone else) find things later on down the line. A table of contents might look something like this: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">\/* Table of Contents:\n\t1. Reset Styles\n\t2. Baseline (Default) Styles\n\t3. Column Structure\n\t4. Main Column (articles, etc)\n\t5. Comments and Pingbacks\n\t6. Sidebar lists and Ads\n\t7. Footer \n*\/<\/pre>\n<p>Then you can just insert appropriately named comment headings along the way to help you or someone else make their way through your document. Scanning code gets a lot easier when you know you\u2019re looking for section #5 and that there will be a comment tag to let you know when you\u2019ve arrived.<\/p>\n<h3>Group Selectors with Common Values<\/h3>\n<p>Let\u2019s say you have five levels of heading tags in your articles, h1 \u2013 h5. They are all rendered in Verdana, but at different sizes and colors. Obviously, you\u2019re going to need more than once rule to style all these elements, but that isn\u2019t to say you can\u2019t group their common attributes together. For example, instead of writing this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">h1 {\n\tfont-family: Verdana, sans-serif;\n\tfont-size: 200%; }\nh2 {\n\tfont-family: Verdana, sans-serif;\n\tfont-size: 175%;\n\tcolor: #333; }\nh3 {\n\tfont-family: Verdana, sans-serif;\n\tfont-size: 150%;\n\tcolor: #f00; }\n\/* And so on\u2026 *\/<\/pre>\n<p>You could make everything a lot cleaner and easier to edit in the future by writing this instead: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">h1, h2, h3, h4, h5, h6 {\n\tfont-family: Verdana, sans-serif; }\nh1 {\n\tfont-size: 200%; }\nh2 {\n\tfont-size: 175%;\n\tcolor: #333; }\nh3 {\n\tfont-size: 150%;\n\tcolor: #f00; }\n\/* And so on\u2026 *\/<\/pre>\n<h3>Group Default Styles<\/h3>\n<p><a href=\"http:\/\/www.businesslogs.com\/blog_design\/my_5_css_tips.php\">Mike Rundle of BusinessLogs recently argued<\/a> that using default styles (for example, styling all your anchor tags at once) is counter-productive, because when an element isn\u2019t behaving like you\u2019d expect it to later on down the line, you can spend a lot of time trying to \u201cfix\u201d your code unnecessarily because you\u2019ve forgotten about a default style you\u2019d set previously.<\/p>\n<p>I agree to a point: I would say that you shouldn\u2019t <em>overly<\/em> style general elements. Don\u2019t style your anchors as bright red with a dotted border unless 99% of your links are going to appear that way. However, if none of your links in your entire site are going to have an underline, I don\u2019t see the harm in removing the underline from your anchor tags. Because here\u2019s the important thing to remember: <em>all<\/em> browsers have a default style sheet. Even if you don\u2019t apply a single style to your anchor tags, they\u2019re going to have a style (blue, underlined) to begin with. Using default styles is a means of establishing a baseline more conducive to your overall site goals.<\/p>\n<p>But you can make your life a little easier by grouping all of your default styles. I tend to put them all up at the top of my document, labeled with a comment-header. That way, if (to continue the example) my anchor tags aren\u2019t behaving like I expected a few hundred lines of CSS later, I can just check really quick to make sure my default styles aren\u2019t interfering in some unexpected way (but since they\u2019re only baseline styles, that\u2019s rarely the case). <\/p>\n<p>These are just five tips that have helped me keep my CSS a bit more organized. Do you have any tips that have helped you streamline your CSS?<\/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 of the nice things about languages like CSS is that you don\u2019t have to write them in any specific way. For example, you could place all the CSS rules for your entire website on a single line of text, [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/organized-style-sheet\/\" title=\"Click to read '5 Steps to a More Organized Style Sheet'\">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":[189,192],"tags":[45,312,352,366],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/42"}],"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=42"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}