Where to put CSS style rules


There are three different places you can place your style rules:

In a separate file

A stylesheet is just a text file containing CSS style declarations. The standard convention is to give stylesheets the .css extension.

Somewhere in the <head> section of your page, create this <link ... /> :

<head>
<title>How to link stylesheets</title>
 
 
<link rel="stylesheet"
      href="mystyle.css"
      type="text/css" />
 
</head>

The href attribute obeys all the conventions about absolute and relative URLs that the <a href="..."> does.

The 'stylesheet' at mystyle.css is a text file which contains just style rules (no comments are necessary to hide it from older browsers.), for example:

body {
  background-color: #bbbbbb;
  font-family: Geneva, Arial, Helvetica, sans-serif;
}
 
/* Here's how to make a comment in a style file */
 

Google "@import CSS" for another way of linking to external stylesheets, that only newer browsers support.

In Dreamweaver

When you create a new style, DW will ask if you want to create it in an external stylesheet.

You can "File | Open" your stylesheet (.css file) and edit it as you would with a text editor.

Style rules in the <head> section affect only one page at a time. It is common to format them with comments so as to hide them from older, CSS-unaware browsers:
<head>
<title>Style Example</title>
<style type="text/css">
<!--
p,ul {
  font-size: 1.5em;
  line-height: 2.3em;
}
-->
</style>
</head>

Local or in-line styles

Local styles let you apply CSS rules at just one place in one page.

This is usually a bad idea:

Before you use a local style for anything but first-time experimenting, you should ask yourself first:

But, it sure is a useful trouble-shooting technique to style stuff locally.

You can use the style attribute in just about any HTML tag, for example:
<p style="text-indent: 2em; color: red;">This paragraph should have the
first line indented, and all the text should be red.</p>
Or to apply the same styles to more than one tag, try:
<div style="margin-left: 5em">
<p> A division "tabbed" over to the right...</p>
<ul>
  <li>Will affect <b>all</b> the tags within the division,</li>
  <li>More...</li>
</ul>
 
 
</div>
A division is always a square box. If you want to affect just some text, you can use the <span> tag instead.:
<p>I'd like <span style="color: green">just some
of this text to be green</span>, not all of it.</p>

See also:

http://htmlhelp.com/reference/css/style-html.html

http://www.w3.org/TR/REC-CSS2/cascade.html#at-import