{"id":26333,"date":"2016-04-13T21:36:42","date_gmt":"2016-04-13T13:36:42","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=26333"},"modified":"2023-03-31T18:37:41","modified_gmt":"2023-03-31T10:37:41","slug":"code-optimization-series-refactoring-css","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/","title":{"rendered":"How to Refactor CSS &#8211; A Guide"},"content":{"rendered":"<p class=\"note\"><strong>Editor\u2019s note: <\/strong>This article is part of our <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/tag\/code-optimization-series\/\" rel=\"noopener\">Code Optimization series<\/a>, where we take a look at how to optimize coding for better efficiency in a bid to be better coders.<\/p>\n<p>CSS refactoring needs to be an essential part of the front-end development workflow, if we want to have a manageable and optimized code base. When we refactor CSS, we <strong>clean up and reorganize our existing code<\/strong> without adding any new features or fixing bugs.<\/p>\n<p>Refactoring helps <strong>prevent CSS explosion, improves code readability and reusability<\/strong>, and <strong>makes CSS sleeker and faster to execute<\/strong>.<\/p>\n<p>Refactoring is usually needed after a while, as even projects that started out with a concise and organized code base sooner or later begin to lose their clarity; consistency, obsolete rules and duplicate code parts appear; and we also start to override styles and employ more and more hacks and workarounds.<\/p>\n<p>The best way to keep our CSS code base maintainable is to stick to the \u201c<em>refactor early, refactor often<\/em>\u201d rule of thumb. In this post, we will take a look at some tips about how we can conduct an effective CSS refactoring process.<\/p>\n<h2>1. Conduct an Initial Audit<\/h2>\n<p>To have a better picture of what we need to refactor, it\u2019s best to <strong>start with a comprehensive audit to see what we currently have<\/strong>.<\/p>\n<p>There are many great online tools that can help us in this endeavor. <a target=\"_blank\" href=\"https:\/\/www.cssdig.com\/\" rel=\"noopener\">CSSDig<\/a> is a powerful Chrome extension that analyzes the CSS of a website, and explores its weaknesses, such as too specific selectors or repetitive properties.<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/unused-css.com\/\" rel=\"noopener\">Unused CSS<\/a> investigates unused CSS rules, while linting tools, such as <a target=\"_blank\" href=\"http:\/\/csslint.net\/\" rel=\"noopener\">CSS Lint<\/a>, make it possible to quickly find compatibility, maintainability and other issues.<\/p>\n<p>It\u2019s also important to manually scrutinize the code during the initial audit, as many problems on the architectural level can only be caught this way.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/www.cssdig.com\/\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg\" width=\"773\" height=\"561\" alt=\"Initial Audit\"><\/a><\/figure>\n<h2>2. Set Up a Manageable Plan<\/h2>\n<p>Refactoring working code is always a daunting task, but we can ease the pain if we create a plan about what we need to do, slice up the refactoring process to manageable chunks, and make a feasible schedule.<\/p>\n<p>In CSS refactoring there\u2019s a crucial thing we always need take into consideration: some things that we refactor, e.g. changing selector names, will make it <strong>necessary to adjust the code of the relevant HTML and JavaScript files<\/strong> as well.<\/p>\n<p>It\u2019s therefore a good idea to <strong>trace forward these additional modifications we\u2019ll need to perform<\/strong>, and <strong>build them into our refactoring schedule<\/strong> along with the primary, CSS-related tasks.<\/p>\n<h2>3. Track Progress<\/h2>\n<p>Before embarking on refactoring, it\u2019s an essential step to <strong>back up our initial files<\/strong>. Introducing a version control system, such as <a target=\"_blank\" href=\"https:\/\/git-scm.com\/\" rel=\"noopener\">Git<\/a> or <a target=\"_blank\" href=\"https:\/\/subversion.apache.org\/\" rel=\"noopener\">Subversion<\/a>, into our workflow can also significantly improve the refactoring process, as we\u2019ll have a registry about the sequential steps we\u2019ve taken, and we <strong>will be able to return to a previous stage if we want to redo things<\/strong>.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/git-scm.com\/\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/version-control.jpg\" width=\"1032\" height=\"655\" alt=\"Git Version Control\"><\/a><\/figure>\n<h2>4. Stick to a Coding Style Guide<\/h2>\n<p>A coherent coding style guide can remarkably improve code readability and maintainability, so before we start to refactor it\u2019s essential to <strong>set up a CSS coding style guide<\/strong>.<\/p>\n<p>The important things to decide upon are:<\/p>\n<ul>\n<li>naming conventions<\/li>\n<li>formatting rules<\/li>\n<li>declaration order<\/li>\n<li>units and values we want to use<\/li>\n<li>commenting rules<\/li>\n<\/ul>\n<p>If we don\u2019t want to create our own coding style guide, we can also use someone else\u2019s, such as ThinkUp\u2019s which can found on <a target=\"_blank\" href=\"https:\/\/github.com\/ThinkUpLLC\/ThinkUp\/wiki\/Code-Style-Guide:-CSS\" rel=\"noopener\">Github<\/a>.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/github.com\/ThinkUpLLC\/ThinkUp\/wiki\/Code-Style-Guide:-CSS\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/style-guide.jpg\" width=\"800\" height=\"573\" alt=\"Coding Style Guide\"><\/a><\/figure>\n<p>It\u2019s not enough though to just introduce a coding style guide, we also need to <strong>stick to it when we rewrite the code<\/strong> during the refactoring, and <strong>expect the same from everyone else<\/strong> who works on our project.<\/p>\n<h2>5. Set Up a Coherent File Structure<\/h2>\n<p>After we are ready with the preparations, the first thing we need to do is to set up a proper CSS file structure that pays attention to the <a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/Guide\/CSS\/Getting_started\/Cascading_and_inheritance\" rel=\"noopener\">cascading nature<\/a> of CSS.<\/p>\n<p>It mainly depends on the project how best to organize our files, but there are some universal rules, such as to use a separate <code>normalize.css<\/code> file for <a target=\"_blank\" href=\"https:\/\/cssdeck.com\/blog\/what-is-a-css-reset\/\" rel=\"noopener\">CSS reset<\/a> styles, a separate <code>global.css<\/code> for global styles that are used across the whole project, and to store 3rd party libraries in a separate folder.<\/p>\n<p>If we want to play safe with our CSS file structure, there are also ready-made architectures, such as <a target=\"_blank\" href=\"http:\/\/smacss.com\/\" rel=\"noopener\">SMACSS<\/a> or <a target=\"_blank\" href=\"https:\/\/speakerdeck.com\/dafed\/managing-css-projects-with-itcss\" rel=\"noopener\">ITCSS<\/a>, that offer effective techniques about <strong>how to organize CSS files in a scalable way<\/strong>.<\/p>\n<p class=\"note\">Find out what <strong>SMACSS<\/strong> in <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/css-writing-methodologies\/\" rel=\"noopener\">CSS Writing Methodologies<\/a><\/p>\n<h2>6. Get Rid of Unused and Duplicate Rules<\/h2>\n<p>After a while, CSS files usually begin to abound in unused rules that we need to identify and clean out during refactoring. There are many online tools that enable us to <strong>investigate these obsolete rules<\/strong>, and sometimes also allow us to quickly ditch them.<\/p>\n<p>The best-known tool for this purpose is probably <a target=\"_blank\" href=\"https:\/\/github.com\/uncss\/uncss\" rel=\"noopener\">UnCSS<\/a>, a Node.js module that makes it possible to get rid of unused CSS rules fast, and it also provides us with sophisticated configuration options that make it easy to fine-tune the cleaning process.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/github.com\/uncss\/uncss\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/uncss.jpg\" width=\"800\" height=\"553\" alt=\"UnCSS\"><\/a><\/figure>\n<p>It\u2019s important to take into account that we <strong>don\u2019t necessarily want to remove unused rules from <em>all<\/em> the CSS files we have<\/strong>, for example from global, reset, or 3rd party stylesheets, so we need to <strong>exclude them<\/strong> while performing the cleaning.<\/p>\n<p>Along with obsolete rules, duplicate rules also lead to superfluous code bloat and performance loss. We can remove them by using the <a target=\"_blank\" href=\"https:\/\/www.npmjs.com\/package\/css-purge\" rel=\"noopener nofollow\">CSS Purge<\/a> Node.js module, but we can also work with <strong>CSS linters in order to search for duplicate rules<\/strong> in our CSS files.<\/p>\n<h2>7. Reduce Specificity<\/h2>\n<p>The <a target=\"_blank\" href=\"https:\/\/www.w3.org\/TR\/css3-selectors\/#specificity\" rel=\"noopener nofollow\">specificity<\/a> of a CSS selector is calculated from the number and the types of the inner selectors it contains. CSS specificity is expressed as a 4-digit number that\u2019s the easiest to understand if we check out this <a target=\"_blank\" href=\"https:\/\/specificity.keegan.st\/\" rel=\"noopener\">visual CSS specifity calculator<\/a>:<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/specificity.keegan.st\/\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/specificity.jpg\" width=\"800\" height=\"634\" alt=\"Reduce Specificity\"><\/a>\n<\/figure>\n<p>The lowest specificity (<code>0001<\/code>) belongs to selectors that only target one general HTML element, such as <code>&lt;h1&gt;<\/code> or <code>&lt;li&gt;<\/code>. <strong>The more inner selectors a compound selector contains, the higher its specificity is.<\/strong><\/p>\n<p>Certain selectors, such as <code>id<\/code> or selectors coming from inline styles, have higher priorities because they override the styles belonging to more generic selectors. For example the specificity of the <code>#id1<\/code> selector is <code>0100<\/code>.<\/p>\n<p>In refactoring, the goal is to decrease the specificity of selectors (keep them short) as much as it\u2019s possible, as <strong>selectors with higher specificity significantly reduce selector reusability<\/strong>, and <strong>lead to a bloated code base<\/strong>.<\/p>\n<p>The 3 main types of high specificity selectors are:<\/p>\n<ol>\n<li><strong>Qualified selectors<\/strong>, such as <code>p.class1<\/code> (defining the <code>p<\/code> tag is unnecessary here, as it makes it impossible to use the same class with other HTML elements)<\/li>\n<li><strong>Deeply nested selectors<\/strong>, such as <code>.class1 .class2 .class3 .class4 ...<\/code><\/li>\n<li><strong>IDs<\/strong>, such as <code>#id1<\/code><\/li>\n<\/ol>\n<p>Online tools, like <a target=\"_blank\" href=\"https:\/\/www.cssdig.com\/\" rel=\"noopener\">CSSDig<\/a> mentioned in Step 1, can be used to quickly find these high specificity selectors. It can also be useful to set up a rule in the coding style guide about things like the maximum level of nesting, or a limit on using <code>id<\/code> selectors.<\/p>\n<h2>8. Weed Out <code>!important<\/code> Rules<\/h2>\n<p>CSS rules followed by the <code>!important<\/code> statement override regular style rules. Using <code>!important<\/code> rules sooner or later <strong>lead to incoherent code<\/strong>. It\u2019s not a coincidence most linting tools mark them as error.<\/p>\n<p>When we need to write CSS quickly, we may begin to rely on them though because of their simplicity.<\/p>\n<p>The main problem with <code>!important<\/code> declarations is that if we want to override them in the future, we need to put even more <code>!important<\/code> declarations in use, so it\u2019s best to weed them out wherever it\u2019s possible during the refactoring process.<\/p>\n<h2>9. Clean Out Magic Numbers and Hard Coded Values<\/h2>\n<p>During our everyday CSS workflow, sometimes we bump into issues we can\u2019t solve, and we begin to use so-called <strong>magic numbers<\/strong>, values that work for some reasons but we don\u2019t understand why. For instance take the following example:<\/p>\n<pre>\r\n.class1 {\r\n\tposition: absolute;\r\n\ttop: 28px;\r\n\tleft: 15.5%;\r\n}<\/pre>\n<p>The main problem with magic numbers is that they are <strong>circumstantial<\/strong>, and they embody the \u201cprogramming by permutation\u201d antipattern. During the refactoring process we need to remove these arbitrary rules from our code, and replace them with more reasonable solutions wherever it\u2019s possible.<\/p>\n<p>The same rule of thumb applies for <strong>hard-coded values<\/strong> as well. Probably the most frequent occurrence of hard-coded values can be found in line-height rules:<\/p>\n<pre>\r\n\/* bad, we'll need to add extra fixed line-height rules \r\n to the child elements of .class1 *\/\r\n\r\n.class1 {\r\n\tfont-size: 20px;\r\n\tline-height: 24px;\r\n}\r\n\r\n\/* good, the flexible line-height rule can be safely \r\nused by child elements as well *\/\r\n\r\n.class1 {\r\n\tfont-size: 20px;\r\n\tline height: 1.2;\r\n}<\/pre>\n<p>Hard-coded values make our code less future-proof and more rigid, so as part of refactoring we need to dig them up, and <strong>replace them with flexible values<\/strong>.<\/p>\n<h2>10. Refactor Units and Values<\/h2>\n<p>To make maintenance and debugging easier in the future, and to avoid failures that can stem from using different units, such as <code>em<\/code> and <code>px<\/code>, simultaneously, we need to <strong>stick to coherent rules about how we use relative and absolute values<\/strong>.<\/p>\n<p>If we used them inconsistently in the past, we need to convert them so that they can constitute a concise system<\/p>\n<p>If we use too many similar colours on our site, it can also be a wise thing to <strong>rationalize the colour scheme<\/strong> by reducing the number of colours we employ. (Here\u2019s a post on <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/website-color-scheme\/\" rel=\"noopener\">how to choose a website colour scheme in a practical manner<\/a>.)<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/pixels.jpg\" width=\"700\" height=\"394\" alt=\"Refactor Colours\"><\/figure>","protected":false},"excerpt":{"rendered":"<p>Editor\u2019s note: This article is part of our Code Optimization series, where we take a look at how to optimize coding for better efficiency in a bid to be better coders. CSS refactoring needs to be an essential part of the front-end development workflow, if we want to have a manageable and optimized code base.&hellip;<\/p>\n","protected":false},"author":146,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[507,4501,3761],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Refactor CSS - A Guide - Hongkiat<\/title>\n<meta name=\"description\" content=\"Learn how to optimize your CSS code with our code optimization series on refactoring CSS. Discover the best practices and techniques to improve your website&#039;s performance and user experience. Read now on Hongkiat.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Refactor CSS - A Guide\" \/>\n<meta property=\"og:description\" content=\"Editor&#039;s note: This article is part of our Code Optimization series, where we take a look at how to optimize coding for better efficiency in a bid to be\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-13T13:36:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-31T10:37:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg\" \/>\n<meta name=\"author\" content=\"Anna Monus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anna Monus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/\"},\"author\":{\"name\":\"Anna Monus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a601053a0ab457901e00cdc83bd5359e\"},\"headline\":\"How to Refactor CSS &#8211; A Guide\",\"datePublished\":\"2016-04-13T13:36:42+00:00\",\"dateModified\":\"2023-03-31T10:37:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/\"},\"wordCount\":1483,\"commentCount\":15,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/code-optimization-series-refactoring-css\\\/cssdig.jpg\",\"keywords\":[\"CSS\",\"CSS Tutorials\",\"Series: Code Optimization\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/\",\"name\":\"How to Refactor CSS - A Guide - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/code-optimization-series-refactoring-css\\\/cssdig.jpg\",\"datePublished\":\"2016-04-13T13:36:42+00:00\",\"dateModified\":\"2023-03-31T10:37:41+00:00\",\"description\":\"Learn how to optimize your CSS code with our code optimization series on refactoring CSS. Discover the best practices and techniques to improve your website's performance and user experience. Read now on Hongkiat.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/code-optimization-series-refactoring-css\\\/cssdig.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/code-optimization-series-refactoring-css\\\/cssdig.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/code-optimization-series-refactoring-css\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Refactor CSS &#8211; A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a601053a0ab457901e00cdc83bd5359e\",\"name\":\"Anna Monus\",\"description\":\"Anna is Technical Editor and Writer for Hongkiat.com. She mainly covers front-end frameworks, web standards, accessibility, WordPress development, and UX design.\",\"sameAs\":[\"https:\\\/\\\/www.annalytic.com\\\/\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/anna_monus\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Refactor CSS - A Guide - Hongkiat","description":"Learn how to optimize your CSS code with our code optimization series on refactoring CSS. Discover the best practices and techniques to improve your website's performance and user experience. Read now on Hongkiat.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/","og_locale":"en_US","og_type":"article","og_title":"How to Refactor CSS - A Guide","og_description":"Editor's note: This article is part of our Code Optimization series, where we take a look at how to optimize coding for better efficiency in a bid to be","og_url":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-04-13T13:36:42+00:00","article_modified_time":"2023-03-31T10:37:41+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg","type":"","width":"","height":""}],"author":"Anna Monus","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Anna Monus","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/"},"author":{"name":"Anna Monus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a601053a0ab457901e00cdc83bd5359e"},"headline":"How to Refactor CSS &#8211; A Guide","datePublished":"2016-04-13T13:36:42+00:00","dateModified":"2023-03-31T10:37:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/"},"wordCount":1483,"commentCount":15,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg","keywords":["CSS","CSS Tutorials","Series: Code Optimization"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/","url":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/","name":"How to Refactor CSS - A Guide - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg","datePublished":"2016-04-13T13:36:42+00:00","dateModified":"2023-03-31T10:37:41+00:00","description":"Learn how to optimize your CSS code with our code optimization series on refactoring CSS. Discover the best practices and techniques to improve your website's performance and user experience. Read now on Hongkiat.","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/code-optimization-series-refactoring-css\/cssdig.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/code-optimization-series-refactoring-css\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Refactor CSS &#8211; A Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a601053a0ab457901e00cdc83bd5359e","name":"Anna Monus","description":"Anna is Technical Editor and Writer for Hongkiat.com. She mainly covers front-end frameworks, web standards, accessibility, WordPress development, and UX design.","sameAs":["https:\/\/www.annalytic.com\/"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/anna_monus\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6QJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/146"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=26333"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26333\/revisions"}],"predecessor-version":[{"id":62879,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26333\/revisions\/62879"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=26333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=26333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=26333"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=26333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}