CSS variables use in Vue









up vote
1
down vote

favorite
1












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>









share|improve this question



















  • 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














up vote
1
down vote

favorite
1












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>









share|improve this question



















  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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? 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












  • 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







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












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.






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer






















  • 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














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.






share|improve this answer






















  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin