How to use variables in CSS

Source: Internet
Author: User

The three main things you should know when using CSS variables

    • Custom properties
    • var () function
    • : Root Pseudo class
One, custom properties

We can define the reused property as a custom property, so that we need to modify the property value in the custom attribute only when we have to change it later.

: root {

--textcolor: #444;

}

The code above is the way to write custom attributes, and it is important to note that the custom attributes begin with

Two, Var () function

We need to use the Var () function to read the variable, otherwise the browser doesn't know what your custom attribute is.

The Var () function can also specify a second property, which represents the default value of the property

p {

Color:var (--textcolor);

}

To set the font color of the P tag, you can write it like the code above

It's the same as p {color: #444;}

Three, Root pseudo class

Before you say the root pseudo class, let's start by talking about the scope of CSS custom properties.

The same CSS custom attribute, which can be declared in multiple selectors, is read with the highest priority attribute in effect.

Body {

--bgcolor:red;

}

. Content {

--textcolor:blue;

}

The code above, the scope of-bgcolor is body, the scope of--textcolor is. Content for this reason, it is best to place the custom attribute in the root element: root, so that any selector can read them.

How to use variables in CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.