Home > Web Front-end > CSS Tutorial > Can CSS Variables Be Interpolated with the `url()` Function?

Can CSS Variables Be Interpolated with the `url()` Function?

Linda Hamilton
Release: 2024-12-27 05:55:09
Original
520 people have browsed it

Can CSS Variables Be Interpolated with the `url()` Function?

Interpolation with CSS Variables

Can CSS variables be interpolated with the url() function?

Custom CSS properties facilitate interpolation for most functions, including rgba(). However, interpolation with url() presents challenges due to legacy reasons.

What is the limitation with url()?

The url() token cannot be split into var() expressions because the parser treats var(--url) as an unquoted URL. Consequently, interpolation does not occur, rendering the background declaration invalid.

Alternative Approaches

To avoid the url() limitation, viable options include:

  • Specifying the entire url() expression in the custom property:
:root {
  --url: url("https://download.unsplash.com/photo-1420708392410-3c593b80d416");
}

body {
  background: var(--url);
}
Copy after login
  • Employing JavaScript for interpolation instead of var().

The above is the detailed content of Can CSS Variables Be Interpolated with the `url()` Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template