Skip to main content

Introduction to CSS

If HTML is the skeleton of a house, CSS (Cascading Style Sheets) is the paint on the walls, the style of the furniture, and the layout of the rooms.

Without CSS, every website would look like a boring plain-text document. With CSS, we can make websites look professional, modern, and beautiful.

1. How CSS Works

CSS works by "selecting" an HTML element and telling it how to look.

Think of it like giving a command: "Hey Browser, find every Paragraph, and make the Text Color Blue."

The 3 Parts of a CSS Rule:

  1. Selector: Which HTML tag are we talking to? (e.g., h1, p, button).
  2. Property: What do we want to change? (e.g., color, font-size, background).
  3. Value: What is the new setting? (e.g., red, 20px, blue).

2. Three Ways to Add CSS

There are three ways to link your styles to your HTML. As you grow as a developer, you will almost always use the External way.

You create a separate file named style.css. This keeps your code clean and organized.


In your HTML:

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

3. The "Cascading" Secret

Why is it called Cascading Style Sheets? Because CSS follows a hierarchy (like a waterfall).

If you tell a paragraph to be Red at the top of your file, but then tell it to be Blue at the bottom, the browser will choose Blue. The last rule usually wins!

4. Your First Styles

Here are the most common "paints" you will use first:

PropertyWhat it doesExample
colorChanges text colorcolor: green;
background-colorChanges the box colorbackground-color: black;
font-sizeMakes text bigger/smallerfont-size: 16px;
text-alignMoves text (left/center/right)text-align: center;

Let's Try It!

  1. Open your index.html from the HTML track.
  2. In the <head>, add this line: <link rel="stylesheet" href="style.css">
  3. Create a new file in the same folder called style.css.
  4. Type this in your CSS file:
style.css
body {
background-color: #f4f4f4;
font-family: sans-serif;
}

h1 {
color: darkblue;
text-align: center;
}
  1. Refresh your browser and see the transformation!
Why learn CSS?

A website with great content but poor design is rarely trusted. CSS allows you to build trust with your users by making your work look professional and easy to read.