admin管理员组文章数量:1429085
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
2 Answers
Reset to default 5you 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);
}
本文标签: Javascript color code variables inside the css file (React JS)Stack Overflow
版权声明:本文标题:Javascript color code variables inside the css file (React JS) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460045a2659282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论