0
How to add css inside a js document?
Any syntax?
7 Answers
+ 3
It's not really embed css from JavaScript, but you could set css styles from JavaScript object.
https://code.sololearn.com/Wi9OqW23izWQ/?ref=app
This method is commonly use in react.js, I present it in pure JavaScript here.
+ 3
If you mean to say that how to style page using JavaScript then, Yes we can do this:-
Syntax: -
style.property(css property) = "value";
Example:-
document.getElementById("paragraph").style.color = "Red";
+ 2
I don't see any way to do it (I could be wrong), as I understand it, CSS and JavaScript are put into separate files so it's easier to update and make changes. Rather than mixing them into one file.
But why you want to do this anyway?
+ 2
Thank you
+ 1
Sorry, I didn't get what you want to do. Can you elaborate with details?
0
Like how to embed css into a js document
0
You can do it using DOM elements.
DOM elements are those things through which you can acess styles in javascript.
first define any element and give it a id:
<h1 id="example">some text</h1>
now go into js and make a let,with id value:
let heading = document.getElementById('example');
then,to make color red,acess color styles like this:
heading.style.color='red';
congrats! you have made text red