CSS custom properties: a guide for deep beginners

You can do it. Finally, as mentioned in the introduction, browser support is excellent. If it is the disadvantage of custom attributes, what are the disadvantages of using these CSS attributes? Of course, there is nothing without the back of the coin. In this case, complexity may increase. If you use many variables, you must track the names of the variables and the actions performed. The
In addition, CSS custom properties are not compatible with previous browsers, and it is not always easy to declare replacements (as described below). Finally, CSS custom properties may affect page performance based on usage. Page speed is a very important quality indicator on a website, so you should know this. But in general, the advantages of this technology far outweigh the disadvantages. Let’s see how to use it? How to use CSS variables – you must define custom attributes before you can use step-by-step custom attributes. All valid CSS values are available, including color, size, string, and time. The
–Main color: \1f8aad– Default box shadow: 0 02px 2px RGBA (0, 0, 0, 0.6)– Letter spacing: normal– Font primary: -apple-system, blinkmacsystemfont, robot, sans serif– Logo–max width: 300px; Second, it must be among the selectors who define the scope. The usual method is to define the attributes to be used globally, such as the color configuration table, at the beginning of the style sheet under the root selector. The
: root {–global–color black: \000; –global–color dark gray: \28303d; –global–color gray: \39414d; –global–color light gray: \f0f0;} If curious: root is the same as HTML, but more specific. In the above example, you may have learned, so the way to declare variables in CSS is to add –. Once defined, variables can be used wherever needed via var (), and the names of custom attributes are used internally. The
Male Entry content {color: var (–global–color dark gray);} Isn’t that easy? A few things to remember: user-defined attribute names can contain letters, numbers, and dashes. Because they are case sensitive, –maincolor and –maincolor are treated as different variables. Attributes are inherited, so you define custom attributes for the parent element. If you do not define child elements, when using custom attributes, the latter will inherit the values of the parent element. That’s all, it’s a way to use CSS variables at the most basic level. Of course, the devil is in the details. Let’s look at a few use cases and actual cases, and then look at the theme. The
To better understand the use cases and sample CSS custom properties, you can end this post with a few practical examples to get an impression of the application. One advantage of placing variables within variables CSS variables is that variables can overlap each other. Sets the global default color and assumes that the borders use the same color. You can: Root {–global–color border: var (—global–color primary);} Why do you do this? It can be reduced to the naming rules of variables. When you define the border color further down, you may not be sure whether this is the entire default color or another color. But you don’t need to know. Only global border colors can be used. The rest have been processed. As mentioned before, defining CSS selectors for custom properties
The corresponding scope is also defined. This also means that you can change the attribute value according to the context: Root {–main color: \1f8aad;} Landing page header {–main color: \183cba; color: var (–main color);} In the above example, for global definition, the root selector defines –main-color. But. In the context of langt page header, we want different values. Therefore, before using the new selector, we redefine –main-color. This generates an exception for a context without triggering the initial value. Although previous browsers supported alternative declaration browsers well, previous browsers faced difficulties due to CSS custom properties. For these reasons, sometimes you need to enter a replacement. However, this is not as easy to operate as a font collection. For example, only a few options are available, and the browser can operate along the line. On the contrary, in many cases, you must first declare fallback and then use custom attributes to take subsequent actions: Root {–main color: red;} Entry header {background color: red; \/ * fallback * \/ background color: VAR (–main color);} In this way, the latest browser will select the second declaration (because the first declaration is overwritten), but the previous browser that does not know the CSS variable will treat the second line as invalid. Another case where you may need to replace another definition for the node side value is that the user-defined attribute value is not defined. This happens occasionally, neglecting important things. Don’t worry. CSS variables are also allowed to be case sensitive, as shown below: Widget Title {font family: VAR (–heading font, robot);} If the user-defined attribute –heading font is not defined, the browser can only use the substitute robot through the downlink. It’s actually very simple. The only thing to consider is that the substitution must be in the braces of VaR (). You can also use a variable as an alternative to other variables and include other alternative values. Widget Title {font family: VAR (–heading font, VAR (–base font, robot));} You can also use CSS variables to add them above the operation execution, and use CSS custom attributes in the operation. You can use calc() to add, subtract, multiply, and divide as needed. For example, you can use it to define the percentage of font size that remains the same when the initial font size changes: Root {–font size base: 1.25rem; –heading–font size h1: Calc (2.5 * var (–font size base:); –heading–font size h2: Calc (2 * var (–font size base:); –heading–font size h3: Calc (1.75 * var (–font size base:);} Another popular example is calculating spacing or complementary colors. For more control, another wonderful application of value splitting CSS custom properties is that you can split CSS values and control other parts. For example, you can split the RGBA () declaration into variables instead of repeating them.: Root {–r: 255; –g: 0; –b: 0; –a: 0.8;} Widget {–r: 0; –g: 255; background color: RGBA (VaR (- -r), VAR (- -g), VAR (- -b), VAR (- -a));} Different from reactive design
It can also be used to change properties after a media query@ Media only screen and (min width: 482px) {: root {–r: 0; –g: 255;}} Development workflows as CSS custom attributes improving CSS variables or custom attributes is an excellent tool in the design \/ development toolbox. This reduces duplication, makes global changes easier, and adds automated documents to style sheets. In addition, as shown above, it is easy to use without explanation. With the information in this post, you can now use CSS custom properties directly and learn more about the topic. Because more can be found, this is just the beginning. What is your favorite use case for CSS variables? Please tell the following comments!

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *