How to change css file using js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to change css file using js?

Is it possible to modify head section, for example a source of CSS file by using JavaScript?

15th Jul 2016, 3:05 PM
ThreeG
ThreeG - avatar
4 Answers
+ 4
i recommend you jQuery. It makes this very easy. $(".your-class").css("color", "red"); $("#your-id").css("position", "absolute"); $("h1").css("font-size", 40); You can edit css like this with jQuery. And you can do much more.
16th Jul 2016, 10:57 PM
Mahmud Özgür
Mahmud Özgür - avatar
+ 4
Thanks guys, you helped me a lot :)
17th Jul 2016, 5:11 AM
ThreeG
ThreeG - avatar
+ 2
as Mahmus Ozgur said, use jQuery. if you want to add more than one style, put them in a class in css file and the use addClass() function. it will be easier. there is also removeClass function to change your styles.
17th Jul 2016, 5:42 AM
Viran Malaka
Viran Malaka - avatar
+ 1
try this; var elements = document.getElementsByTagName('link'); var oldUrl = 'a'; //url to replace var newUrl = 'b' //url that replace the old url for(var i = 0; i<elements.length; i++){ if(elements[i].href == oldUrl){ elements.href = newUrl; } }
16th Jul 2016, 10:23 PM
Fernando
Fernando - avatar