Can CSS Variables Be Used Inside the `url()` Function?
Can CSS Variables Interpolate with url()?
Despite the widespread use of CSS variables, there remains a limitation when it comes to interpolating them within the url() function. This limitation arises from the legacy nature of the url() token.
In modern CSS, interpolation of custom properties is a fundamental feature. It allows dynamic values to be inserted into various functions, such as rgba(). However, the url() function stands as an exception to this rule.
When using url(var(--url)), the parser interprets it as a single url() token, not as a combination of a url() function token and a variable expression. As a result, the variable is treated as a URL itself, rendering the interpolation invalid.
To circumvent this issue, avoid constructing URL tokens from multiple variable expressions in url(). This includes approaches such as --uo: url(; --uc: ); and background: var(--uo) var(--url) var(--uc);. Custom properties cannot contain unmatched string delimiters or parts of url() tokens.
For backgrounds, the solution lies in specifying the complete url() expression within the custom property and substituting it directly:
:root { --url: url("https://download.unsplash.com/photo-1420708392410-3c593b80d416"); } body { background: var(--url); }
Alternatively, JavaScript can be utilized to perform the interpolation instead of var().
The above is the detailed content of Can CSS Variables Be Used Inside the `url()` Function?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...
