DEV Community

Cover image for How to connect CSS to HTML
Bronwen Gien
Bronwen Gien

Posted on • Originally published at worldofdev.info

How to connect CSS to HTML

Welcome to the wonderful world of coding! In this blog post, we’ll be discussing how to connect your CSS file to your HTML file. This is an important step in creating a web page, and if you’re just starting out, it’s a good idea to get comfortable with this process. So let’s get started!

As a beginner, I was intimidated to start with CSS as quickly as I did. I had only just started with HTML, and it felt like it was too soon to start my second language. But it’s never too early. The reality is that if you learn styling in HTML, the syntax is the same. The only difference is how you format the actual code, which is why you can learn two programming languages at the same time.

How to connect CSS to HTML is relatively simple. All you need to do is add a <link> tag in the head section of your HTML file and add the location of your CSS file with a rel attribute of “stylesheet”.

Table Of Contents

  1. Basic HTML Document Structure
  2. Ways to connect CSS to HTML
  3. How to connect CSS to HTML
  4. Why use CSS and not inline HTML styling
  5. Conclusion

Basic HTML Document Structure

If you know your Basic HTML Document Structure, you know that your document contains a <head> tag, and various metadata should be included in the <head> tag. One of the items to include in your head tag is the connection to your CSS file.

The first thing you need to do is include a <link> tag in your <head> tag.

Ways to connect CSS to HTML

There are a few ways to connect a CSS stylesheet to an HTML document. This article will show you the most common methods and explain how I suggest beginners connect CSS to HTML files.

The first way to connect CSS to HTML is by using the element, which is placed in the <head> section of your HTML document. This method is most commonly used when you want to load an external CSS file. This is the method that we will give code examples for below, as it is one of the easiest ways for beginners to connect CSS to HTML.

The second way to connect CSS to HTML is by using the <style>element, which is placed in the <head> section of your HTML document. This method is most commonly used when you want to include a small amount of CSS code in line with your HTML code. I don’t recommend this method as this is effectively mixing content with design. I have written a detailed article on the 6 reasons you shouldn’t style inline in your HTML that you can read if you are interested in understanding why this is not recommended in more detail

The third way to connect CSS to HTML is by using the <script> element, which is placed in the

or section of your HTML document. This method is most commonly used when you want to include a CSS file as a JavaScript resource.

Each of these methods has its advantages and disadvantages, so you’ll need to decide which one is best for your project.

As mentioned above, I recommend the first method for beginners who are learning how to connect CSS to HTML files.

Let us dive into the more technical elements and go through some code examples on how to connect a CSS stylesheet to HTML.

How to connect CSS to HTML

Learn how to connect your CSS in your HTML file with a code example by Clicking Here

Top comments (0)