Skip to content

01. Styling Basics Including JavaScript

HTML provides the structure, CSS handles styling, and JavaScript adds interactivity. Understanding how these three technologies work together is fundamental to web development. While HTML can include basic styling and JavaScript, best practices involve separating concerns and using each technology for its intended purpose.

HTML provides the foundation:

<div class="container">
<h1>Title</h1>
<p>Content</p>
</div>

CSS styles the HTML:

<style>
.container {
background-color: #f0f0f0;
padding: 20px;
}
h1 {
color: #333;
}
</style>

JavaScript adds behavior:

<script>
document.querySelector('.container').addEventListener('click', function() {
this.style.backgroundColor = '#ccc';
});
</script>
<div style="color: red; font-size: 18px;">Styled content</div>
<style>
.class { color: red; }
</style>
<link rel="stylesheet" href="styles.css">
<script>
// JavaScript code
</script>
<script src="script.js"></script>
  • HTML: Structure and content
  • CSS: Visual presentation
  • JavaScript: Behavior and interactivity

Prefer external files:

<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>