I recently came across the website StuffAndNonsense.co.uk, which is a personal site for the digital designer Andy Clarke. I was astonished by it, using webpage technologies I didn't even know existed.
I looked into the styles and found this bit:
:root {
--font-family: tondo_rg,sans-serif;
--font-family-light: tondo_lt,sans-serif;
--font-family-bold: tondo_bd,sans-serif;
--font-weight: 400;
--font-size-xxx-display: 8vmin;
--font-size-xx-display: 4.11rem;
--font-size-x-display: 3.653rem;
--font-size-display: 3.247rem;
--font-size-xxxx-large: 2.887rem;
--font-size-xxx-large: 2.027rem;
--font-size-xx-large: 1.802rem;
--font-size-x-large: 1.602rem;
--font-size-large: 1.424rem;
--font-size-medium: 1.266rem;
--font-size: 1.125rem;
--font-size-small: 1rem;
--font-size-x-small: .889rem;
--font-size-xx-small: .79rem;
--lineheight-text: 1.5;
--lineheight-heading: 1.3;
--color-background: #fff;
--color-background-selection: #f0f2f3;
--color-border: #cacfd5;
--color-text-default: #0b1016;
--color-text-alt: #95a0ac;
--color-base: #f4f5f6;
--color-accent: #ba0d37;
--color-logo-enamel: #ba0d37;
--color-logo-highlight: #fdfdfd;
--color-logo-metal: #cacfd5;
--color-logo-lettering: #fff;
--color-logo-type: #0b1016;
--color-text-link: #2c4159;
--color-text-link-active: var(--color-text-link);
--color-text-link-focus: var(--color-text-link);
--color-text-link-hover: var(--color-accent);
--color-text-link-visited: var(--color-text-link);
--color-button-default: #2c4159;
--color-button-alt: #243449;
--color-button-border: #8586a4;
--color-button-shadow: #ecedee;
--border-radius-x-small: .25rem;
--border-radius-small: .5rem;
--border-radius-medium: .75rem;
--border-radius-large: 1rem;
--border-radius-circle: 50%;
--border-width-hairline: .5px;
--border-width-thin: 1px;
--border-width-thick: 2px;
--grid-gap: 4vw;
--max-width: 92rem;
--spacing-xx-small: .125rem;
--spacing-x-small: .25rem;
--spacing-small: .5rem;
--spacing: .75rem;
--spacing-medium: 1rem;
--spacing-large: 1.5rem;
--spacing-x-large: 2rem;
--spacing-xx-large: 3rem;
--duration-instantly: 0;
--duration-immediately: .1s;
--duration-quickly: .2s;
--duration-promptly: .5s;
--duration-slowly: 1s;
}
I was... confused to say the least. Not only had I never seen CSS properties prefixed by --
, but I'd never seen stuff like font-size-xx-large: 1.802rem;
. What is this doing? I tried Googling (and even Binging) it to no avail.
These properties are CSS Variables. You can see more on CSS variables here: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables Here's an example of how to access CSS variables:
element {
background-color: var(--main-bg-color);
}
The reason they are in something called root
: The :root selector allows you to target the highest-level "parent" element in the DOM, or document tree.
Variables in CSS starts with "--" which are case sensitive :root is used to define global scope which is similar to defining for body selector
:root {
--font-size-xx-large: 16px;
--font-size-xx-small: 12px;
}
For referencing that variable, we need to use var() function
p {
font-size: var(--font-size-xx-large);
}
div{
font-size: var(--font-size-xx-small);
}
Note: Currently it is not supported by all browsers
Check compatibility details here - https://caniuse.com/#feat=css-variables
code sample for reference - https://codepen.io/nagasai/pen/aYmPYv
:root {
--font-size-xx-large: 16px;
--font-size-xx-small: 12px;
}
p {
font-size: var(--font-size-xx-large);
}
div{
font-size: var(--font-size-xx-small);
}
<p>Paragraph font size large 16px</p>
<div>Div font size small 12px</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With