CSS Syntax

Ways to Apply CSS to HTML

Updated in 8th of June 2025

Programming or Coding

Ways to Apply CSS to HTML


Alright, so we know what CSS is and why it`s awesome for making websites look good. Now, let`s talk about "Where does the CSS code actually go in relation to your HTML?"

Think of your HTML as a person, and CSS as their outfit. There are a few ways to get the clothes onto the person:


1. External Stylesheets (The Best Way)

This is like putting all your outfits in a separate, dedicated wardrobe.

  • How it works: You create a completely separate file, let`s say styles.css. This file only contains your CSS rules.
  • Connecting them: In your HTML file, inside the <head> section, you add a special line to "link" to that wardrobe:


  • Why it`s the best:
    • Super Clean: Your HTML stays neat, focused only on content. Your CSS stays neat, focused only on styling.
    • Reuse, Reuse, Reuse!: You can link the same styles.css file to hundreds of different HTML pages. Change one thing in styles.css, and it instantly updates across your entire website! Imagine changing your brand`s color in one spot and it updates everywhere – that`s the power!

Quick Exercise:

1. Create a file named index.html



2. Create a file named main.css (in the same folder as index.html):



Now, open index.html in your browser. See how the h1 is blue and the p is green and bigger? That`s your external stylesheet in action!


2. Internal Stylesheets (Good for Single Pages)

This is like putting an outfit inside a specific box on a shelf, labeled for just that one occasion.

  • How it works: You put your CSS code directly inside a <style> tag, which lives in the <head> section of your HTML file.


  • When to use it: This is handy if you have a single HTML page that needs some unique styles that won`t be used anywhere else on your site. It keeps everything for that one page in one file.



3. Inline Styles (Use with Caution!)

This is like sewing a specific color directly onto one single piece of clothing.

  • How it works: You add the style attribute directly to an HTML tag, and put your CSS rules right there.


  • Why it`s generally discouraged:
    • Messy HTML: Your HTML gets cluttered with styling rules, which defeats the purpose of separating concerns.
    • No Reuse: If you want another red, bold paragraph, you have to copy and paste the same style attribute again.
    • Hard to Maintain: Want to change all your red bold paragraphs to green italic? You`d have to find every single one and edit it manually! Yikes!
  • When it`s okay: Mostly for quick tests, or if you have a very, very specific situation where a style truly only applies to one element and will never be reused (which is rare).

The big takeaway: For almost all your web projects, aim to use External Stylesheets. It`s the cleanest, most efficient, and easiest way to manage your website`s look.

RETURN TO FIRST PAGE

OTHER CONTENTS