What no one told you about CSS Variables – DEV Community

Применяется к полям формы, содержимое которых не соответствует указанному типу.

Описание

CSS псевдокласс :invalid находит любые <input> или <form> элементы, контент которых не проходит валидацию (en-US), в соответствии с типом поля. Он позволяет вам легко менять внешний вид полей, что позволяет пользователю видеть и исправлять ошибки.

По умолчанию, Gecko не применяет стили к псевдоклассу :invalid. Однако, применяет стили (красное “свечение”, используя свойство box-shadow) к псевдоклассу :-moz-ui-invalid (en-US), который применяется в подгруппе случаев для :invalid.

Вы можете отключить свечение, используя следующий CSS или полностью изменить внешний вид некорректных полей.

:invalid { box-shadow: none;}:-moz-submit-invalid { box-shadow: none;}:-moz-ui-invalid { box-shadow:none;}

Обозначения

Описание Пример
<тип> Указывает тип значения. <размер>
A && B Значения должны выводиться в указанном порядке. <размер> && <цвет>
A | B Указывает, что надо выбрать только одно значение из предложенных (A или B). normal | small-caps
A || B Каждое значение может использоваться самостоятельно или совместно с другими в произвольном порядке. width || count
[ ] Группирует значения. [ crop || cross ]
* Повторять ноль или больше раз. [,<время>]*
+ Повторять один или больше раз. <число>+
? Указанный тип, слово или группа не является обязательным. inset?
{A, B} Повторять не менее A, но не более B раз. <радиус>{1,4}
# Повторять один или больше раз через запятую. <время>#

Table of content

  1. Be careful with !important
  2. They cannot store urls
  3. They can make an invalid value valid
  4. They can be used unitless
  5. They can be animated
  6. They cannot store the inherit value
  7. They can be empty
  8. CSS variables are not C++ variables
  9. They only work from parent to child
  10. They can have strange syntaxes

1) Be careful with !important

Using !important with CSS variables is a bit tricky so let’s start with a basic example:

p { –color:red!important; color:var(–color); color:blue;}

Enter fullscreen mode Exit fullscreen mode

What will be the color of p? you think it’s red because we will have the following:

p { color:red!important; color:blue;}

Enter fullscreen mode Exit fullscreen mode

But it’s not! the color of p will be blue because we will have the following:

p { color:red; color:blue;}

Enter fullscreen mode Exit fullscreen mode

!important in this case isn’t part of the value of color but is used to increase the specificity of –color. From the specification:

Note: Custom properties can contain a trailing !important, but this is automatically removed from the property’s value by the CSS parser, and makes the custom property “important” in the CSS cascade. In other words, the prohibition on top-level “!” characters does not prevent !important from being used, as the !important is removed before syntax checking happens.

Here is another example to better understand:

p{ –color:red!important; –color:blue; color:var(–color);}

Enter fullscreen mode Exit fullscreen mode

The above will give us a red color:

  1. We have two declarations of the same property called –color so we need to resolve the cascade. The first one is having !important so it wins
  2. We have our winner (–color:red!important) so !important is removed then the value is applied to color
  3. We have color:red.

Let’s make our code:

p{ –color:red!important; –color:blue; color:var(–color); color:blue;}

Enter fullscreen mode Exit fullscreen mode

Following the same logic, we resolve the cascade for –color and for color. –color:red!important is the winner and the same for color:blue so at the end we have blue because we no more care about color:var(–color).

An important rule is to always consider CSS variables (custom properties) as ordinary properties and not only variables that store values.

Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc. ref

2) They cannot store urls

This is a common limitation you will stumble upon one day.

What you cannot do ❌

:root { –url:“https://picsum.photos/id/1/200/300”;}.box { background:url(var(–url));}

Enter fullscreen mode Exit fullscreen mode

What you should do ✔️

:root { –url:url(“https://picsum.photos/id/1/200/300”);}.box { background:var(–url);}

Enter fullscreen mode Exit fullscreen mode

This limitation is related to how url() is parsed. A bit tricky to explain but as we can see the fix is pretty easy. Always add the url() part within the CSS variable.

If you want more accurate detail, I advise reading this Stack Overflow answer

3) They can make an invalid value valid!

This one is my favorite quirk and it’s the one that will give you a lot of headaches.

Let’s start with a basic example:

.box { background: red; background: linaer-gradient(red, blue);}

Enter fullscreen mode Exit fullscreen mode

Our .box will have a gradient coloration … wait, no it has a red background. Ah! I made a typo in linear-*. I can easily notice my mistake because the browser crossed the declaration and used the previous one.

Alt Text

Now, let’s introduce a variable:

.box { –color:red; background: var(–color); background: linaer-gradient(var(–color), blue);}

Enter fullscreen mode Exit fullscreen mode

Test the code and you will see that the background is now transparent and our second declaration is no more crossed because it’s now a valid one. You will even notice that the first declaration is the one crossed because the second one override it.

What the hell is happening here ??!!

When using a variable within a property the browser will only evaluate the value of such property at “computed-value time” because we need to first know the content of the variable. In such case, the browser will consider the value as valid when doing the cascade and only later it will become invalid.

In our case, the browser is considering the last declaration after resolving the cascade. Then when doing the evaluation, it seems to be invalid so it will be ignored. We won’t get back to the previous declaration since we already resolved the cascade and we end with no background so a transparent one.

You may think such behavior is illogical but it’s indeed logical because a value can be valid or invalid based on the CSS variable so the browser cannot really know from the beginning.

.box { –color:10px; /* a “valid” variable */ background: red; /* a “valid” declaration */ background:linear-gradient(var(–color),blue); /* a “valid” declaration that will override the first one */ /* The result is an “invalid” value … */ }

Enter fullscreen mode Exit fullscreen mode

If a property contains one or more var() functions, and those functions are syntactically valid, the entire property’s grammar must be assumed to be valid at parse time. It is only syntax-checked at computed-value time, after var() functions have been substituted. ref

and

A declaration can be invalid at computed-value time if it contains a var() that references a custom property with its initial value, as explained above, or if it uses a valid custom property, but the property value, after substituting its var() functions, is invalid. When this happens, the computed value of the property is either the property’s inherited value or its initial value depending on whether the property is inherited or not, respectively, as if the property’s value had been specified as the unset keyword. ref

To use easy words: a CSS variable will make the status of a propery in a standby mode until we do the evaluation. Only after the evaluation we can say if it’s valid or invalid. If it’s invalid then it’s too late, we cannot get back to use another one.

A related Stack Overflow question

4) They can be used unitless

Almost all the tutorials/courses will show you such example:

:root { –p: 10px;}.box { padding: var(–p);}

Enter fullscreen mode Exit fullscreen mode

But you can also do the following:

:root { –p: 10;}.box { padding: calc(var(–p)*1px);}

Enter fullscreen mode Exit fullscreen mode

Having the unit in the variable isn’t mandatory and in some case it’s even better to use a unitless value because adding a unit is fairly easy and we may need to use the same value with different unit.

Here is one example among many (taken from this answer )

Never forget this important feature. It will save you one day.

5) They can be animated

Initially, CSS variables are defined to be non-animatable properties as per the specification:

Animatable: no

But things have changed and thanks to the new @property we can do animation/transition with CSS variables.

The support is still low (especially on Firefox) but it’s time to get to know this.

Find below some use cases where I am relying on such feature:

I will be writing more articles to show the magic we can do with this. Stay tuned!

6) They cannot store the inherit value

Let’s consider the following example:

<div class=“box”> <div class=“item”></div></div>

Enter fullscreen mode Exit fullscreen mode

.box { border:2px solid red;}.item { –b:inherit; border:var(–b);}

Enter fullscreen mode Exit fullscreen mode

Intuitively, we may think that .item will inherit the same border of its parent element because –b contain inherit but it won’t (you can try and see).

As I explained in the (1), the common mistake is to think that CSS variables will simply store value that we can use later but no. CSS variables (custom properties) are ordinary properties so inherit apply to them and is not store inside them.

Example:

.box { –b:5px solid blue; /* we define the variable on the parent */}.item { –b:inherit; /* the child will inherit the same value so “5px solid blue”*/ border:var(–b); /* we will have “5px solid blue” */}

Enter fullscreen mode Exit fullscreen mode

As you can see, the logic of inheritance apply to them the same way as with common properties.

Worth to note that doing the above is useless because CSS variables are by default inherited. It’s like setting inherit to a property that is by default inherited (color for example).

This said, I have elaborated a technique to be able to use CSS variables with inherit

Nov 10 ’18 Comments: Answers: 1

3

Let’s consider this simplified example in order to illustrate the issue:

</p>

Enter fullscreen mode Exit fullscreen mode

The same logic apply to other keywords like unset and revert: How to set CSS variable to the value unset, “–unset-it: unset”?

7) They can be empty

Yes you can do the following:

.box { –color: ; background:var(–color); }

Enter fullscreen mode Exit fullscreen mode

The above is valid as per the specification:

Note: While <declaration-value> must represent at least one token, that one token may be whitespace. This implies that –foo: ; is valid, and the corresponding var(–foo) call would have a single space as its substitution value, but –foo:; is invalid.

Pay attention to the last sentence because we need to have at least one space. The below is invalid:

.box { –color:; background:var(–color); }

Enter fullscreen mode Exit fullscreen mode

Alt Text

This quirk is mainly used with the fallback feature to do some magic.

A basic example to understand the trick:

.box { background: linear-gradient(blue,transparent) var(–color,red); }

Enter fullscreen mode Exit fullscreen mode

<div class=“box”> I will have `background:linear-gradient(blue,transparent) red;`</div><div class=“box” style=“–color:green”> I will have `background:linear-gradient(blue,transparent) green;`</div><div class=“box” style=“–color: ;”> I will have `background:linear-gradient(blue,transparent) ;`</div>

Enter fullscreen mode Exit fullscreen mode

  1. The first box has no variable defined so the fallback will get used.
  2. The second one has a variable defined so it will get used
  3. The last one defined an empty variable so that emptyness will be used. It’s like we no more have the var(–color,red).

The empty value allow us to remove the var() declaration from a property! This can be useful when using var() within a complex value.

In case var() is used alone, the same logic apply but we will end having an empty value which is invalid for most of the properties.

If we took our first example we will have background: ; which will lead to an invalid value at “computed-value time” (remember the (3)) so a transparent background.

8) CSS variables are not C++ variables

Unfortunately, many developers tend to compare CSS variables to variables of other languages and end having a lot of issues in their logic. For this specific reason, I don’t want to call them variables but Custom properties because they are properties.

What everyone want to do

:root { –p:5px; –p:calc(var(–p) + 1px); /* let’s increment by 1px */}

Enter fullscreen mode Exit fullscreen mode

:root { –x: 5px; –y: 10px; /* let’s do a variable switch */ –c: var(–y); –y: var(–x); –x: var(–c);}

Enter fullscreen mode Exit fullscreen mode

.box { –s: 10px; margin:var(–s); /* I want 10px of margin */ –s: 20px; padding:var(–s): /* then 20px of padding */}

Enter fullscreen mode Exit fullscreen mode

All the above will never work. The first two are simply invalid because we have a cyclic dependencies since a variable is refering to itself (first example) or a group of variables are creating a cycle (the second example).

In The last example, both padding and margin will have 20px because the cascade will give priority to the last declaration –s: 20px that will get applied to both margin and padding.

This said, you should stop thinking C++, Javascript, Java, etc when working with CSS variables because they are custom properties having their own logic.

9) They only work from parent to child.

Remember this gold rule: CSS variables always travel from a parent element (or an ancestor) to child elements. They never travel from child to parent or between sibling elements.

This will lead us to the following mistake:

:root { –c1: red; –c2: blue; –grad: linear-gradient(var(–c1),var(–c2);}.box { –c1: green; background:var(–grad);}

Enter fullscreen mode Exit fullscreen mode

You think the background of .box will be linear-gradient(green, blue)? No, it will be linear-gradient(red, blue).

The root element is the uppermost element in the DOM so its an ancestor of our box element and our gold rule says that we can only do parent –> child so –c1 cannot go in the opposite direction to reach the root element, change –grad and then we get back in the other direction to re-send the changed value of –grad.

In such example, the .box will inherit the value of –grad defined with the values of –c1 and –c2 inside root. Changing –c1 will simply change the value of –c1 inside .box, nothing more.

Find below a more detailed answer I wrote around this subject:

Aug 25 ’18 Comments: 2 Answers: 1

16

I’m attempting to scale size via a var custom property in a way that the classes would compose without being coupled. The desired effect is that the 3 lists would be at 3 different scales but as demonstrated on CodePen all 3 lists are the same scale. I’m looking for…

Even the Stack Overflow team stumbled upon this quirk!

10) They can have strange syntaxes

A last and funny quirk.

Did you know that you can do the following?

body { :red; background:var();}

Enter fullscreen mode Exit fullscreen mode

Amazing, right? Yes, a CSS variable can be defined using only the two dashes. Related: Is “–” a valid CSS3 identifier?

You think the above is crazy, take a look at the following:

body { –📕:red; –📗:green; –📘:blue; –📙:orange;}

Enter fullscreen mode Exit fullscreen mode

Yes, emojis! you can define your variables using emojis and it works.

The syntax of CSS variables allow almost everything the only requirement is to start with –. You can also start with a number (ex: –1:). Related: Can a css variable name start with a number?

Why not only dashes:

body { ———:red; background:var(———);}

Enter fullscreen mode Exit fullscreen mode

Or the same variable storing two different values

body { –‎​:red; –‎:blue; background:linear-gradient(90deg, var(–‎​),var(–‎));}

Enter fullscreen mode Exit fullscreen mode

Try the above and you will get a gradient coloration!

To achieve such magic I am relying on a hidden character that make both of the variables different but visually we see them the same. If you try the code on jsfiddle.net You will see the following:

Alt Text

Of course, you should never use such thing in a real project unless you want to make your boss and coworkers crazy 😜

Замечания

Радиокнопки

Если любая из радиокнопок в группе (т.е., с одинаковым атрибутом name) имеет атрибут required, псевдокласс :invalid применяется ко всем из них, если ни одна из кнопок группы не выбрана.

Спецификация

Каждая спецификация проходит несколько стадий одобрения.

  • Recommendation (Рекомендация) — спецификация одобрена W3C и рекомендована как стандарт.
  • Candidate Recommendation (Возможная рекомендация) — группа, отвечающая за стандарт, удовлетворена, как он соответствует своим целям, но требуется помощь сообщества разработчиков по реализации стандарта.
  • Proposed Recommendation (Предлагаемая рекомендация) — на этом этапе документ представлен на рассмотрение Консультативного совета W3C для окончательного утверждения.
  • Working Draft (Рабочий проект) — более зрелая версия черновика после обсуждения и внесения поправок для рассмотрения сообществом.
  • Editor’s draft (Редакторский черновик) — черновая версия стандарта после внесения правок редакторами проекта.
  • Draft (Черновик спецификации) — первая черновая версия стандарта.

Пример

Этот пример представляет собой простую форму, цвета элементов которой зелёные, когда данные корректные, и красные, когда нет.

HTML

<form> <label>Введите URL:</label> <input type=url /> <br /> <br /> <label>Введите эл. почту:</label> <input type=email required/></form>

CSS

input:invalid { background-color: #ffdddd;}form:invalid { border: 5px solid #ffdddd;}input:valid { background-color: #ddffdd;}form:valid { border: 5px solid #ddffdd;}input:required { border-color: #800000; border-width: 3px;}

Браузеры

В таблице браузеров применяются следующие обозначения.

  •  — свойство полностью поддерживается браузером со всеми допустимыми значениями;
  •  — свойство браузером не воспринимается и игнорируется;
  •  — при работе возможно появление различных ошибок, либо свойство поддерживается лишь частично, например, не все допустимые значения действуют или свойство применяется не ко всем элементам, которые указаны в спецификации.

Число указывает версию браузера, начиная с которой свойство поддерживается.

Поддержка браузерами

BCD tables only load in the browser

Автор и редакторы

Автор: Влад Мержевич

Последнее изменение: 01.06.2018

Редакторы: Влад Мержевич

Рейтинг
( 1 оценка, среднее 5 из 5 )
Загрузка ...