26
loading...
This website collects cookies to deliver better user experience
let a = 10;
const b = 20;
var c = 30;
let
, const
, var
; instead we just use 2 hyphens (dashes --) before the user-defined variable name and put them in a scope.scope
, just like in Javascript we've scopes like function scope, block scope, global scope we also have scopes in CSS.:root {
--main-color: red;
}
:root
selector. It targets the top element of the DOM and hence has a global scope. Similarly we can have other local scopes based on different CSS Selectors.div
.var()
function.var()
function takes the property name as argument, retrieves it's value from the stylesheet if it's present and replaces it with the actual CSS-Property.var()
can also be used to provide a fallback value in case the property is missing.
div {
font-size: var(--main-font-size, 16px);
}
getPropertyValue()
method of the computed CSSStyleDeclaration object.setProperty()
method of the CSSStyleDeclaration object.
:root {
--main-font-color: #E9E9E9;
--main-bg-color: rgba(128,128,0,1);
}
p {
color: var(--main-font-color);
background-color: var(--main-bg-color);
}
span {
color: #E9E9E9;
background-color: rgba(128,128,0,1);
}
media-queries
.
For other differences please look at the following section for an amazing article on CSS-Tricks