spacer

Embedded Style Sheets

 
spacer
Using embedded style sheets will allow you to begin unleashing the full power of CSS by enabling you to apply styles to all HTML elements of a particular type on an entire web page. Whereas an inline style will only allow you to address one HTML element at a time, an embedded style sheet will allow you to address multiple HTML elements at once. This is accomplished by using the style element and a list of CSS rule sets.


The style Element


The style element requires both start and end tags <style>...</style> which are inserted between the <head>...</head> tags (a.k.a., document head) of your web page. The type attribute defines the type of style sheet being used so we'll put text/css as the value. The entire contents of the style element should be wrapped in HTML comment tags to hide it from browsers not compatible with CSS.

Here's an example of an essential web page structure with the style element properly inserted in the document head:


Example 1 - THE style ELEMENT DISPLAYED IN THE DOCUMENT HEAD

<html>

<head>
<title></title>

<style type="text/css">
<!--
...CSS rule sets go here...
-->
</style>


</head>

<body>
...web page content...
</body>

</html>




CSS Rule Sets


Now all we have to do is define some CSS rule sets. These will associate a list of CSS properties with certain HTML elements on our web page. Here's the syntax you use to create a CSS rule set:

SELECTOR {property: value; property: value; property: value;}

In the above:
  1. SELECTOR refers to the targetted HTML element(s). You can simply insert the actual name of the HTML element you wish to target (known as a type selector) or you can be more specific by using class or id selectors.
  2. property refers to a CSS property.
  3. value refers to an associated CSS value.

Here's a few things to remember about CSS syntax in embedded style sheets:
  1. The list of property/value pairs (a.k.a., style declarations) are enclosed in a set of curly braces { }
  2. The property and its associated value are separated by a colon (:)
  3. Each property/value pair ends with a semi-colon (;)
  4. When using multiple word values in embedded style sheets, the multiple word string must be enclosed in either single or double quotation marks.

Here's an example of a web page document with a simple CSS rule set using a type selector. This will apply the color green to the text contained in all paragraph tags on the web page:


Example 2 - A CSS RULE SET TARGETS ALL PARAGRAPHS ON THE SAME PAGE

<html>

<head>
<title></title>

<style type="text/css">
<!--

p {color: green;}
-->
</style>


</head>

<body>

<p>...This text will be green...</p>
<p>...This text will be green...</p>
<p>...This text will be green...</p>

</body>

</html>




You may insert as many CSS rule sets as you like in the style element. When listing multiple CSS property/value pairs, many web authors (myself included) prefer to stack them one on top of the other and then indent the list to make it easier to read. This is perfectly fine since CSS code —like HTML— is designed to ignore extraneous white space.

Here's an example of this kind of formatting using multiple rule sets which themselves contain multiple property/value pairs:


Example 3 - EMBEDDED STYLE SHEET CSS RULE SETS (FORMATTED FOR EASY READING)

<style type="text/css">
<!--
H1 {
   font-family: 'Times New Roman';
   font-size: 36px;
   background: #ffffff;
   color: maroon;
}

H2 {
   font-family: arial,verdana,sans-serif;
   font-size: 20px;
   background: #ffffff;
   color: black;
}

P {
   font-family: arial,verdana,sans-serif;
   font-size: 16px;
   background: #ffffff;
   color: navy;
}
-->
</style>



Example 3 - EMBEDDED STYLE SHEET CSS RULE SETS (UNFORMATTED)

<style type="text/css">
<!--
H1 {font-family: 'Times New Roman'; font-size: 36px; background: #ffffff; color: maroon;}

H2 {font-family: arial,verdana,sans-serif; font-size: 20px; background: #ffffff; color: black;}

P {font-family: arial,verdana,sans-serif; font-size: 16px; background: #ffffff; color: navy;}
-->
</style>



 

Click here to view Example 3 applied to an actual web page...


NOTES:
  • When the above list of rule sets is inserted in the style element, it will apply the associated CSS properties to all H1, H2 and P elements on the web page. (Need to be more 'selective' with your selectors? Try using classes and IDs.)
  • The multi-word Times New Roman value must be enclosed in single (or double) quotation marks.
  • The value for the font-family property declared for the H2 and the P elements is a comma-separated list of fonts which will be applied in order from left to right according to the availability of the font on the user's computer. (This uses the same principle for applying a list of fonts using the font element in HTML. See Font Face for more information).
  • Type selectors (HTML element names) are case-insensitive.

*   *   *


Well then, that takes care of applying CSS to an entire web page. Let's turn it on full blast and learn how to apply CSS to multiple web pages at once using just one .css file. This is accomplished using external style sheets...


SEE ALSO: