how to apply css in javascript code example

Example 1: how to change style of an element using javascript

<html>
<body>

<p id="p2">Hello World!</p>

<script>
document.getElementById("p2").style.color = "blue";
</script>

<p>The paragraph above was changed by a script.</p>

</body>
</html>

Example 2: how to give css style in javascript

document.getElementById("myH1").style.color = "red";

Example 3: add css in javascript

document.getElementById("demo").style.display = "none";

Example 4: css in js

JSS is an authoring tool for CSS which allows you to use JavaScript to describe styles in a declarative, conflict-free and reusable way. It can compile in the browser, server-side or at build time in Node.

JSS is framework agnostic. It consists of multiple packages: the core, plugins, framework integrations and others.

Example 5: change style js

// select element from DOM
const el = document.querySelector('.para')
// change css style
el.style.color = 'purple'

Tags:

Css Example