How To Create Your First CSS File

Today we are going to write and save our first CSS file. Let’s begin by opening a text editing program. If you are on a Microsoft Windows PC open the program named Notepad (hold down the Windows Key on your keyboard and press R, then type notepad and press enter). If you are using a Macintosh computer, launch the application named “TextEdit” (which can be found in your Apps folder).

Let’s Write Our First Bit of CSS

Let’s imagine we have a simple web page with a heading, and we want the heading to be orange and center aligned. Add the following code into your new blank text document:

h1 {
color: orange;
text-align: center;
}

Hopefully, you remember this code from our previous lesson. The task for today is to save our CSS file and link it to an HTML page.

Step 1: Saving The CSS File

Create a new folder on your desktop (or another location you prefer) and name it CSS-TestNow, back in your text editing program save your document as “style.css”.

save css file

Linking CSS File to an HTML Page

Our new CSS file is worthless if we don’t apply it to a web page. Let’s create a quick HTML page for this lesson. Create a new blank file in Notepad (or TextEdit) and add the following code:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>CSS-Test</title>
</head>
<body>

<h1>CSS-Test</h1>

<div id="box-one">
<p>This is box one.</p>
</div>

<div id="box-two">
<p>This is box two.</p>
</div>

</body>
</html>

If you’ve read my first few HTML Lessons, then this code is at least somewhat familiar. I’ll explain it as the lesson continues; for now save this document in our “CSS-Test” folder and name it “index.htm”.

save xhtml file

Linking the Two Files Together

We still need to tell the web browser to load our “style.css” file when the “index.htm” page is viewed. Add the following code to “index.htm” directly above our </head> closing tag:

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

This line of code tells our browser that we want to link a Style Sheet, that it’s located in the same folder as our HTML page, and that it’s named “style.css”.

Now, when you view “index.htm” page in a web browser you should see a centered, orange heading:

css test 1

Let’s Style Those Two Boxes

If you look at the code of our HTML page, you’ll see two <div> elements. We added an HTML attribute of “id” for these two elements and assigned them values of “box-one” and “box-two.” We can use an element’s “id” to select and style it with CSS. For example, let’s make the first box gray, and the second box yellow. Add the following code to your CSS file, directly below our original <h1> rule:

#box-one {
background-color: gray;
}

#box-two {
background-color: yellow;
padding: 10px;
}

When an element has an “id” we can access it with a CSS selector by placing a pound sign (#) in front of it’s id value. So to select the first <div> element we simply type “#box-one” and then begin our CSS Rule.

Our New Fancy Boxes

When you save your CSS file and refresh our HTML page in a web browser you should see something very similar to this:

css test 2

Yay For Style

It may not be beautiful, but we styled our first HTML page with CSS! Let’s recap your CSS knowledge so far. You know:

  • The basic syntax of CSS (covered in our previous lesson)
  • How to link a CSS file to an HTML page
  • How to select certain HTML elements and style them

In our next lesson we’ll continue learning about CSS Selectors and the different ways to target specific elements with your CSS.

Leave a Reply