CSS variables use in Vue
up vote
1
down vote
favorite
Is it possible to use pure CSS variables with Vue without having to link any stylesheets or use SASS/PostCSS? Unsure why I'm unable to get this to work in its most basic form.
<template>
<div id="test">
TEST
</div>
</template>
<style scoped>
:root {
--var-txt-color: #c1d32f;
}
#test {
color: var(--var-txt-color);
}
</style>
css vue.js global-variables css-variables
add a comment |
up vote
1
down vote
favorite
Is it possible to use pure CSS variables with Vue without having to link any stylesheets or use SASS/PostCSS? Unsure why I'm unable to get this to work in its most basic form.
<template>
<div id="test">
TEST
</div>
</template>
<style scoped>
:root {
--var-txt-color: #c1d32f;
}
#test {
color: var(--var-txt-color);
}
</style>
css vue.js global-variables css-variables
1
what if you use something else than :root? trybody
or a container element
– Temani Afif
Nov 10 at 14:49
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
1
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is it possible to use pure CSS variables with Vue without having to link any stylesheets or use SASS/PostCSS? Unsure why I'm unable to get this to work in its most basic form.
<template>
<div id="test">
TEST
</div>
</template>
<style scoped>
:root {
--var-txt-color: #c1d32f;
}
#test {
color: var(--var-txt-color);
}
</style>
css vue.js global-variables css-variables
Is it possible to use pure CSS variables with Vue without having to link any stylesheets or use SASS/PostCSS? Unsure why I'm unable to get this to work in its most basic form.
<template>
<div id="test">
TEST
</div>
</template>
<style scoped>
:root {
--var-txt-color: #c1d32f;
}
#test {
color: var(--var-txt-color);
}
</style>
css vue.js global-variables css-variables
css vue.js global-variables css-variables
edited Nov 10 at 14:49
Temani Afif
59.3k93472
59.3k93472
asked Nov 10 at 14:47
Ben Casalino
4591619
4591619
1
what if you use something else than :root? trybody
or a container element
– Temani Afif
Nov 10 at 14:49
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
1
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03
add a comment |
1
what if you use something else than :root? trybody
or a container element
– Temani Afif
Nov 10 at 14:49
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
1
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03
1
1
what if you use something else than :root? try
body
or a container element– Temani Afif
Nov 10 at 14:49
what if you use something else than :root? try
body
or a container element– Temani Afif
Nov 10 at 14:49
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
1
1
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This won't work as expected because of scoped
attribute for stylesheet. Example above compiles into:
[data-v-4cc5a608]:root {
--var-txt-color: #f00;
}
And, as you understand, it will not target actual :root
element.
It can be solved by:
Not using
scoped
attribute for this stylesheet. Notice that it may cause styles conflict with other variables declarations for:root
element.
Using current component's wrapping element as root. If we declare variables this way:
.test {
--var-txt-color: #c1d32f;
color: var(--var-txt-color);
}
.test-child-node {
background-color: var(--var-txt-color);
}
Then it will can reuse variables for other elements of the same component. But still, it won't be possible to use declared variables inside child components without removing scoped
, if it is the case.
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This won't work as expected because of scoped
attribute for stylesheet. Example above compiles into:
[data-v-4cc5a608]:root {
--var-txt-color: #f00;
}
And, as you understand, it will not target actual :root
element.
It can be solved by:
Not using
scoped
attribute for this stylesheet. Notice that it may cause styles conflict with other variables declarations for:root
element.
Using current component's wrapping element as root. If we declare variables this way:
.test {
--var-txt-color: #c1d32f;
color: var(--var-txt-color);
}
.test-child-node {
background-color: var(--var-txt-color);
}
Then it will can reuse variables for other elements of the same component. But still, it won't be possible to use declared variables inside child components without removing scoped
, if it is the case.
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
add a comment |
up vote
1
down vote
accepted
This won't work as expected because of scoped
attribute for stylesheet. Example above compiles into:
[data-v-4cc5a608]:root {
--var-txt-color: #f00;
}
And, as you understand, it will not target actual :root
element.
It can be solved by:
Not using
scoped
attribute for this stylesheet. Notice that it may cause styles conflict with other variables declarations for:root
element.
Using current component's wrapping element as root. If we declare variables this way:
.test {
--var-txt-color: #c1d32f;
color: var(--var-txt-color);
}
.test-child-node {
background-color: var(--var-txt-color);
}
Then it will can reuse variables for other elements of the same component. But still, it won't be possible to use declared variables inside child components without removing scoped
, if it is the case.
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This won't work as expected because of scoped
attribute for stylesheet. Example above compiles into:
[data-v-4cc5a608]:root {
--var-txt-color: #f00;
}
And, as you understand, it will not target actual :root
element.
It can be solved by:
Not using
scoped
attribute for this stylesheet. Notice that it may cause styles conflict with other variables declarations for:root
element.
Using current component's wrapping element as root. If we declare variables this way:
.test {
--var-txt-color: #c1d32f;
color: var(--var-txt-color);
}
.test-child-node {
background-color: var(--var-txt-color);
}
Then it will can reuse variables for other elements of the same component. But still, it won't be possible to use declared variables inside child components without removing scoped
, if it is the case.
This won't work as expected because of scoped
attribute for stylesheet. Example above compiles into:
[data-v-4cc5a608]:root {
--var-txt-color: #f00;
}
And, as you understand, it will not target actual :root
element.
It can be solved by:
Not using
scoped
attribute for this stylesheet. Notice that it may cause styles conflict with other variables declarations for:root
element.
Using current component's wrapping element as root. If we declare variables this way:
.test {
--var-txt-color: #c1d32f;
color: var(--var-txt-color);
}
.test-child-node {
background-color: var(--var-txt-color);
}
Then it will can reuse variables for other elements of the same component. But still, it won't be possible to use declared variables inside child components without removing scoped
, if it is the case.
edited Nov 10 at 15:51
answered Nov 10 at 15:45
aBiscuit
1,2121513
1,2121513
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
add a comment |
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
Ended up using SASS, but the fact that its 'scoped' makes sense on why its not working, thank you so much!
– Ben Casalino
Nov 10 at 15:57
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240081%2fcss-variables-use-in-vue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what if you use something else than :root? try
body
or a container element– Temani Afif
Nov 10 at 14:49
I just tried using a body tag but that did not work. Could it be that because its a component and not on the main App.vue causes it to not understand the :root? This is the component in particular I'm trying to get to work on: github.com/bencasalino/nba-timeline-project/blob/master/src/…
– Ben Casalino
Nov 10 at 14:57
1
I don't know as I know CSS variable but not experienced with Vue.js ... if you are able to give a link with the working app I can help you by looking at the generated code
– Temani Afif
Nov 10 at 15:03