This question already has answers here:
Do custom CSS properties use one leading dash or two?
(2 answers)
Is there a difference between CSS custom properties and CSS variables?
(4 answers)
How do CSS custom properties cascade?
(1 answer)
Closed 4 years ago.
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>
Related
I got some example CSS code (well written and working) with many span statements inside, that I modified for my use. What exactly they do? VS Code shows me as an error, but browsers don't complain, and I couldn't find any references in the CSS documentation, as if this syntax does not exist.
Example:
h2 {
letter-spacing: 2vw;
font-size: 2vw;
font-weight: bold;
text-align: center;
span {
display: block;
font-size: 8vw;
letter-spacing: -1vw;
}
}
VS code complains:
"code": "css-colonexpected",
"severity": 8,
"message": "colon expected",
"source": "css",
If I add colon it would be suggesting keys right away, and would not accept anything in curly brackets{}
Thanks
the brackets { and } define scope so that
body {
color: #000;
}
Would define that the color (text color) of the body element type (css query selector) would be #000 (which is hex for black)
however, if you have an element in an element like this using a precompiler such as less for css using the less syntax.
body {
color: #000;
span {
color: #FF0000;
}
}
this would do as the previous css did, but in less you can create a hierarchy
the body's color will be set to black as before.
and then any span child of the body element will have its color set to red (#FF0000)
CSS/LESS are used in conjunction with the HTML DOM object model.
You're correct that this syntax doesn't exist for CSS, as it doesn't support nested selectors like this.
The correct syntax would be:
h2 {
letter-spacing: 2vw;
font-size: 2vw;
font-weight: bold;
text-align: center;
}
h2 span {
display: block;
font-size: 8vw;
letter-spacing: -1vw;
}
This syntax is of course perfectly acceptable if you use a CSS preprocessor, like SASS or LESS for example. CSS preprocessors compile CSS written like you've done into standard CSS syntax, and add extra functionality, like using variables and conditional statements.
I think that modern browsers are probably capable of understanding syntax like this in certain situations, but if you want to use to this sort of syntax then using a preprocessor is a safer option to avoid errors.
This question already has answers here:
How can I define colors as variables in CSS?
(19 answers)
Is there a difference between CSS custom properties and CSS variables?
(4 answers)
Closed 1 year ago.
//style.sass
$common-color: #333;
body {
font: 100% $font-stack;
color: $common-color;
}
can I do the similar in css? because I am not using .sass for my project.
:root {
--maincolor: red;
--mainfont: Arial;
}
body {
color: var(--maincolor);
font: var(--mainfont);
{
You can write it as below
:root {
--common-color: #333;
}
body {
font: 100% $font-stack;
color: var(--common-color);
}
Here :root is the scope of the variable
More information can be found in the link -
https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
In CSS3 you're able to use variables by declaring them with --name-of-variable and access them with var(--name-of-variable). Your example would look like:
:root {
--common-color: #333;
--font-stack: 'Roboto', sans-serif;
}
body {
font: 100% var(--font-stack);
color: var(--common-color);
}
See https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties for more information about scoping and fallbacks.
I am currently working on a react project which uses DSM invision for the UI design, basicly, DSM provides a css file '_style-params' which contains style variables.
--color-primary: #00a2ff;
--color-secondary: #6c757d;
--color-success: #28a745;
--color-danger: #dc3545;
for this kind is easy to understand and use, i can just import the css file into my main css file and write:
background-color:var(--color-primary);
However, when it comes to font, I have some question:
below is from _style-params.css:
/* button fonts/default/5-warning font style */
--font-button-fonts-default-5-warning-font-size: 16px;
--font-button-fonts-default-5-warning-line-height: 14px;
--font-button-fonts-default-5-warning-text-align: center;
--font-button-fonts-default-5-warning-color: #f0ad4e;
--font-button-fonts-default-5-warning-letter-spacing: 0.2px;
--font-button-fonts-default-5-warning-font-style: normal;
--font-button-fonts-default-5-warning-font-weight: 400;
--font-button-fonts-default-5-warning-font-family: Roboto;
--font-button-fonts-default-5-warning: {
font-size: var(--font-button-fonts-default-5-warning-font-size);
line-height: var(--font-button-fonts-default-5-warning-line-height);
text-align: var(--font-button-fonts-default-5-warning-text-align);
color: var(--font-button-fonts-default-5-warning-color);
letter-spacing: var(--font-button-fonts-default-5-warning-letter-spacing);
font-style: var(--font-button-fonts-default-5-warning-font-style);
font-weight: var(--font-button-fonts-default-5-warning-font-weight);
font-family: var(--font-button-fonts-default-5-warning-font-family);
};
Am I able to just use following varible (which is a object)
var(--font-button-fonts-default-5-warning)
I dunno which property to use this variable, i tried following:
font:var(--font-button-fonts-default-5-warning)
which is not working (obviously).
So, am I able to use this object CSS varible?
or I have to use the individual variables?
There is no object concept in CSS variables. You need to use them individually but you can also combine them in the same variable that you can use later relying on the shothand notation of properties.
Example:
:root {
/* button fonts/default/5-warning font style */
--font-button-fonts-default-5-warning-font-size: 30px;
--font-button-fonts-default-5-warning-line-height: 14px;
--font-button-fonts-default-5-warning-text-align: center;
--font-button-fonts-default-5-warning-color: #f0ad4e;
--font-button-fonts-default-5-warning-letter-spacing: 0.2px;
--font-button-fonts-default-5-warning-font-style: normal;
--font-button-fonts-default-5-warning-font-weight: 400;
--font-button-fonts-default-5-warning-font-family: "Roboto";
--font-button-fonts-default-5-warning:
var(--font-button-fonts-default-5-warning-font-style)
var(--font-button-fonts-default-5-warning-font-weight)
var(--font-button-fonts-default-5-warning-font-size)/
var(--font-button-fonts-default-5-warning-line-height)
var(--font-button-fonts-default-5-warning-font-family);
}
.warning {
font:var(--font-button-fonts-default-5-warning);
color: var(--font-button-fonts-default-5-warning-color);
letter-spacing: var(--font-button-fonts-default-5-warning-letter-spacing);
text-align:var(--font-button-fonts-default-5-warning-text-align);
}
<p>text here</p>
<p class="warning">text here</p>
We can use almost all the values inside font but not color, letter-spacing and text-align that you need use them individually.
Precursor:
Under normal circumstances, I would never do this.
I have a CSS file that I am currently collaborating on with another person. I built the file initially, then they have added rules to it after the fact. But, instead of adding rules to selectors that already exist, they have duplicated selectors everywhere. I don't even want to get into how disorganized the file has become. The problem is that the duplicated selectors are spread out all over the file now and it could take some time to sort it out.
Anyway, I am currently in the process of trying to clean up the file. I have tried beautify, css format, etc in my editor (ST3), which cleans up fine but still leaves the duplicate selectors. I have tried various online tools like CSS Lint, ProCSSor, Dirty Markup, CleanCSS and so far none of these tools give me the desired result.
Is there any way that these selectors can be merged by some other means instead of manually?
Here's an example of my situation, just for reference:
I'd like to turn this...
.sameClass {
float: left;
width: 100%;
margin: 0;
padding: 0;
}
.differentClass {
border: none;
background: black;
padding: 0;
}
.sameClass {
font-weight: bold;
font-size: 24px;
display: inline-block;
}
into this...
.sameClass {
float: left;
width: 100%;
margin: 0;
padding: 0;
font-weight: bold;
font-size: 24px;
display: inline-block;
}
.differentClass {
border: none;
background: black;
padding: 0;
}
CSSO (Github project) is the tool will help you merge identical CSS classes.
It can be configured to execute some cleaning, compaction and restructuring.
Test in sandbox here : https://css.github.io/csso/csso.html
// Input
.card {box-shadow: none;}
.foo { color: #ff0000; }
.bar { color: rgba(255, 0, 0, 1); }
.card {border: 1px solid grey;}
// Output compacted + merged
.bar,.foo{color:red}
.card {box-shadow: none;border: 1px solid grey;}
A simplistic approach would be to sort your CSS file(s) by selector. This can be done by considering each rule as a "paragraph" (meaning you will have to ensure there are empty lines between rules, and nowhere else), and then using your editor's "sort paragraph" feature, if it has one. For instance, emacs has the M-x sort-paragraphs command.
Once multiple rules for the same selector are grouped together, you can manually go in and combine them.
This question already has answers here:
Lighten parent's (unknown) background-color in child
(3 answers)
How to override a LESS mixin variable based on a parent's variable
(1 answer)
Closed 7 years ago.
Hi I'm new to using Less and trying to make the best of the features it offers. What I would like to do is the following:
say I have a few anchor elements in html
Blue link
Red link
Green link
and I have the following css
.gen-link-prop {
text-decoration: none;
padding: 10px 40px;
color: #fff;
display: inline-block;
margin: 0 5px;
}
#blue-link-color: #9999ff;
#red-link-color: #ff9999;
#green-link-color: #99ff99;
.blue-link {
.gen-link-prop;
background-color: #blue-link-color;
}
.red-link {
.gen-link-prop;
background-color: #red-link-color;
}
.green-link {
.gen-link-prop;
background-color: #green-link-color;
}
.blue-link:hover, .red-link:hover, .green-link:hover {
background-color: darken(#<-- reference to base color here--<, 20%);
}
I want to be able to apply an operation to a property value that is already applied to the element. Is this even possible? Or is it something simple that I missed somewhere. Help is greatly appreciated