Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Javascript into CSS

I've been thinking about something. On my forum, there are default CSS codes that the users can choose from. This changes everything from background to text color. I have a javascript code. Is it possible to make it so that the javascript is part of the CSS, so that if a certain CSS code is defaulted, then part of it is that javascript?

like image 486
Eric Avatar asked Dec 27 '10 01:12

Eric


People also ask

Can I put JavaScript in CSS?

You can, but you really ought not to. Instead, use separate script files which happen to correspond to the CSS files you use.

Can I put script in a CSS file?

To link a CSS file with your HTML file, you have to write the next script on your HTML file inside the head tag. To link a Js file with your HTML, you only have to add the source of the script inside the body tag or outside; it doesn't matter.

How do I get JavaScript in CSS?

First, you need to select the element with querySelector . Then, you use getComputedStyle to get the element's styles. If you log style , you should see an object that contains every CSS property and their respective values. You can also see this object in Chrome's and Firefox's dev tools.


1 Answers

You can, but you really ought not to. Instead, use separate script files which happen to correspond to the CSS files you use. Maybe ask another question where you lay out a specific scenario where you're trying to solve a specific problem, and we can probably tell you the practice that's generally done to solve it.

like image 181
Dave Markle Avatar answered Oct 02 '22 12:10

Dave Markle