最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript color code variables inside the css file (React JS) - Stack Overflow

programmeradmin1浏览0评论

I have the list of color codes in the JS file.

JavaScript:

export var ColorCodes = { 
    first:'#D09FD2',  
    second:'#A72500',  
    third:'#F67GHI',   
    fourth:'#8FERDD',  
} 

I tried to use the js variables inside the CSS file like below.

it throws errors.

CSS:

import 'my.js'

.firstColor{

    font-size:15px;
    color:ColorCodes.first;
    }

.secColor{

    font-size:12px;
    color:ColorCodes.second;
    }

HTML

<div class="firstColor">aaa</div>   

I have the list of color codes in the JS file.

JavaScript:

export var ColorCodes = { 
    first:'#D09FD2',  
    second:'#A72500',  
    third:'#F67GHI',   
    fourth:'#8FERDD',  
} 

I tried to use the js variables inside the CSS file like below.

it throws errors.

CSS:

import 'my.js'

.firstColor{

    font-size:15px;
    color:ColorCodes.first;
    }

.secColor{

    font-size:12px;
    color:ColorCodes.second;
    }

HTML

<div class="firstColor">aaa</div>   
Share Improve this question edited Sep 23, 2019 at 7:47 Laczkó Örs 1,1161 gold badge27 silver badges43 bronze badges asked Sep 23, 2019 at 7:23 John KenJohn Ken 9621 gold badge13 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

you cannot use js inside css, instead you should abstract your color codes inside your css and then use dynamic classes via javascript

There is a thing you can try, according with w3 it works, check it here .

:root {
  --first-color: #D09FD2;
  --second-color: #A72500;
  --third-color: #F67GHI;
  --fourth-color: #8FERDD;
}

.firstColor{

    font-size:15px;
    color:var(--first-color);
    }

.secColor{

    font-size:12px;
    color:var(--second-color);
    }
发布评论

评论列表(0)

  1. 暂无评论