CSS Rules

Part 1: Learning the Rules

It's all in the details, and rules are important. CSS is the acronym for Cascading Style Sheets. CSS is an extension to basic HTML that allows you to "style" how the content within your web pages will look. With CSS you can set up rules (or definitions) to tell specific HTML tags how to display content, or you can create rules and apply them to tags when you need them. Each browser has a set of parameters for how it reads paragraphs, headings, tables, etc., and then renders it on your screen. Each of the tags can be defined so that you can tell the browser how to display it. With this in mind, you can think of your CSS file as a sort of dictionary with a clear definition of how each item is displayed on the screen.

HTML Selector

All HTML tags have default properties. With CSS you can define any HTML tag to change the defaults.

css
  1. p {
  2. line-height: 14px;
  3. }

You can define a style for multiple tags at one time by separating each html tag name with a comma.

css
  1. h1, h2, h3 {
  2. color: #c00000;
  3. }

Class

A class is a rule that can be applied to an HTML tag. You can give a class any name you want with the exception of a few reserved words.

css
  1. .myclass {
  2. font: bold 14px Verdana;
  3. }

Classes have an advantage in that you can create a number of different classes and apply them to different paragraphs, headings, divs, etc.

ID

ID rules are similar to a class, but they can only be applied once on a page to a particular HTML tag. IDs are commonly used in CSS positioned layouts since items like a header or footer are only going to appear once on a page. An ID rule looks like the following:

css
  1. #myid {
  2.  background-color: #ffffff;
  3.  height: 40px;
  4.  padding: 10px 20px;
  5. }

Part 2: Parts of a CSS Rule

Each of the rule examples above (html selector, class, and ID), consist of three distinct parts. The first part of the rule is the type of selector whether it be an html selector, a class, or an ID. The selector is followed by a space and then a beginning curly brace. Within the curly brace will be the property. It is followed by a colon.

After the property will come the value of that property. Note that you can have more than one property and value in a rule. The value is followed by a semicolon. To end the rule, place the closing curly brace. You cannot have a property without a value nor a value without a property. Below is the syntax for a CSS rule:

css
  1. selector {
  2. property: value;
  3. property: value;
  4. }

Part 3: Where you can place a CSS rule

You can also choose where to place your CSS rules. You can place them in an external style sheet (preferred), as an embedded style, or as an inline style. Note that classes and IDs cannot be applied or placed in an inline style.

Inline Rules

An inline rule is a style that is attached to one specific tag. There may be times when you need a rule that you don't plan to use anywhere else. The syntax for an inline style rule is different from that of embedded or external styles:

HTML & css
  1. <h1 style="color: #c00000"> ... </h1>

Inline styles have both advantages and disadvantages. One advantage would be if you want to override an already defined style. A disadvantage is having a lot of inline styles adding bulk to your HTML coding.

Embedded Rules

An embedded rule is a style that is attached to one specific web page. Embedded styles are placed within the <head> section of the HTML page and the rules affect the entire page. An embedded rule would look like the following:

html & css
  1. <style type="text/css">
  2. .style1 {
  3. text-align: center;
  4. font: "Times New Roman";
  5. color: #800000;
  6. }
  7. </style>

External CSS File

An external style sheet is the file containing CSS information that can be linked with any number of pages. This is a very convenient way of formatting the entire site as well as restyling it by editing just one file.

External style sheets are text documents with a .css file extension. The syntax for linking to an external style sheet is:

html & css
  1. <link ref="stylesheet" type="text/css" href="style.css" />

As a Recap...

  1. We've learned that there are three types of CSS rules: HTML, Class, and ID
  2. We've learned there are three parts to a CSS rule: selector {property: value;}
  3. And we've learned there are three places to apply a CSS rule: Inline, Embedded, and External

Congratulations! Take a break and, when you are ready, move on to the next lesson.