SFC CSS v-bind() not working as expected
Vue version
3.2.45
Link to minimal reproduction
Steps to reproduce
- Create SFC
- Assing color (as string) to variable
- Use v-bind to bind color to css variable in :root pseudo element
- Use the variable in other css class
What is expected?
I expected that css variable would be available for me to use in other classes.
What is actually happening?
CSS variable looks like its undefined and it is not working.
When you look at reproduction, you can see that I made two test classes. One with
background: var(--test-color)
,
other with
background: v-bind(colorVar)
.
The second one works properly, while the first one, which is the issue about, does not.
System Info
System:
OS: Windows 10 10.0.19045
CPU: (6) x64 Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz
Memory: 3.68 GB / 15.81 GB
Binaries:
Node: 16.18.0 - C:\Program Files\nodejs\node.EXE
npm: 9.2.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.54)
Internet Explorer: 11.0.19041.1566
npmPackages:
vue: ^3.2.45 => 3.2.45
Any additional comments?
No response
Yeah that won't work because the CSS variables generated by using v-bind() in the component don't live in the :root
context, so they are not accessible there. They are applied to the component's root elements, as you can see by inspecting the DOM.
This is intended behaviour so that variables do not bleed outside of the component.