Loading...

HTML essentialsGetting Started

Linking CSS & JavaScript

5 min lesson

Next
  • HTML gives structure, CSS gives style and JavaScript adds behavior; you connect them with links in the HTML.
  • A <link> tag in the head loads an external CSS file, for example <link rel="stylesheet" href="style.css">.
  • A <script> tag loads JavaScript; placing it at the end of body lets the HTML load first.
  • The href or src attribute is the path to the file, relative to the HTML file.
  • Keeping CSS and JavaScript in separate files keeps your HTML clean and reusable.
  • This builds on Document structure & meta tags, where you learned the head, and completes the Getting Started group.
  • The example links a CSS file that colors the heading and a script that shows a message; you will see a colored heading and an alert.
Keep in mind
  • Link CSS in the head and put scripts at the end of body.
  • Use relative paths that match your folder structure exactly.
  • One file, one job: HTML for structure, CSS for style, JS for behavior.
  • If a style or script does not load, check the file name and path first.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Linked files</title>
  <!-- Load the external stylesheet -->
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <!-- This heading gets its color from style.css -->
  <h1>Styled by CSS</h1>
  <!-- One short paragraph of visible text -->
  <p>This page links CSS and JavaScript.</p>
  <!-- Load the script after the HTML so elements exist -->
  <script src="app.js"></script>
</body>
</html>

What happens: A browser tab titled "Linked files", a heading "Styled by CSS" shown in the color set by style.css, the paragraph "This page links CSS and JavaScript.", and an alert box from app.js.

Breaking it down

link tag

<link rel="stylesheet" href="style.css">

Tells the browser to load style.css and apply its rules. Without it, the page appears with default browser styles.

script tag

<script src="app.js"></script>

Loads and runs app.js. Placed at the end of body, the HTML is ready before the script runs.

relative path

href="style.css"

Points to a file in the same folder as the HTML. A wrong path means the file is not found and nothing loads.

A helpful analogy

Think of a theater: HTML is the stage and actors, CSS is the lighting and costumes, and JavaScript is the director who makes things happen. The link and script tags are the cues that bring the lighting and director in.

Nesting rules

  • 1.Put link tags inside head and script tags just before </body>.
  • 2.Use one CSS file and one JavaScript file per page until you need more.

Common mistakes

Wrong file path in href or src

Avoid

<link rel="stylesheet" href="css/style.css"> when the file is in the same folder

Use instead

<link rel="stylesheet" href="style.css">

The browser looks for the file at the path you give. A wrong path returns a 404 and the style or script never loads.

Putting the script in the head without defer

Avoid

<script src="app.js"></script> in head

Use instead

<script src="app.js"></script> before </body>

In the head, the script runs before the page elements exist, so it cannot find them. At the end of body, the elements are ready.

Quick tips

  • •Open the Network panel in DevTools to confirm style.css and app.js loaded with status 200.
  • •Change one color in style.css and refresh; seeing the change proves the link works.
Previous
Next