DEV Community

Paul J. Williams
Paul J. Williams

Posted on • Updated on

Link to CSS and JavaScript in an HTML File

Link to CSS and JavaScript in an HTML File

The purpose of this tutorial is to teach you how to link to CSS and JavaScript files within an HTML file. It is possible to write CSS and JavaScript directly inside an HTML document, but it is generally best to keep these three languages in their own separate files.

Contents

  1. Directory and File Structure
  2. HTML
  3. CSS
  4. JavaScript

1. Directory and File Structure

It is a good idea to keep your HTML, CSS, and JavaScript files in separate directories. Create a directory for your project called css-and-js. Inside this directory, create three additional directories. Call them html, css, and javascript.

Inside your html directory, create a file called css-and-js.html. Inside your css directory, create a file called styles.css. And inside your javascript directory, create a file called script.js.

2. HTML

In order to link to your CSS and JavaScript files, you will need an HTML document within which to work. Open css-and-js.html and enter the following HTML:

<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='UTF-8'/>
    <title>Linking to CSS and JavaScript</title>
  </head>
  <body>
  </body>
</html>

Be sure to save your work any time you add code to your files. In the next two sections, we will go over what you need to add to your HTML document in order to link to your CSS and JavaScript.

3. CSS

First, you will need to add something in the body of your HTML to apply styling to. On the next line after the opening <body> tag, indent and add the following:

<h1>If this text is red, then you successfully linked your CSS file!</h1>

As it stands, this text will appear in the color defined as the root color in the browser’s default stylesheet – usually black. In order to change the color of the text, open your styles.css file and add:

h1 {  
  color: red;  
}

The final step in this section is to link to the CSS file inside your HTML document. Enter the following in the <head> section on the line after the closing </title> tag:

<link rel='stylesheet' href='../css/styles.css'>

The <link> element must be placed in the <head> section of the document. Notice that the <link> element is an empty element, so it does not need a closing tag.

The rel attribute defines the relationship between the resource and the HTML document. The href attribute points to your CSS file.

Since the file is located in another directory, you must specify the path by going up one directory using two dots (..), followed by a slash (/), the directory your CSS file is in (css), another slash, and then your CSS file name (styles.css): href=‘../css/styles.css’.

This is what your HTML document should look like so far:

<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='UTF-8'/>
    <title>Linking to CSS and JavaScript</title>
    <link rel='stylesheet' href='../css/styles.css'>
  </head>
  <body>
    <h1>If this text is red, then you successfully linked your CSS file!</h1>
  </body>
</html>

4. JavaScript

Next, you will need to add some code to your JavaScript file. Open script.js and add:

alert('You successfully linked your JavaScript file!');

Save your work and navigate back to your HTML document. The final step is to link to the JavaScript file inside your HTML document. Enter the following in the <body> section on the line after the <h1> element:

<script src='../javascript/script.js'></script>

It is considered the best practice to place the <script> element in the <body> section just before the closing </body> tag.

The src attribute points to your JavaScript file.

Since the JavaScript file is located in another directory, you must specify the path in the src attribute: src='../javascript/script.js'.

That's the last bit of code you will need to enter. This is what your HTML document should look like:

<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='UTF-8'/>
    <title>Linking to CSS and JavaScript</title>
    <link rel='stylesheet' href='../css/styles.css'>
  </head>
  <body>
    <h1>If this text is red, then you successfully linked your CSS file!</h1>
    <script src='../javascript/script.js'></script>
  </body>
</html>

Be sure to save your work in each of your three files. Now it's time to see if the links work. Open your css-and-js.html file in the browser of your choice. When you open the file in your browser, you should first encounter an alert box with the message you wrote in your JavaScript file. After clicking OK, the text you entered in the <body> of your HTML should appear red.

If the alert box does not appear or if the <body> text is not red, go back through the steps in this tutorial to ensure everything is entered exactly as shown here.

Congratulations! You've now linked to CSS and JavaScript files within an HTML document.

Discussion (0)