Skip to main content

What does the <link> tag do? How is CSS linked?

The <link> tag is used to connect an HTML document to external files. It is most often used to link CSS stylesheets, which define the page's styling: colors, fonts, spacing, background, animations, and so on.


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

Breakdown:

  • rel="stylesheet": specifies the link type: this is a stylesheet;
  • href="style.css": the path to the CSS file (can be relative or absolute);
  • <link>: a void tag (it has no closing tag).

2. Where it is placed

The <link> tag is always placed inside the <head> block, so the browser loads the styles before rendering the page:

html
<head> <meta charset="UTF-8"> <title>My site</title> <link rel="stylesheet" href="style.css"> </head>

3. Example of linking CSS

External file:

html
<link rel="stylesheet" href="styles/main.css">

This approach is preferred:

  • the styling code is separated from HTML,
  • the CSS file can be reused across multiple pages,
  • the page is cached faster by the browser.

Absolute path:

html
<link rel="stylesheet" href="https://example.com/css/main.css">

Used when linking external libraries (for example, Bootstrap).

Multiple files:

html
<link rel="stylesheet" href="reset.css"> <link rel="stylesheet" href="main.css"> <link rel="stylesheet" href="media.css">

Order matters: the last file can override the previous ones.


AttributePurposeExample
relDefines the link typerel="stylesheet"
hrefPath to the filehref="style.css"
typeMIME type (optional)type="text/css"
mediaWhich devices/screens to apply it tomedia="screen and (max-width: 768px)"
asFor preloading (rel="preload")as="style"

5. How linking CSS works

  1. The browser loads the HTML.
  2. It reaches <head>.
  3. It sees <link rel="stylesheet" href="style.css">.
  4. It loads the CSS file and applies the styles to the DOM.
  5. After that, it renders the page with the styling.

Summary:

PropertyDescription
Tag<link>
TypeVoid
Main purposeLinking external resources (primarily CSS)
LocationInside <head>
Key attributesrel, href, media
ImportanceCritical for styling and structure

In simple terms: <link> tells the browser:

"Take this file and use it to style the page."

And CSS is linked exactly through it, so the page looks not like bare HTML, but like a fully styled site.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

What does the <link> tag do? How is CSS linked?: HTML Interview Question