{"id":29116,"date":"2018-09-05T10:55:44","date_gmt":"2018-09-05T15:55:44","guid":{"rendered":"https:\/\/getflywheel.com\/?p=29116"},"modified":"2024-01-29T12:55:49","modified_gmt":"2024-01-29T18:55:49","slug":"add-css-variables-customize-wordpress-child-themes-how-to","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/","title":{"rendered":"How To Add CSS Variables to Customize WordPress Child Themes"},"content":{"rendered":"\n<p>CSS Variables, also referred to as CSS custom properties, allow for easy reuse in your CSS stylesheets. <\/p>\n\n\n\n<p>If you\u2019ve done work with <a rel=\"noreferrer noopener\" href=\"https:\/\/wpengine.com\/builders\/child-themes-style-variations\/\" data-type=\"URL\" data-id=\"https:\/\/wpengine.com\/builders\/child-themes-style-variations\/\" target=\"_blank\">WordPress child themes<\/a>, you probably find yourself doing a lot of styling and overriding on things like color, padding, etc. If you\u2019re new to web design, it\u2019s worth mentioning that there\u2019s nothing wrong with using plain CSS. CSS Variables are optional and as you get more advanced, you\u2019ll begin to see how variables will benefit you.<\/p>\n\n\n\n<p>These are not intended to replace CSS preprocessors, the most popular being <a data-type=\"URL\" data-id=\"https:\/\/sass-lang.com\/\" href=\"https:\/\/sass-lang.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">SASS<\/a>. Many builders specifically use SASS for larger projects, and along with CSS Variables as a core piece, it offers many advanced options. <\/p>\n\n\n\n<p>If you\u2019ve worked with preprocessors before, however, you know that they need to be compiled to output the CSS. For that reason, this tutorial will focus on CSS Variables, and won\u2019t dive into details about preprocessors.<\/p>\n\n\n\n<p>CSS Variables are a happy medium, and fortunately, there\u2019s&nbsp;strong browser support. Before committing to using CSS Variables in production, be sure to check <a href=\"https:\/\/caniuse.com\" data-type=\"URL\" data-id=\"https:\/\/caniuse.com\" target=\"_blank\" rel=\"noreferrer noopener\">Can I Use<\/a> to make sure they have the support you need. <\/p>\n\n\n\n<p>CSS Variables are more efficient than working with plain CSS but don\u2019t&nbsp;require an advanced setup like SASS. The browser does the \u201ccompiling,\u201d and you\u2019re not dependent on setting up and environment that outputs the compiled CSS.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2018\/09\/desk-scene-662.png\" alt=\"css variables and theme customizations feature woman typing on laptop with various scrum and design tools on white desk\" class=\"wp-image-29130\" \/><figcaption class=\"wp-element-caption\">CSS Variables are more efficient than plain CSS and don\u2019t&nbsp;require an advanced setup like SASS<\/figcaption><\/figure>\n\n\n\n<p>This approach is a good project fit for things like styling simple WordPress child themes. For projects like this, there tends to be a need to make styling updates to create a branded theme. CSS Variables can help, as they provide a manageable list of styling overrides and don&#8217;t require a special environment  for compiling SASS to CSS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Get Genesis Framework &amp; StudioPress Themes for&nbsp;Free!<\/strong><\/h2>\n\n\n\n<p>WP Engine customers gain access to a <a rel=\"noreferrer noopener\" href=\"https:\/\/wpengine.com\/studiopress-themes\/\" data-type=\"URL\" data-id=\"https:\/\/wpengine.com\/studiopress-themes\/\" target=\"_blank\">suite of premium WordPress themes<\/a> that comes standard with every plan! You can get started building your next site for just&nbsp;$20\/month! Learn more <a href=\"https:\/\/wpengine.com\/plans-2022\/\" data-type=\"URL\" data-id=\"https:\/\/wpengine.com\/plans-2022\/\" target=\"_blank\" rel=\"noreferrer noopener\">here.<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/getflywheel.com\/layout\/wp-content\/uploads\/2020\/02\/LinkedIn_1200x630@2x-3-784x412.jpg\" alt=\"promotional image for the Genesis Framework and StudioPress themes powered by WP Engine\" class=\"wp-image-39543\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use&nbsp;CSS Variables<\/h2>\n\n\n\n<p>When using specific colors to stay on brand, one common requirement is having a brand color palette that can be used over and over again. It gets redundant to type the same color value each time. Plus, what&nbsp;if you want to make a change to one of the color values in the set? <\/p>\n\n\n\n<p>For example, maybe a blue needs to be slightly darker. This happens all the time. Yes, of course, there\u2019s the trusty find and replace. Adjusting&nbsp;the value in one place, however, is more efficient when making this global change and as variables are reused.<\/p>\n\n\n\n<p>Here\u2019s what a <code>CSS<\/code> Variable looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;css]\n:root {\n--text-black: #232525;\n}\n\nheader {\ncolor: var(--text-black);\n}\n&#091;\/css]<\/code><\/pre>\n\n\n\n<p><br>As I\u2019m streamlining the workflow for styling a WordPress child theme, I\u2019ve added the variables to my <code>styles.css<\/code> file. This is just the \u201cshort list\u201d and as more are added, efficiently naming each variable becomes important.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;css]\n:root {\n--white: #ffffff;\n--black: #232525;\n--mid-gray: #eeeeee;\n--brand-red: #e50000;\n}\n&#091;\/css]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2018\/09\/side-desk-scene-662-1.png\" alt=\"css variables and theme customizations feature woman typing on laptop with various scrum and design tools on white desk\" class=\"wp-image-29132\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The var() Function<\/h3>\n\n\n\n<p>How are variables actually used? It\u2019s pretty simple. First, the variable is declared and then it\u2019s used in the needed CSS rule-set. <\/p>\n\n\n\n<p>Scope is an important thing to be aware of. Variables need to be declared within a CSS selector that\u2019s&nbsp;within its intended scope. In many cases, you\u2019ll need variables to be available in the global scope, that way they\u2019re&nbsp;accessible to everything. You can use either the&nbsp;<code>:root<\/code> or the <code>body<\/code> selector for the global scope. There may be instances where you need to limit the scope, however, and want to work with a locally-scoped variable.<\/p>\n\n\n\n<p>It\u2019s easy to spot variables;&nbsp;they have two dashes before them. The two dashes (&#8211;) need to be included.<\/p>\n\n\n\n<p>The syntax of the <code>var()<\/code> is pretty straightforward:<\/p>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code><code>var(variable-name, value)<\/code>\n&#091;css]\n:root {\n--light-gray: #eeeeee\n--text-black: #434344\n}\n\n.sidebar {\n--background-color: var(--light-gray);\n--color: var(--text-black);\n}\n&#091;\/css]<\/code><\/pre>\n<\/div>\n\n\n\n<p>Instead of adjusting the color in multiple places, the variable value is adjusted in once place.<\/p>\n\n\n\n<p>The web inspector (Chrome in this case) allows you to inspect and see which variables are being used.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2018\/09\/image3-copy.png\" alt=\"css variables theme customization chrome inspector css\" \/><\/figure>\n\n\n\n<p>The following example first defines global custom properties named <code>--light-gray<\/code> and <code>--text-black<\/code>. &nbsp;The <code>var()<\/code> function is called, which inserts the value of the custom properties later in the stylesheet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;css]\n:root {\n--light-gray: #eeeeee;\n--text-black: #434344;\n}\n\n.sidebar {\nbackground-color: var(--light-gray);\ncolor: var(--text-black);\n}\n&#091;\/css]<\/code><\/pre>\n\n\n\n<p><br>It\u2019s worth mentioning that variables can be very useful when working with CSS breakpoints. By using globally scoped variables in different breakpoints, things like padding and other useful styling can be customized for various sizes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;css]\n:root {\n--gutter: 5px;\n}\n\nsection {\npadding: var(--gutter);\n}\n\n@media (min-width: 600px) {\n:root {\n--gutter: 15px;\n}\n}\n&#091;\/css]<\/code><\/pre>\n\n\n\n<p><br>Here\u2019s an example of variables that can be found in the local scope. We\u2019ve only touched on the global scope so far, so you\u2019ll notice that things are not in the root. These are styles for a warning message. There\u2019s&nbsp;a warning text color declared here that is only useful for this class. Also, it\u2019s worth noting that <code>calc<\/code> can be utilized also.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;css]\n.warning {\n--warning-text: #ff0000;\n--gap: 20;\ncolor: var(--warning-text);\nmargin-top: calc(var(--gap) * 1px);\n}\n&#091;\/css]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2018\/09\/image2-copy.png\" alt=\"css variables theme customizations warning text\" \/><\/figure>\n\n\n\n<p>The examples above cover the basics of CSS Variables. These concepts can be applied to custom WordPress themes&nbsp;or any other custom CSS that you write. Variables have advantages over just plain CSS and will help you work more efficiently when making site-wide changes. They allow for better-organized stylesheets without the need for a preprocessor.<\/p>\n\n\n\n<hr class=\"wp-block-separator aligncenter has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Fuel the Freedom to Create With WP Engine<\/strong><\/h2>\n\n\n\n<p>WP Engine\u00a0powers the freedom to create on WordPress. The company\u2019s products, the fastest among all WordPress providers, power 1.5 million digital experiences. More of the top 200,000 sites in the world use WP Engine to power their digital experiences than anyone else in WordPress. Find our more about our world-class <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\">WordPress hosting<\/a> or\u00a0<a data-type=\"internal\" data-id=\"#sales\" href=\"#sales\">speak to a representative today<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS Variables, also referred to as CSS custom properties, allow for easy reuse in your CSS stylesheets. If you\u2019ve done work with WordPress child themes, you probably find yourself doing a lot of styling and overriding on things like color, padding, etc. If you\u2019re new to web design, it\u2019s worth mentioning that there\u2019s nothing wrong<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":140145,"template":"","resource-topic":[1396],"resource-role":[1397,896],"resource-type":[916],"class_list":["post-29116","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Add CSS Variables to Customize WordPress Child Themes<\/title>\n<meta name=\"description\" content=\"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Add CSS Variables to Customize WordPress Child Themes\" \/>\n<meta property=\"og:description\" content=\"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-29T18:55:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2018\/09\/css-variables-header.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/\",\"name\":\"How To Add CSS Variables to Customize WordPress Child Themes\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2018-09-05T15:55:44+00:00\",\"dateModified\":\"2024-01-29T18:55:49+00:00\",\"description\":\"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How To Add CSS Variables to Customize WordPress Child Themes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Add CSS Variables to Customize WordPress Child Themes","description":"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How To Add CSS Variables to Customize WordPress Child Themes","og_description":"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-01-29T18:55:49+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2018\/09\/css-variables-header.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/","name":"How To Add CSS Variables to Customize WordPress Child Themes","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2018-09-05T15:55:44+00:00","dateModified":"2024-01-29T18:55:49+00:00","description":"CSS Variables allow for easy reuse in your CSS stylesheets. Use this guide to add CSS variables to customize child themes in WordPress!","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/add-css-variables-customize-wordpress-child-themes-how-to\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How To Add CSS Variables to Customize WordPress Child Themes"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2018\/09\/css-variables-grid.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Designer, Developer","topic":"<strong>Topics:<\/strong> Design","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/29116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/140145"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=29116"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=29116"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=29116"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=29116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}