Code has been added to clipboard!

How to Link CSS to HTML: CSS External Style Sheets Explained

Reading time 2 min
Published Jun 11, 2019
Updated Jan 21, 2020

TL;DR — CSS external stylesheet means that you upload all styling properties and values to a separate .css file. Then, you link the document to your website. Learn how to link CSS to HTML to boost website performance and to update CSS rules easily.

  • CSS external stylesheet is a .css file with all CSS rules.
  • You can link CSS to HTML by using the <link> element.
  • After learning how to link a CSS file to HTML, you can style multiple pages and separate style from content.

Styling Multiple HTML Pages

CSS external stylesheets refer to .css documents that contain CSS properties and values for the entire website.

Tip: .css files cannot contain HTML tags.

The following example shows how to link CSS to HTML by adding an external .css file into an HTML <head> section:

Example
<head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

Note: modifications and updates to the .css file will apply to the whole website. Linking HTML to CSS is the best option for easy website maintenance.

You have to use the <link> element to link HTML to CSS. The element itself is empty, but it contains three attributes:

  • rel describes the relationship between the HTML document and a linked document.
  • type determines what type of data should be taken by an input element.
  • href points to a specific file that you're uploading.

Note: you can create a .css file with any text editor. The document must have the .css extension.

The example below illustrates how a .css file from the first external CSS example looks like:

Example
h1 {
   color: red;
   margin-left: 20px;
}

p {
   color: blue;
}

Note: never leave a space between property value and unit. Write 10px, not 10 px.

  • CSS external stylesheet is the standard option for web design.
  • Knowing how to link a CSS file to HTML lets you optimize code and create websites with a consistent style.
  • Linking many external stylesheets to websites might increase website loading time.
  • Use inline CSS for styling a single element, and internal CSS for one page.
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index