How to useJavaScript in HTML
Define a JavaScript function called changeColor that is used to change the color of an HTML element with the id “myElement”. Here we used <script> tagto add javascript code in HTML.
Syntax
<script>
<!-- javascript body -->
</script>
Example: Illustration of adding color in HTML without CSS Using JavaScript
HTML
<!DOCTYPE html> < html > < head > < title >JavaScript Example</ title > < script > function changeColor() { var element = document.getElementById("myElement"); element.style.color = "green"; } </ script > </ head > < body onload = "changeColor()" > < h1 id = "myElement" >Using javascript to change color of this text</ h1 > </ body > </ html > |
Output:
How to add color in HTML without CSS ?
In HTML, adding color to text and elements is typically done using CSS. However, there are several approaches to add color directly in HTML, without using CSS.
We will discuss the following approaches to add color without CSS in HTML Elements.
Table of Content
- Using Font Tag
- Using JavaScript
- Using SVG tag with fill attribute
- Using Text Color Attribute
- Using the bgcolor Attribute