Three Ways to Add CSS in HTML Pages

Oct 20, 2022 Three Ways to Add CSS in HTML Pages

When we create a web page with lots of content and design, our code becomes long and it gets hard to find the source code of individual sections. So, it will be helpful if you split the HTML and CSS code into different files.

Also sometimes if our web page doesn’t have much content it will become extra work to add CSS into another file and import it. In that case, we can add CSS styles at the beginning of our page or if we don’t have to apply many CSS styles to HTML tags we can also add CSS code with HTML elements.

So, How many ways can we add CSS to HTML?

Three. internal, external, and inline style. This means importing CSS stylesheet files or adding CSS code at the beginning of the HTML or with HTML elements.

Let me show you these methods with examples.

If for some reason you don’t understand any part of this post you can read the completed code in my Github Repository.

How to Include CSS in HTML With Embedded or Internal Method

Both internal and external CSS methods are similar. Because they work within the <head> section. The only difference is your CSS code is on the same file or an external file.

So, for internal CSS we will write our CSS code within the <style> element like the code below.

<!DOCTYPE html>
<html>
  <head>
    <title>Three Ways CSS to HTML</title>
    <style>
      body {
        font-family: sans-serif;
      }
      .container {
        width: fit-content;
        margin: 200px auto;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>Three Methods to Add CSS into HTML</h1>

      <ul>
        <li>Internal CSS</li>
        <li>Inline CSS</li>
        <li>External CSS</li>
      </ul>
    </div>
  </body>
</html>

As you can see, we have applied some CSS styles using the internal CSS method.

In this example above, we changed the default font style of the <body> tag, and with the width: fit-content; declaration, we removed the extra whitespace. Because by default an element takes the whole width of the content area. Also with the margin: 200px auto; declaration, we set the top and bottom margin to 200px and left and right margin to auto which made the element center horizontally.

How to Add Inline CSS to HTML

Instead of adding CSS styles to selectors from the <style> tag, you can add your code directly with your HTML tags.

This method has some benefits. You don’t have to call selectors(i.e., h1, p) to change the design of an element. Also, the CSS code will be added with an HTML tag so you can tell by looking at the element that these are the changes I applied for this element.

To use Inline CSS, you have to use the style attribute and add the code inside the quotation mark like the class attribute.

Now, what is an HTML attribute?

HTML attributes provide additional information about elements. For example, you want to give a unique name to your HTML elements, you use class or id attributes.

Check out the changed body tag with the previous example below.

<!DOCTYPE html>
<html>
  <head>
    ....
  </head>
  <body style="font-size: large">
    <div class="container">....</div>
  </body>
</html>

We used the font-size property to change the default font size and added value large which means we set the font size to large from normal.

If you want to add multiple declarations just separate them with a semicolon like normal CSS.

How to Use External CSS Stylesheet in HTML

The meaning of the headline is basically how to put your CSS code in an external file and import that file into your HTML to design your web page.

But, some questions emerge here: How can the HTML know that file contains CSS code? and Where is the CSS file located?

The answer is HTML <link> tag. It defines relationships between your current document and an external resource. This tag is most commonly used to import stylesheets. It has some attributes to use in different cases like href, rel, etc.

Back to the questions, How can we import the CSS file? We will use the location of the CSS file with the href attribute. How can we know that we are importing a CSS file? We will use the rel attribute to tell that we are importing stylesheets.

The href(Hypertext REFerence) attribute defines the location or URL of the external resource and the rel attribute defines the relationship between the document and an external file.

Let’s create a file called styles.css file in the same directory you created your HTML file. Remember to put .css at the end of your file name.

Now, move the CSS code from the style element of the <head> section to your styles.css and save it. Right now, there should not be any CSS code on your head section because we moved those to our external file. So, you can also delete the <style></style> tags.

Finally, import the CSS file like the code below. Remember your file name should be correct.

<!DOCTYPE html>
<html>
  <head>
    <title>Three Ways CSS to HTML</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body style="font-size: large">
    <div class="container">
      <h1>Three Methods to Add CSS into HTML</h1>

      <ul>
        <li>Internal CSS</li>
        <li>Inline CSS</li>
        <li>External CSS</li>
      </ul>
    </div>
  </body>
</html>

Now, refresh your browser to see if your CSS styles are still working or not.

Let’s also add some new styles to test our final method.

body {
  font-family: sans-serif;
  background-color: salmon;
  color: white;
}
.container {
  width: fit-content;
  margin: 200px auto;
}

We changed the background color of the <body> tag to steelblue and font color to white with the color property.

External css method

Yay, it’s working. Again if you got stuck following any part of this post visit the Github repository mentioned at the beginning.

From these three methods, the external method is the most popular one. Because it keeps your code clean and well structured.

You can also put your CSS file into a folder. These will be helpful when you will have multiple CSS files. And as for the href attribute on <link> tag, you need to specify your file location with your folder name like ./yourfoldername/styles.css.

I think this is enough for you to learn about including or applying CSS styles to your web page. It’s time to learn about more HTML tags and CSS properties which will make your web page stunning.

Good Luck!