{"id":18024,"date":"2013-09-02T18:01:01","date_gmt":"2013-09-02T10:01:01","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=18024"},"modified":"2025-04-24T17:08:04","modified_gmt":"2025-04-24T09:08:04","slug":"css-variables","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/css-variables\/","title":{"rendered":"A Look Into: Using CSS Variables"},"content":{"rendered":"<p>Variables, officially known as Custom Properties, have finally come to <a href=\"https:\/\/www.hongkiat.com\/blog\/tag\/css\/\">CSS<\/a> \u2013 the language we use daily for styling webpages. We have discussed using variables with <a href=\"https:\/\/www.hongkiat.com\/blog\/less-basic\/\">CSS Pre-processors<\/a> <a href=\"https:\/\/www.hongkiat.com\/blog\/getting-started-saas\/\">in our previous posts<\/a>. Today, we will see how to use them natively in CSS.<\/p>\n<h2>Declaring Variables<\/h2>\n<p>CSS custom properties are defined using a name that begins with two dashes (<code>--<\/code>), like so:<\/p>\n<pre>\n--primary-color: #333;\n<\/pre>\n<p>In the example above, we created a custom property named <code>--primary-color<\/code>. Unlike some CSS Pre-processor variables, custom properties must be declared within a CSS ruleset, typically on the <code>:root<\/code> pseudo-class to make them globally available.<\/p>\n<p>For example:<\/p>\n<pre>\n:root {\n  --primary-color: #333;\n}\n<\/pre>\n<p>When naming custom properties, it\u2019s advisable to follow standard CSS naming conventions.<\/p>\n<p>CSS typically uses dashes to separate words (kebab-case), like <code>--primary-color<\/code>. While JavaScript often uses camelCase (e.g., <code>variableNamesLikeThis<\/code>), sticking to kebab-case for CSS variables ensures consistency within your stylesheets.<\/p>\n<h2>Using Variables<\/h2>\n<p>How do we apply these variables in CSS rules? We use the <code>var()<\/code> function to retrieve the value of a custom property. For instance, to apply the primary color to the body\u2019s background, we would write:<\/p>\n<pre>\n:root {\n  --primary-color: #333;\n}\n\nbody {\n  background-color: var(--primary-color);\n}\n<\/pre>\n<p>CSS variables (Custom Properties) are now widely supported across all modern browsers without vendor prefixes.<\/p>\n<h2>Scope<\/h2>\n<p>If you are familiar with JavaScript, you should already understand the concept of <strong>Scope<\/strong>. Similarly, scope applies to CSS custom properties. The value is inherited and will be applied to the element where it is declared and its descendants.<\/p>\n<p>In our previous example, declaring the variable on the <code>:root<\/code> pseudo-class makes it available globally to all elements on the page.<\/p>\n<p>If we declare it within a specific selector, like a <code>div<\/code>:<\/p>\n<pre>\ndiv {\n  --primary-color: #333; \/* Or a different value *\/\n}\n<\/pre>\n<p>Then the variable (or its specific value within this scope) will only be available to that <code>&lt;div&gt;<\/code> and its descendants.<\/p>\n<h2>Further Resource<\/h2>\n<ul>\n<li> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.w3.org\/TR\/css-variables\/\">CSS Custom Properties for Cascading Variables Module Level 1<\/a> \u2013 W3C <\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Without a doubt, <strong>CSS Custom Properties (Variables)<\/strong> are a powerful addition to CSS. They are now a W3C Recommendation and widely supported, making them a standard feature for managing styles directly in CSS. This often reduces the need for pre-processors for tasks like theme management or maintaining consistent values.<\/p>\n<p>Visit the demo page to see CSS variables in action.<\/p>\n<div class=\"button\">\n  <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/hongkiat.github.io\/css-variables\/\">View Demo<\/a>\n  <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/hongkiat\/css-variables\/\">Download Source<\/a>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using variables with CSS Pre-processors in our previous posts. Today, we will see how to use them natively in CSS. Declaring Variables CSS custom properties are defined using a name that begins&hellip;<\/p>\n","protected":false},"author":113,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3392,352],"tags":[507],"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.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Using CSS Variables<\/title>\n<meta name=\"description\" content=\"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using\" \/>\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\/css-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Look Into: Using CSS Variables\" \/>\n<meta property=\"og:description\" content=\"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/css-variables\/\" \/>\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=\"2013-09-02T10:01:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T09:08:04+00:00\" \/>\n<meta name=\"author\" content=\"Thoriq Firdaus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@tfirdaus\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thoriq Firdaus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/\"},\"author\":{\"name\":\"Thoriq Firdaus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e7948c7a175d211496331e4b6ce55807\"},\"headline\":\"A Look Into: Using CSS Variables\",\"datePublished\":\"2013-09-02T10:01:01+00:00\",\"dateModified\":\"2025-04-24T09:08:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/\"},\"wordCount\":360,\"commentCount\":25,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"CSS\"],\"articleSection\":[\"Coding\",\"Web Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/\",\"name\":\"Using CSS Variables\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-09-02T10:01:01+00:00\",\"dateModified\":\"2025-04-24T09:08:04+00:00\",\"description\":\"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Look Into: Using CSS Variables\"}]},{\"@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\\\/e7948c7a175d211496331e4b6ce55807\",\"name\":\"Thoriq Firdaus\",\"description\":\"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.\",\"sameAs\":[\"https:\\\/\\\/thoriq.com\",\"https:\\\/\\\/x.com\\\/tfirdaus\"],\"jobTitle\":\"Web Developer\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/thoriq\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Using CSS Variables","description":"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using","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\/css-variables\/","og_locale":"en_US","og_type":"article","og_title":"A Look Into: Using CSS Variables","og_description":"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using","og_url":"https:\/\/www.hongkiat.com\/blog\/css-variables\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2013-09-02T10:01:01+00:00","article_modified_time":"2025-04-24T09:08:04+00:00","author":"Thoriq Firdaus","twitter_card":"summary_large_image","twitter_creator":"@tfirdaus","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Thoriq Firdaus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/"},"author":{"name":"Thoriq Firdaus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e7948c7a175d211496331e4b6ce55807"},"headline":"A Look Into: Using CSS Variables","datePublished":"2013-09-02T10:01:01+00:00","dateModified":"2025-04-24T09:08:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/"},"wordCount":360,"commentCount":25,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["CSS"],"articleSection":["Coding","Web Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/css-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/","url":"https:\/\/www.hongkiat.com\/blog\/css-variables\/","name":"Using CSS Variables","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2013-09-02T10:01:01+00:00","dateModified":"2025-04-24T09:08:04+00:00","description":"Variables, officially known as Custom Properties, have finally come to CSS \u2013 the language we use daily for styling webpages. We have discussed using","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/css-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/css-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"A Look Into: Using CSS Variables"}]},{"@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\/e7948c7a175d211496331e4b6ce55807","name":"Thoriq Firdaus","description":"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.","sameAs":["https:\/\/thoriq.com","https:\/\/x.com\/tfirdaus"],"jobTitle":"Web Developer","url":"https:\/\/www.hongkiat.com\/blog\/author\/thoriq\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-4GI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18024","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\/113"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=18024"}],"version-history":[{"count":4,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18024\/revisions"}],"predecessor-version":[{"id":74098,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18024\/revisions\/74098"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=18024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=18024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=18024"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=18024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}