{"id":49831,"date":"2017-07-11T09:37:37","date_gmt":"2017-07-11T04:07:37","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=49831"},"modified":"2017-09-12T12:43:01","modified_gmt":"2017-09-12T07:13:01","slug":"10-best-practices-in-css","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/10-best-practices-in-css\/","title":{"rendered":"10 Best Practices in CSS"},"content":{"rendered":"<p>If you are inquisitive about learning and implementing CSS, then you must do it the right way, the way professionals do.<\/p>\n<p>Sometimes it\u2019s the result of sloppy committal to writing from the beginning, and thanks to various hacks and changes over time.<\/p>\n<p>CSS could be a language that is employed by almost each developer for some purpose. Moreover, it is a language for which we have a tendency to typically view as granted, it&#8217;s powerful and has several nuances that may facilitate (or damage) our styles.<\/p>\n<p><strong>1. Create it Readable<\/strong><\/p>\n<p>The readability of your CSS is necessary, although the general public overlooks its importance. Nice readability of your CSS makes it a lot easier to keep up with the future, as you will be able to notice components faster.<\/p>\n<p><strong>Note:<\/strong>\u00a0Usually while\u00a0writing CSS, most developers adopt one of the following approaches.<\/p>\n<p><strong>Approach 1:<\/strong> All on one line as below:<\/p>\n<p>[java]<br \/>\n.example{background:red;padding:2em;border:1px solid black;}<br \/>\n[\/java]<\/p>\n<p><strong>Approach\u00a02:<\/strong> Each style gets its own line as below:<\/p>\n<p>[java]<br \/>\n.example {<br \/>\n  background: red;<br \/>\n  padding: 2em;<br \/>\n  border: 1px solid black;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Both practices square measure is acceptable, although you may usually notice that way two despises way one! Simply keep in mind &#8211; opt for the tactic that appears best TO YOU. This is all that matters.<\/p>\n<p><strong>2. Use Reset and Normalize CSS<\/strong><\/p>\n<p>Most CSS frameworks have an option to reset them. If you are not aiming to use the existing framework reset CSS \u00a0one, then a better option will be to set them to reset or normalize. Resets or Normalize CSS eliminate browser inconsistencies like heights, font sizes, margins, headings, etc. The reset permits your layout to look consistent all told browsers.<\/p>\n<p>Find different type of Reset CSS from here: http:\/\/cssreset.com\/<\/p>\n<p><strong>3. Organize the Stylesheet<\/strong><\/p>\n<p>It is smart to put your stylesheet come in the simplest way that permits you to realize elements of your code quickly. I like to recommend a top-down format that tackles designs as they seem within the ASCII text file. So, an associate degree example stylesheet could be ordered like this:<\/p>\n<p>i) Generic Element (body, a, p, h1, etc.)<br \/>\nii) .header<br \/>\niii) .nav<br \/>\niv) .wrapper<\/p>\n<p>Also, use commented CSS like below:<\/p>\n<p>[java]<br \/>\n\/****** header ******\/<br \/>\nstyles goes here&#8230;<br \/>\n\/****** navigation ******\/<br \/>\nstyles goes here&#8230;<br \/>\n\/****** main content ******\/<br \/>\nstyles goes here&#8230;<br \/>\n\/****** footer ******\/<br \/>\nstyles go here&#8230;<br \/>\n[\/java]<\/p>\n<p><strong>4. Combine CSS Elements<\/strong><\/p>\n<p>Elements during a stylesheet typically share properties. Rather than re-writing previous code, why not simply mix them? As an example, your h1, h2, and h3 components would possibly share identical font and color. Hence you can combine the same:<\/p>\n<p>[java]<br \/>\nh1, h2, h3 {<br \/>\n\tfont-family: verdana;<br \/>\n\tcolor: #e1e1e1;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>5. Use Appropriate Naming Convention<\/strong><\/p>\n<p>One of the main advantages of CSS is the ability to separate designs from content. You\u2019ll be able to <a title=\"Front-end Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">change the\u00a0entire design<\/a> of your website by simply modifying the CSS without ever touching the markup language. Therefore don\u2019t create your CSS by using limiting names. Use additional versatile naming conventions and keep consistent.<\/p>\n<p><b>The naming of your CSS elements is based on what they&#8217;re, not what they give the impression of being like. <\/b>For instance, <em>&#8220;.comment-blue&#8221;<\/em> is not appropriate than <em>&#8220;.comment-beta&#8221;<\/em> (because if you need to change the color of this class, then you only need to change in CSS, not in HTML), and <em>&#8220;.post-large-font&#8221;<\/em> is additional limiting than <b><em>&#8220;.post-title&#8221;<\/em>.<\/b><\/p>\n<p><strong>6. Always avoid inline styling<\/strong><\/p>\n<p>Using inline styling show the HTML code messy. Updating each HTML events style through global CSS is hard. So you always need to avoid inline style. Below is the example related to this style:<\/p>\n<p><em>Bad Code:<\/em><\/p>\n<p>[java]<br \/>\n&lt;div style=&quot;float:left&quot;&gt;<br \/>\n\t&lt;img src=&quot;images\/logo.gif&quot; alt=&quot;&quot; \/&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p><em>Good Code:<\/em><\/p>\n<p>[java]<br \/>\n&lt;div class=&quot;left&quot;&gt;<br \/>\n\t&lt;img src=&quot;images\/logo.gif&quot; alt=&quot;&quot; \/&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p><strong>7. Always use External CSS<\/strong><\/p>\n<p>The use of external CSS is beneficial. You can pull all the CSS in an external file and include this files in your HTML.<\/p>\n<p><em>Benefits of External CSS:<\/em><\/p>\n<ul>\n<li>All the CSS are stored within single Stylesheet.<\/li>\n<li>All the style changes are done in a single CSS for each page (Easier to maintain the website).<\/li>\n<li>Single CSS will be cached and page load faster.<\/li>\n<li>Due to the use of external CSS, we can differentiate the CSS like web page layout CSS and Print-friendly CSS.<\/li>\n<\/ul>\n<p><b>Use different version external stylesheet in your HTML as follows:<\/b><\/p>\n<p>[java]<br \/>\n&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text\/css&quot; media=&quot;screen&quot; \/&gt;<br \/>\n&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text\/css&quot; media=&quot;print&quot; \/&gt;<br \/>\n[\/java]<\/p>\n<p><strong>8. Shorthand CSS<\/strong><\/p>\n<p>One feature of CSS is the ability to use shorthand properties and values. Most properties and values have acceptable shorthand alternatives.<\/p>\n<p><em>Example:<\/em><\/p>\n<p>[java]<br \/>\nimg {<br \/>\n\tmargin-top: 5px;<br \/>\n\tmargin-right: 10px;<br \/>\n\tmargin-bottom: 5px;<br \/>\n\tmargin-left: 10px;<br \/>\n}<\/p>\n<p>button {<br \/>\n\tpadding: 0 0 0 20px;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Instead of above we can use shorthand CSS as follows:<\/p>\n<p>[java]<br \/>\nimg {<br \/>\n\tmargin: 5px 10px;<br \/>\n} <\/p>\n<p>button {<br \/>\n\tpadding-left: 20px;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><em>Also:<\/em><\/p>\n<p>[java]<br \/>\n.module {<br \/>\n\tbackground: #DDDDDD;<br \/>\n\tcolor: #FF6600;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Instead of above we can use shorthand CSS as follows:<\/p>\n<p>[java]<br \/>\n.module {<br \/>\n\tbackground:#ddd;<br \/>\n\tcolor:#f60;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>9. Minify CSS file size with CSS Compressors<\/strong><\/p>\n<p>It&#8217;s an excellent thought to minify the CSS file to reduce the file size. Through this, you can help browsers to accelerate the stacking of your CSS codes. So you can use a tool like CSS Compressor and Minifier to get this going.<\/p>\n<p>You can minify your CSS here: https:\/\/csscompressor.net\/<\/p>\n<p><strong>10. Cross-browser compatibility:<\/strong><\/p>\n<p>When you use an external stylesheet (where we can use browser engine prefix like -moz-, -webkit-, -o- and -ms-) for layout and valid markup (XHTML, HTML5), then your web pages work well on all browsers such as IE, Opera, Chrome, Mozilla, and Safari, etc..<\/p>\n<p>Also following are the prefix description:<\/p>\n<p>[java]<br \/>\n-moz- \/* this is use for Firefox browser*\/<br \/>\n-webkit- \/*this is use for chrome and safari browsers*\/<br \/>\n-o- \/*this is use for opera browser*\/<br \/>\n-ms- \/*this is use for Internet Explorer (but not always it&#8217;s depends on CSS3 browser support)*\/<br \/>\n[\/java]<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>We have listed out ten best practices which every front-end and back-end developer follows. Also if the beginner level developer follows these guidelines, they will not miss a single point while they are writing the CSS.<\/p>\n<p>This approach is quite practical for both front-end and back-end developers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are inquisitive about learning and implementing CSS, then you must do it the right way, the way professionals do. Sometimes it\u2019s the result of sloppy committal to writing from the beginning, and thanks to various hacks and changes over time. CSS could be a language that is employed by almost each developer for [&hellip;]<\/p>\n","protected":false},"author":206,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":100},"categories":[3429,1],"tags":[245,4622,4452,4842,4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49831"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/206"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=49831"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49831\/revisions"}],"predecessor-version":[{"id":53727,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49831\/revisions\/53727"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=49831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=49831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=49831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}