Setting Up Your First External CSS File

In a recent post, CSS – Back To Basics, I discussed the purpose of CSS and how to use tags, classes, and IDs to create style definitions. This time around, I’ll chat about setting up an external CSS file and giving it some basic definitions that you should always include in your main CSS file.

So first, let’s explain how to make an external CSS file. The purpose in doing this is to keep all your CSS in a separate place from your HTML. But why is that important? Well, for one, it will speed up the load time of your page for your users. Probably infinitely more important is that it is much more likely that most of the CSS you write for one of your documents will apply to another one. Why repeat it in the head of every page if you can just write it once? That way, when you need to make changes, it’ll be much easier.

Alright, so here is how you make a reference to an external CSS file. In your head section, put the following line of code:

<link href="main.css" rel="stylesheet" type="text/css" />

You can replace main.css with whatever you want to name your CSS document. Main is a good idea if you plan on having multiple CSS files (for large sites). For smaller sites, you probably only need one (for example, my website, www.davidjrush.com, has only one external CSS file (actually, technically it has two, one for the blog, and one for the rest).

You can make your new CSS file (for now, we’ll just assume you are calling it main.css) in a text writer. Change the file extension from .txt to .css just like you would change .txt to .html for an HTML file. Inside the document, you don’t need any tags, in fact, you shouldn’t have any! All you should write in this document is the style that you would have put in the head (but without the <style> tag).

Now I’ll discuss some basics of what you should have in every main CSS file you ever write for a website. There are somewhere around 80 tags for HTML, but only about 30 of them are commonly used. Of these 30 you should think about defining these basic tags: body, div, img, table, p, ul, a, and a:hover. The reasoning behind this is safety safety safety! Different browsers have different default settings for some of these tags. So the smart move is to give them all uniform settings from the start.

You may want to start your main CSS document with the following:

/*--this is a comment, you can put anything inside the star and slash--*/
body{
   margin: 0px;
   padding: 0px;
}
div{
   margin: 0px;
   padding: 0px;
}
img{
   border: 0px;
   margin: 0px;
   padding: 0px;
}
table{
   margin: 0px;
   padding: 0px;
}
p{
   margin: 15px;
   padding: 0px;
}
ul{
   margin-top: 10px;
   margin-bottom: 10px;
   margin-left: 35px;
   padding: 0px;
   list-style-image: url('image location');
   line-height: 20px; (gives spacing between list items)
}
a{
   text-decoration: underline/none;
   color: hexcode/word;
   font-weight: bold/normal;
}
a:hover{
   text-decoration: underline/none;
   color: hexcode/word;
   font-weight: bold/normal;
   /*make sure to change at least one thing from your a tag definition*/
}
a:visited{
   text-decoration: underline/none;
   color: hexcode/word;
   font-weight: bold/normal;
   /*make sure to change at least one thing from your a tag definition*/
}

Anything in red is just a comment to help you out. The first several definitions really just look at margin and padding. This is where cross browser compatibility comes into play. For example, Internet Explorer and Firefox have different default settings for margin on p tags. This will cause pages to render differently. It always seems like a safe bet to define your margin and padding for most anything. In an upcoming post, I’ll discuss defining your copy/text (which will include more specific information on p tags, a tags, and other similar issues.

You’ll notice in the links section, there is a definition for an a tag, an a tag being hovered on by the mouse, and an a tag that has been visited by the user. The latter two are called “pseudo-classes”. There are others, and they can be applied to tags other than the a tag, just so you know.

If you have any questions about this starter file, post a comment, or send me an email. Stay tuned for further CSS related posts.

2 Responses to “Setting Up Your First External CSS File”

  1. Dave Swart says:

    David, Love your site and explanations of css, I’m ready to take the plunge and add some css to my site. I just want to start with something simple like adding an icon in front of pdf links. Something like

    a[href$=’.pdf’] {
    background: transparent url(/images/content/icon_pdf.gif) center left no-repeat;
    padding-left: 18px;
    }

    However being a newbie I don’t know where exactly this code should go I’ve read many articles and I know it should go in the header. I have a test site that I try things out on and haven’t been able to get it to work I am using drupal 7 for my website.
    Thanks for any thoughts on this.

    • David says:

      Anywhere in the head is fine. If you want to put your CSS in an external file, just use the line in the head of the document as explained in this article.