style css in js code example

Example 1: add style javascript

// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";

Example 2: html css js

<!DOCTYPE html>
<html lang="pt-br" xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link rel="stylesheet" type="text/css" href="style.css">
	<title>...</title>
</head>
<body>
	...
<script type="text/javascript" src="script.js"></script>
</body>
</html>

Example 3: style through javascript

element.style.backgroundColor = "red";   // set the background color of an element to red

Tags:

Html Example