Home Web Front-end CSS Tutorial How to set CSS3 var variables in HTML tags and JS

How to set CSS3 var variables in HTML tags and JS

Nov 27, 2018 pm 05:10 PM
html tag


The content of this article is about how to set CSS3 var variables in HTML tags and JS. It has certain reference value and friends in need You can refer to it, I hope it will be helpful to you.

1. Set the CSS variables in the HTML tag

as follows:

<div style="--color: #cd0000;">
<img  src="/static/imghw/default1.png"  data-src="mm.jpg"  class="lazy"     style="max-width:90%" alt="How to set CSS3 var variables in HTML tags and JS" >
</div>
Copy after login

Just set it in the style attribute just like a normal CSS statement.

The effect is as follows:

How to set CSS3 var variables in HTML tags and JS

2. Set CSS variables in JS

as follows, HTML representation:

< ;div id="box">

How to set CSS3 var variables in HTML tags and JS

< /div>

If you want var (--color) to take effect, execute the following JavaScript code:

box.style.setProperty('--color', '#cd0000' );

That is, use the setProperty() method, the effect is as follows GIF screenshot:

How to set CSS3 var variables in HTML tags and JS

3. Obtain CSS variables in JS

You can use the getPropertyValue() method to obtain CSS variables in JS, as shown below:

// 获取 --color CSS 变量值
var cssVarColor = getComputedStyle(box)。getPropertyValue(&#39;--color&#39;);
// 输出cssVarColor
// 输出变量值是:#cd0000
console.log(cssVarColor);
Copy after login

4. About CSS3 var() variables

CSS3 var() variables are a good thing, when they were introduced 2 years ago Not many browsers support it yet, but now, Edge16 also fully supports it.

How to set CSS3 var variables in HTML tags and JS

The above is an introduction to how to set CSS3 var variables in HTML tags and JS. If you want to know more about CSS3 tutorial, please pay attention PHP Chinese website.



The above is the detailed content of How to set CSS3 var variables in HTML tags and JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove HTML tags using Python regular expressions How to remove HTML tags using Python regular expressions Jun 22, 2023 am 08:44 AM

HTML (HyperTextMarkupLanguage) is a standard language for creating Web pages. It uses tags and attributes to describe various elements on the page, such as text, images, tables, links, etc. However, when processing HTML text, it is difficult to quickly extract the text content for subsequent processing. At this time, we can use regular expressions in Python to remove HTML tags to quickly extract plain text. In Python, regular tables

How to extract HTML tag content using regular expressions in Go language How to extract HTML tag content using regular expressions in Go language Jul 14, 2023 pm 01:18 PM

How to use regular expressions to extract HTML tag content in Go language Introduction: Regular expression is a powerful text matching tool, and it is also widely used in Go language. In the scenario of processing HTML tags, regular expressions can help us quickly extract the required content. This article will introduce how to use regular expressions to extract the content of HTML tags in Go language, and give relevant code examples. 1. Introduce related packages First, we need to import related packages: regexp and fmt. regexp package provides

How to remove HTML tags from string in PHP? How to remove HTML tags from string in PHP? Mar 23, 2024 pm 09:03 PM

PHP is a commonly used server-side scripting language that is widely used in website development and back-end application development. When developing a website or application, you often encounter situations where you need to process HTML tags in strings. This article will introduce how to use PHP to remove HTML tags from strings and provide specific code examples. Why do you need to remove HTML tags? HTML tags are often included when processing user input or text obtained from a database. Sometimes we want to remove these HTML tags when displaying text

How to escape html tags in php How to escape html tags in php Feb 24, 2021 pm 06:00 PM

In PHP, you can use the htmlentities() function to escape html, which can convert characters into HTML entities. The syntax is "htmlentities(string,flags,character-set,double_encode)". You can also use the html_entity_decode() function in PHP to de-escape html and convert HTML entities into characters.

How to remove HTML tags from given string in Java? How to remove HTML tags from given string in Java? Aug 29, 2023 pm 06:05 PM

String is a final class in Java, it is immutable, which means we cannot change the object itself, but we can change the reference of the object. HTML tags can be removed from a given string using the replaceAll() method of String class. We can remove HTML tags from a given string using regular expressions. After removing the HTML tags from the string, it returns a string as normal text. Syntax publicStringreplaceAll(Stringregex,Stringreplacement) example publicclassRemoveHTMLTagsTest{&nbs

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use HTML tags in HTML tables? How to use HTML tags in HTML tables? Sep 08, 2023 pm 06:13 PM

We can easily add HTML tags in the table. HTML tags should be placed inside <td> tags. For example, add paragraph <p>…</p> tags or other available tags inside the <td> tag. Syntax The following is the syntax for using HTMl tags in HTML tables. <td><p>Paragraphofthecontext</p><td>Example 1 An example of using HTML tags in an HTML table is given below. <!DOCTYPEhtml><html><head&g

PHP regular expression method to verify basic HTML tags PHP regular expression method to verify basic HTML tags Jun 24, 2023 am 08:07 AM

PHP is an efficient web development language that supports regular expression functions and can quickly verify the validity of input data. In web development, HTML is a common markup language, and validating HTML tags is a very important method for web form validation. This article will introduce basic HTML tag verification methods and how to use PHP regular expressions for verification. 1. Basic structure of HTML tags HTML tags consist of element names and attributes surrounded by angle brackets. Common tags include p, a, div

See all articles