CSS Lesson 2: 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 XHTML page.
Step 1: Saving The CSS File
Create a new folder on your desktop (or another location you prefer) and name it CSS-Test. Now, back in your text editing program save your document as “style.css”.

Linking CSS File to an XHTML Page
Our new CSS file is worthless if we don’t apply it to a web page. Let’s create a quick XHTML page for this lesson. Create a new blank file in Notepad (or TextEdit) and add the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; 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 XHTML 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”.

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" type="text/css" media="screen, projection" />
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 XHTML 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:

Let’s Style Those Two Boxes
If you look at the code of our XHTML 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 XHTML page in a web browser you should see something very similar to this:

Yay For Style
It may not be beautiful, but we styled our first XHTML 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 XHTML 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.






I am currently going to school online at the Art Institute for Web Design/ Interactive Media. I have only been working with HTML and CSS code for a couple of months now, and your tutorials have really helped me so much man, I really just wanted to take this opportunity to thank you for posting such great tutorials, and making everything really easy to understand. Every sense I have found your Web site last week,I have been religiously referencing back to your site for help, and it has made my online experience with Coding a hell of a lot easier. (lol) I even go as far as leaving your site open in my Task Bar constantly for quick reference. Thanks you so much buddy, your work is very appreciated.- Norton
Hi there, I stumbled across your amazing Site! and is so glad I did.
Have a Question as I am currently in the process of doing my own website.
I just have 2problems that I hope you or anyone could help me with..
I want to remove my Avatar (profile picture) from my Twitter HTML Widget for my Website, how do I do this?
What part of the Code do I change? cant figure this out at all
heard its actually very Easy lol, but I am only familer with HTML.. or how do I Link a Css document to my webpage to do this?
ps: if you could take the time to see Lauren Conrads site which is
http://www.laurenconrad.com then scroll down to her Twitter feed on the Right hand side is Exactly how I want mine, which will Explain it to you better than I can lol,
Thank you so much for taking time to read Regards Jen