H.dev

HTML Link & Script Tags

Hero

HTML Link & Script Tags Understand how to bring your website to life by connecting external CSS stylesheets for design and JavaScript files for interactivity. Learning Outcomes: Master the <link> and <script> tags to separate HTML structure from styling and logic. Prerequisites: Understanding of the basic HTML document structure. Reading Time: 4 min read Difficulty: Beginner

Introduction

HTML is just the skeleton of your website. To add skin, paint, and style, you need CSS. To add muscle, movement, and logic, you need JavaScript. But how do you actually attach these separate languages to your HTML file? This is exactly where the <link> and <script> tags come into play!

Why This Matters

For large projects like himubey.in, writing thousands of lines of CSS and JavaScript directly inside your HTML file would be an absolute nightmare to maintain. By using the <link> and <script> tags, you can keep your HTML clean and modular, loading external styling and logic files exactly when you need them.

Core Concepts

The <link> tag is a self-closing element used primarily to connect your HTML document to an external CSS stylesheet. It can also be used to link custom fonts (like Google Fonts) and favicons (the tiny icon in the browser tab). It almost always goes inside the <head> tag.

2. The <script> Tag

The <script> tag is a paired element used to embed or reference executable JavaScript code. Unlike the link tag, it is commonly placed at the very bottom of the <body> tag to ensure the HTML loads before the script runs.

Syntax

<head>
  <!-- Linking an external CSS file using the 'href' attribute -->
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>Hello World</h1>
  
  <!-- Linking an external JS file using the 'src' attribute -->
  <script src="app.js"></script>
</body>

Real World Example

Let's wire up the main layout file for Himanshu Dubey's portfolio. We will link a custom stylesheet, a Google Font, and a JavaScript file for a dark mode toggle.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Himubey | Developer Portfolio</title>
  
  <!-- 1. Linking a Favicon -->
  <link rel="icon" href="favicon.ico" type="image/x-icon">
  
  <!-- 2. Linking an external font (Google Fonts) -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap">
  
  <!-- 3. Linking our custom CSS -->
  <link rel="stylesheet" href="/css/main-style.css">
</head>
<body>
  
  <h1 class="font-inter">Welcome to my Portfolio</h1>
  <button id="darkModeBtn">Toggle Dark Mode</button>
  
  <!-- 4. Linking external JavaScript at the end of the body -->
  <script src="/js/theme-toggle.js"></script>
</body>
</html>

Best Practices

  • rel is Required: When using the <link> tag for CSS, you must include rel="stylesheet". This tells the browser what the relationship is between the HTML file and the file you are linking.
  • Place Scripts at the Bottom: Browsers read HTML from top to bottom. If you place a heavy JavaScript file in the <head>, it will pause the loading of your website until the script finishes downloading! Placing it just before the closing </body> tag ensures the user sees the website instantly.

Common Mistakes

Mistake: Confusing the attributes for links and scripts.

<!-- WRONG: Scripts use 'src', not 'href'! -->
<script href="app.js"></script>

<!-- WRONG: Links use 'href', not 'src'! -->
<link rel="stylesheet" src="style.css">

Correct Approach:

<!-- CORRECT -->
<script src="app.js"></script>
<link rel="stylesheet" href="style.css">

💡 Himubey Tip

In modern web development (especially when you start using frameworks like Next.js or React), you might see developers putting <script> tags in the <head> but adding the defer attribute (e.g., <script src="app.js" defer></script>). This tells the browser to download the script in the background but wait to execute it until the HTML is fully parsed. It's the best of both worlds!

Interview Questions

  1. Why is the <link> tag usually placed in the <head>, while the <script> tag is often placed at the end of the <body>?
  2. What attribute is used in the <link> tag to specify the path to a CSS file?
  3. What attribute is used in the <script> tag to specify the path to a JS file?

Mini Challenge

Task: Connect the pieces.

  1. Create three files: index.html, style.css, and logic.js.
  2. In your index.html, set up the basic boilerplate.
  3. Use the <link> tag in the head to connect your style.css.
  4. Use the <script> tag at the bottom of the body to connect your logic.js.

Summary

The <link> and <script> tags are the connective tissue of web development. They allow you to modularize your code by separating your HTML (structure) from your CSS (presentation) and JavaScript (behavior). Mastering how to link these resources efficiently is a crucial skill for any frontend developer.

Next Lesson

Next Lesson: HTML Video & Audio

Design & Developed by Himanshu Dubey © 2026