How to use JavaScript to apply CSS

You can try out the below methods to apply styling rules to your HTML elements.

				
					<script>
document.getElementById("yourid").style.color = "green";
</script>

<script>
document.getElementsByClassName('yourclass')[0].style.color = "red";
</script>

<button type="button"
onclick="document.getElementById('yourid').style.color = 'orange'">
Click Me!</button>
				
			

Changing font colour by getElementById: YourID

Changing font colour by getElementByClassName: YourClass

document.getElementById("yourid").style.color = "green"; document.getElementsByClassName('yourclass')[0].style.color = "red";
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Contact Us Today