0

How to add css inside a js document?

Any syntax?

25th Sep 2019, 6:45 AM
B.D
B.D - avatar
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.
25th Sep 2019, 11:00 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 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";
25th Sep 2019, 11:11 AM
Puneet
+ 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?
25th Sep 2019, 7:44 AM
Ipang
+ 2
Thank you
25th Sep 2019, 12:24 PM
B.D
B.D - avatar
+ 1
Sorry, I didn't get what you want to do. Can you elaborate with details?
25th Sep 2019, 7:30 AM
Ipang
0
Like how to embed css into a js document
25th Sep 2019, 7:35 AM
B.D
B.D - avatar
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
10th Feb 2023, 11:47 AM
RandomTuber
RandomTuber - avatar