Is there a way to interpolate CSS variables with url()?
You can perform interpolation with most CSS functions, including rgba()
(see an example here). In fact, interpolation is one of the main features of custom properties.
But you cannot do this with url()
, as url(var(--url))
is parsed not as a url(
function token followed by var(--url)
followed by a )
, but a single url()
token that is invalid because the var(--url)
is being treated as a URL itself, and unquoted URLs in url()
tokens cannot contain parentheses unless they are escaped. This means the substitution never actually occurs, because the parser never sees any var()
expressions in the property value — indeed, your background
declaration is completely invalid.
If you didn't understand any of that, that's fine. Just know that you cannot use var()
interpolation with url()
due to legacy reasons.
Even though the problem depicted in the question is related to the legacy url()
token, you cannot do this by building URL tokens out of several var()
expressions either, in case you were thinking of trying something like --uo: url(; --uc: );
or --uo: url("; --uc: ");
, and background: var(--uo) var(--url) var(--uc);
. This is because custom properties cannot contain unmatched string delimiters or parts of url()
tokens (called bad URL tokens).
If you want to specify a URL in a custom property, you need to write out the entire url()
expression, and substitute that entire expression:
:root {
--url: url("https://download.unsplash.com/photo-1420708392410-3c593b80d416");
}
body {
background: var(--url);
}
Or, use JavaScript instead of var()
to perform the interpolation.
How to pass CSS variable in background URL
You can use it like the following:
:root { --color: rgba(255, 0, 0, 0.5); --color2: rgba(0, 255, 0, 0.5); --url: url(https://static-cdn.jtvnw.net/ttv-boxart/Apex%20Legends-300x400.jpg);}.tinted-image { height: 125px; background: linear-gradient(to right, var(--color), var(--color2)), var(--url); width: 200px;}
<div class="tinted-image"></div>
How to assign background-image url dynamically with only html and css?
Even if I feel like it would be much easier to do with simply using inline background-image
property (especially if you want to concatenate url string). For simple use you can do that like that:
<div class="var" style="--url: url(https://picsum.photos/320/240)"></div>
.var {
background-image: var(--url);
}
For reasons unclear for me, using url(var(--url));
doesn't seem to be working at all (at least at Chrome)
Css variable interpolation Styled Components
So you can't use CSS variables for anything but property declarations. They don't work in selectors.
But I think you should be able to achieve the same here by other means. I know you mentioned I can't use JS variables in my case
and probably your usecase is less straightforward than the example, but can't you either just use React props interpolation that comes with styled-components
&:nth-child(${p => p.nthChildLargeScreen}) {
//...styles...
}
@media (max-width: 768px) {
&:nth-child(${p => p.nthChildLargeScreen}) {
//...reverse styles from above...
}
&:nth-child(${p => p.nthChildSmallScreen}) {
//...styles...
}
}
Or even just global variables?
&:nth-child(${NTH_CHILD_LARGE_SCREEN}) {
//...styles...
}
...etc
If you can write the styled component, and define CSS variables, in the first place, seems like you should be able to do at least one of the above instead.
CSS Custom property Url isn't working in Asp .Net
The Solution was to add c# Url.Content bewfore the css adding the css url like this :
href="@Url.Content("~/wwwroot/Uploads/@item.ImagePath")" />
You can check the following link:
https://stackoverflow.com/a/48386317/15091527
Related Topics
Css @Font-Face Not Working With Firefox, But Working With Chrome and Ie
Css Selector For "Foo That Contains Bar"
How to Set Background Image and Opacity in the Same Property
Better Way to Set Distance Between Flexbox Items
How to Use Jsf Generated HTML Element Id With Colon ":" in CSS Selectors
Css - Why Doesn't Percentage Height Work
How to Show Only Corner Borders
Line Before and After Title Over Image
How to Make Twitter Bootstrap Menu Dropdown on Hover Rather Than Click
Make Flex-Grow Expand Items Based on Their Original Size
Css Text-Overflow in a Table Cell
How to Hide an Element When Printing a Web Page
Does Firefox Support Position: Relative on Table Elements
Difference Among Col-Lg-*, Col-Md-* and Col-Sm-* in Bootstrap
Bootstrap Take Full Width for Select Drop Down