0

I have tried to to that using this line of code but it doesn't work

@import url("Javascriptfile.js");

and <script type="text/javascript" src="Script.js">

these two ways doesn't work.

in js file I have

export const PrimaryColor = "#4267B2";

and I want to import it in my css file how can i do it?

1
  • To make changes to your css dependent on your js, I believe you need to have your js modify the loaded css objects, but my knowledge is fairly old. An alternative is to make your js dependent on the css, by adding style info classes that the js reads at startup. So your css could define a style for PrimaryColor, and your javascript could read it, and use that value as the constant variable PrimaryColor that it then uses for other features not directly driven by the css. Commented Oct 21, 2019 at 14:07

1 Answer 1

2

I do not believe this is possible in any way.

You can manipulate CSS with JavaScript to a certain extent, but it is mostly done through DOM manipulation (acting on class names) or generating CSS programmatically (but it is still CSS in the end).

I don't think it is ever possible to access "JavaScript world" from the scope of CSS.

However, if you want to use variable names in CSS to reference constants, like colors, sizes, etc., you can use the CSS custom properties feature : https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.