๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css3_variables.asp
CSS Variables - The var() function
The var() function is used to insert the value of a CSS variable.
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css3_variables_javascript.asp
CSS Change Variables With JavaScript
Static/Relative Position Fixed/Absolute Position Sticky Position Code Challenge CSS Position Offsets ... The var() Function Overriding Variables Variables and JavaScript Variables in MQ Code Challenge CSS @property
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css3_variables_overriding.asp
CSS Overriding Variables
Static/Relative Position Fixed/Absolute Position Sticky Position Code Challenge CSS Position Offsets ... The var() Function Overriding Variables Variables and JavaScript Variables in MQ Code Challenge CSS @property
๐ŸŒ
W3Schools
w3schools.com โ€บ htmlcss โ€บ htmlcss_variables.asp
HTML & CSS: Custom Properties (CSS Variables)
CSS custom properties, commonly called variables, let you centralize design tokens like colors, spacing, and typography.
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css3_variables_mediaqueries.asp
CSS Variables in Media Queries
CSS Reference CSS Selectors CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS At-rules CSS Functions CSS Reference Aural CSS Web Safe Fonts CSS Animatable CSS Units CSS PX-EM Converter CSS Colors CSS Color Values CSS Default Values CSS Browser Support ... Media Queries are about defining different style rules for different devices (screens, tablets, mobile phones, etc.). You can learn more Media Queries in our Media Queries Chapter. Now we want to set one variable value for screens below 450px wide, and another variable value for screens above 450px wide.
๐ŸŒ
W3Schools
w3schools.in โ€บ css3 โ€บ variables
How to Use CSS Variables for Reusable and Dynamic Styles
Learn how to use CSS variables (custom properties) to simplify your styles, reduce repetitive code, and create dynamic designs with JavaScript features such as theme switching.
๐ŸŒ
W3Schools
w3schools.com โ€บ cssref โ€บ func_var.php
CSS var() function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.
Find elsewhere
๐ŸŒ
W3C
w3.org โ€บ TR โ€บ 2012 โ€บ WD-css-variables-20120410
CSS Variables Module Level 1
This module defines a new type of primitive value, the variable, which is accepted by all properties. This specification follows the CSS property definition conventions from [CSS21]. Value types not defined in this specification are defined in CSS Level 2 Revision 1 [CSS21]. Other CSS modules ...
๐ŸŒ
W3schoolsapp
w3schools.w3schoolsapp.com โ€บ css โ€บ css3_variables.html
CSS Variables - var() Function
CSS Reference CSS Selectors CSS ... specify the first browser version that fully supports the property. Variables in CSS should be declared within a CSS selector that defines its scope....
๐ŸŒ
W3C
w3.org โ€บ TR โ€บ css-variables-1
CSS Custom Properties for Cascading Variables Module Level 1
June 16, 2022 - The <custom-property-name> production ... never give them a meaning beyond what is presented here. ... Custom properties define variables, referenced with the var() notation, which can be used for many purposes....
๐ŸŒ
Cach3
w3schools.com.cach3.com โ€บ css โ€บ css3_variables.asp.html
CSS Variables - var() Function - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.
๐ŸŒ
W3Schools
w3schools.com โ€บ htmlcss โ€บ htmlcss_colors_backgrounds.asp
HTML & CSS Colors and Backgrounds
Note: CSS variables (also called custom properties) let you store colors once and reuse them across styles.
๐ŸŒ
W3Schools
w3schools.com โ€บ sass โ€บ sass_variables.asp
Sass Variables
It could be wise to define all global variables in its own file, named "_globals.scss", and include the file with the @include keyword. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css_counters.asp
CSS Counters
CSS counters are "variables" maintained by CSS, and their values can be incremented (or decremented) by CSS rules.
Top answer
1 of 13
90

There's no requirement that all styles for a selector reside in a single rule, and a single rule can apply to multiple selectors... so flip it around:

/* Theme color: text */
H1, P, TABLE, UL
{ color: blue; }

/* Theme color: emphasis */
B, I, STRONG, EM
{ color: #00006F; }

/* ... */

/* Theme font: header */
H1, H2, H3, H4, H5, H6
{ font-family: Comic Sans MS; }

/* ... */

/* H1-specific styles */
H1
{ 
   font-size: 2em; 
   margin-bottom: 1em;
}

This way, you avoid repeating styles that are conceptually the same, while also making it clear which parts of the document they affect.

Note the emphasis on "conceptually" in that last sentence... This just came up in the comments, so I'm gonna expand on it a bit, since I've seen people making this same mistake over and over again for years - predating even the existence of CSS: two attributes sharing the same value does not necessarily mean they represent the same concept. The sky may appear red in the evening, and so do tomatoes - but the sky and the tomato are not red for the same reason, and their colors will vary over time independently. By the same token, just because you happen to have two elements in your stylesheet that are given the same color, or size or positioning does not mean they will always share these values. A naive designer who uses grouping (as described here) or a variable processor such as SASS or LESS to avoid value repetition risks making future changes to styling incredibly error-prone; always focus on the contextual meaning of styles when looking to reduce repetition, ignoring their current values.

2 of 13
16

You can achieve it and much more by using Less CSS.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ everything-you-need-to-know-about-css-variables-c74d922ea855
Everything You Need to Know About CSS Variables [Full Handbook]
February 12, 2018 - By Emmanuel Ohans Most programming languages have support for variables. But sadly, CSS has lacked support for native variables from the very beginning. You write CSS? Then no variables for you. Well, except if you were using a preprocessor like Sass...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ CSS โ€บ Using_CSS_custom_properties
Using CSS custom properties (variables) - MDN Web Docs
December 20, 2024 - Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that represent specific values to be reused throughout a document. They are set using the @property at-rule or by custom property syntax (e.g., --primary-color: blue;). Custom ...