Learn

Perfect! We successfully separated structure (HTML) from styling (CSS), but the web page still looks bland. Why?

When HTML and CSS codes are in separate files, the files must be linked. Otherwise, the HTML file won’t be able to locate the CSS code, and the styling will not be applied.

You can use the <link> element to link HTML and CSS files together. The <link> element must be placed within the head of the HTML file. It is a self-closing tag and requires the following attributes:

  1. href — like the anchor element, the value of this attribute must be the address, or path, to the CSS file.
  2. rel — this attribute describes the relationship between the HTML file and the CSS file. Because you are linking to a stylesheet, the value should be set to stylesheet.

When linking an HTML file and a CSS file together, the <link> element will look like the following:

<link href='https://www.codecademy.com/stylesheets/style.css' rel='stylesheet'>

Note that in the example above, the path to the stylesheet is a URL:

https://www.codecademy.com/stylesheets/style.css

Specifying the path to the stylesheet using a URL is one way of linking a stylesheet.

If the CSS file is stored in the same directory as your HTML file, then you can specify a relative path instead of a URL, like so:

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

Using a relative path is very common way of linking a stylesheet.

Instructions

1.

Let’s link the stylesheet style.css to the HTML file index.html.

First, add a <link> element within the <head> section.

2.

Next, add the href attribute to the <link> element and set it equal to style.css.

Take a look at the web page in the browser to the right. Do you notice any changes yet?

3.

Finally, add the rel attribute and set it to the correct value. Keep an eye on the first paragraph’s font—it should appear different from the destinations’ descriptions when your stylesheet is linked correctly.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?