Categories
html-css-tutorials

How to include CSS in HTML

Did you know “How to include css in html” is one of the most commonly asked HTML CSS interview questions during interview.
You need to be fundamentally very clear on the different ways to include css in html page

How to include css in html
how to include css in html

In this post I will show you the different ways on how to include css in html file and the difference between each method.

Prerequites:
You have a basic understanding of what is HTML and you also know what is CSS.
Assumption is you already have an html page where you want to include css.

Tools needed:
An HTML Editor. You can use any html editor installed on your system or you can use any of the online html css editors. 
A browser of your choice to see you output.

I am using the online editor glitch.com in google chrome browser so that this is the only thing I need.


Different ways on how to include CSS in HTML

Basically there are 3 different ways to include CSS in HTML

  1. By linking an external css file
  2. By writing internal css in html page
  3. By writing Inline CSS in html page

Let’s look at each of these methods one by one with practical implementation. 


How to include css in html: Link External CSS

Method 1 : How to link a CSS file to HTML

This is the ideal and the most preferred method. 
In this method you put all your styles in a different css file and load the styles by linking the css file into your html documents.

You can do this by either linking a files on your own system/server or your link from a different source.

Link a css file from your local system or server.

This css file can reside in your local project directory of your local system or on your own server.
The screen shot below is the source code of your html document.

How to include css in html - HTML Document
How to include css in html – HTML Document

The screen shot below is the source code of your style.css file

External CSS File
How to include css in html – External CSS File

Below is the code to link an external css file. Make the sure the css file is in the same directory.

All you need to do this is put this code in the <head> tag of your html document. Make sure you have a file named styles.css in your system

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

Can you see the text color of the heading “hi There!” is blue. 
This is because we are setting the css property of the text color of h1 element to #373fff in our style.css file.

h1{
 color:#373fff;
}

Link an external css from a different source

You can also link an external css from a different source.
Look at the screenshot below. In this example the code highlighted shows how to to link an external css from a bootstrap server. 

How to include css in html - Link External CSS
How to include css in html – Link External CSS from a different source

Did you noticed how the color of the heading has changed to ‘black’ color and the all the margin is gone.
This is because we are loading styles from the bootstrap.min.css.

Benefits of External CSS

While there are many benefits of this method, here are some of the most obvious ones:

  • You separate styles from markup
  • Developers/Engineers do not need to bother and work on your css files 

This method is same as the following methods:

  • how to connect css to html
  • how to connect html to css
  • how to attach css to html
  • how to reference css in html

How to include css in html: Internal CSS

Method 2 : How to add internal css in html page

In this method you write the styles in the head tag of the html page itself. 

Let’s take a look at the screenshot below:

Internal CSS
How to include css in html – Internal CSS

As you can see I added some styles for the h1 element within the style tag of the page itself.
Can you see how the text color of the heading changed to black.
This is because of the css styles written in the page.

What we learnt now that styles written in the style tag of the page will override the styles coming from external css.

Internal CSS takes precedence over External CSS rules

It over rides the styles written in the external css files

When to use Internal CSS

  • For a single page website
  • If you do not have access to external css file 
  • If you need to make some changes to reflect only on a single page

This method is same as the following methods:

  • how to insert css in html
  • how to apply css to html

How to include css in html: Inline CSS

Method 3 : How to write Inline css in html

In this method you write the styles in the element tag itself.
Let’s take a look at the screen below to see inline css in action.

How to include css in html - Inline CSS
How to include css in html – Inline CSS in html

Can you see the color of the header has changed to ‘red’. This is because we have added an inline style in the h1 element itself.

When to use inline style

  • When you want a small change to take place on a particular element and you have access to HTML file.
  • For quick fixes
  • When you do not have access to external css files
  • In emergency when you are not familiar with main external css file
  • Inline CSS are a standard practice for HTML emailers
  • Use inline css for a single page website where each element has different styles

Which styles takes precedence over what?

As you notice this document has 3 styles defined for the text color of the header:

  • Color ‘blue’ defined in the external css file
  • Color ‘black’ defined in the internal style tag
  • Color ‘red’ defined in the inline style.

And the final color of the header text is red. This is because:

Inline CSS takes precedence over Internal and External CSS rules.

Inline css over rides the styles written in the internal styles and external css files

Where to put css in html

Wondering where to put css in html?
It all depends on the methods you choose on how to include css in html.

  • If you are planning to use Method 1 ie. an external css, you obviously write your styles in the external css file and link the file in your html page to load the styles.
  • If you are planning to use method 2  ie. internal css method, you write the styles inside a style tag in the head element of your html page.
  • If you prefer inline css method than you write the styles in the element tag itself

How to import CSS in html

Importing a css is not very different from linking an external css.
You can import CSS in html using either of the 2 methods below

Import as a string

@import "navigation.css"; /* Using a string */

or Import as URL

@import url("navigation.css"); /* Using a url */

Know the Difference Between @import and <link>


Try yourself on how to include CSS in HTML

The best way to learn is to do by yourself. Below is the link to the sample code I have shared in this article. Feel free to play around.

https://glitch.com/~-how-to-include-css-in-html


Wrapping up

In this post we learnt the 3 different methods of how to include css in html.
I hope this post has answered your question and clarified all your doubts in regards to how to include CCS in html document.

Please share this post if you have found this helpful.

Feel free to leave feedback in the comment sections. Also let me know if you would like help on more such articles.

One reply on “How to include CSS in HTML”

Leave a Reply

Your email address will not be published. Required fields are marked *