This css working OK
.my_div{
display: inline-block;
width: 200px;
height: 200px;
line-height: 200px;
font-size: 50px;
text-align: center;
border: solid 1px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
}
.bg1{
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
}
html
<div class="my_div bg1">
.bg1
</div>
If I try :root{--color2: 23;}
and background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
it’s ok too.
but if I add another line with -moz-radial-gradient
or -o-radial-gradient
or ms-radial-gradient
or simply radial-gradient
with hsla(var(--color2), 100%, 56%, 1)
that fail in all navigators
See code snippet for live results:
:root{
--color2: 23;
}
.my_div{
display: inline-block;
width: 200px;
height: 200px;
line-height: 200px;
font-size: 50px;
text-align: center;
border: solid 1px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
}
.bg1{
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
}
.bg2{
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
}
.bg3{
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
}
<div class="my_div bg1">
.bg1
</div>
<div class="my_div bg2">
.bg2
</div>
<div class="my_div bg3">
.bg3
</div>
I have tried also :
:root{
--color2: 23;
--compozite-color: hsla(var(--color2), 100%, 56%, 1);
}
And in css for multiple navigators not working … only work for one
This work:
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, var(--compozite-color) 100%);
This NOT WORK: (nor in webkit that worked before)
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, var(--compozite-color) 100%);
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, var(--compozite-color) 100%);
I have tried also (but not work):
.bg_webkit{
background-image: -webkit-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
}
.bg_moz{
background-image: -moz-radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
}
.bg{
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
}
…
<div class="my_div bg_webkit bg_moz bg">
.bg1
</div>
<div class="my_div bg_webkit bg_moz bg">
.bg2
</div>
<div class="my_div bg_webkit bg_moz bg">
.bg3
</div>
This (for only one navigator) work (but it doesn’t help me)
<div class="my_div bg_webkit">
.bg1
</div>
2
Answers
Short Answer:
The CSS property (unprefixed version) should be:
background-image: radial-gradient(circle closest-side at center center, #fedc3f 0%, hsla(var(--color2), 100%, 56%, 1) 100%);
More explanation:
Your original property
background-image: radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
is not valid. It does not match the description here.I googled
"center center, circle contain"
and found this link which says the syntax for radial gradients has changed (that was 2012, by the way).So, while
-webkit-radial-gradient
and-moz-radial-gradient
still support the old syntax,radial-gradient
doesn’t.However, (in the case where there is no variable), when that invalid syntax is encountered, the browser will ignore it. For example, in Chrome, as you can see from the Developer Tools, what is applied to
.bg1
isbackground-image: radial-gradient(center center, circle contain, #fedc3f 0%, #ff7420 100%);
.Note that it also ignores
-moz-radial-gradient
one.However, when there is a variable, it will not "simply ignore the invalid value". Instead, it uses the property value with a variable anyway (overriding the previous values), and only fails after substituting (see this other thread).
That also explains why
.bg2
does not work in Chrome (it tries to load-moz-radial-gradient
then fails), but it works in Firefox anyway.And
.bg3
does not work for Chrome and Firefox, as both will try to use your unprefixedradial-gradient(...)
and fail.So, the solution is use a valid value for the unprefixed one. You can replace
center center, circle contain
withcircle closest-side at center center
.Also, since you are using
var()
, that means you don’t need to use-moz-radial-gradient
or-webkit-radial-gradient
anymore. By checking caniuse: "radial-gradient" and caniuse: "custom properties", we can see thatradial-gradient
is supported even earlier thanvar
(except that we lack data for "radial-gradient" for some less common browsers, greyed out in the link above)(And in case someone is curious about the case of ‘cover’,
center center, circle cover
should be replaced withcircle farthest-corner at center center
)