In this tutorial, we aim to explore the best practices for working with CSS Variables, also known as CSS Custom Properties. We will delve into strategies to organize your variables, effective naming conventions, and how to handle scope and inheritance efficiently.
By the end of the tutorial, you will have a firm grasp on:
- How to declare and use CSS variables
- The naming conventions for CSS variables
- How to handle scope and inheritance
Prerequisites: Basic knowledge of CSS
CSS Variables are declared in a property and are prefixed with --
. They are case sensitive and can be used by calling var()
function.
:root {
--main-color: #36a2eb;
}
body {
background-color: var(--main-color);
}
In this example, we defined a variable --main-color
in the :root
selector (which represents the root of the document) and used it as the background-color
of the body
.
It is a good practice to use meaningful names for your variables that reflect the variable's function. Also, using hyphen-separated names (kebab-case) is a common practice.
:root {
--main-background-color: #36a2eb;
--main-font-size: 16px;
}
CSS variables are subject to the same scope and inheritance rules like other CSS properties. A variable declared inside a selector will only be available within that selector and selectors inside it.
body {
--main-color: #36a2eb;
}
div {
background-color: var(--main-color); /* Unavailable here */
}
In this example, --main-color
is not available in div
because it is declared inside body
.
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
}
body {
color: var(--primary-color);
background-color: var(--secondary-color);
}
In this example, we define two variables --primary-color
and --secondary-color
in the root of the document. We then use these variables to style the body's text and background color.
:root {
--font-size: 16px;
}
p {
--font-size: 18px;
font-size: var(--font-size);
}
h1 {
font-size: var(--font-size);
}
In this example, we define a global --font-size
variable. In the p
selector, we override the global variable. The h1
selector, however, still uses the global variable.
In this tutorial, we learned the following key points:
- How to declare and use CSS variables
- The naming conventions for CSS variables
- How to handle scope and inheritance
Next steps for learning could include delving deeper into CSS variables in responsive design or exploring CSS variables in theming. Additional resources include the MDN Web Docs and CSS Tricks.
Create a stylesheet with at least three variables: --main-color
, --secondary-color
, and --tertiary-color
. Use these variables to style a simple HTML page.
Create a stylesheet where you define a global --font-size
variable. Then, in at least two different selectors, override this variable with different values.
Create a stylesheet where you define some variables inside a specific selector, and try to use them in a different selector. Can you explain the results?
Remember, practice is key when getting to grips with new concepts. Happy coding!