i2tutorials

Javascript DOM- CSS

Javascript DOM- CSS

 

The HTML DOM provides JavaScript to change the style (css)of HTML elements.

 

Changing HTML Style:

 

To change the style(css) of an HTML element.

 

syntax:

 

document.getElementById(id).style.property = new style

 

Example:

 

<!DOCTYPE html>
<html>
<body>

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

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

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

</body>
</html>

 

OUTPUT:

 

Javascript DOM- CSS

 

Using Events:

 

Events are produced by the browser when “things happen” to HTML elements:

 

 

Example:

 

<!DOCTYPE html>
<html>
<body>

<h1 id="id1">My Heading 1</h1>

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

</body>
</html>

 

OUTPUT:

 

 

Exit mobile version