Embedded Style Sheets
ON THIS PAGE
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:
SELECTORrefers 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.propertyrefers to a CSS property.valuerefers to an associated CSS value.
Here's a few things to remember about CSS syntax in embedded style sheets:
- The list of property/value pairs (a.k.a., style declarations) are enclosed in a set of curly braces
{ } - The property and its associated value are separated by a colon (:)
- Each property/value pair ends with a semi-colon (;)
- 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
styleelement, it will apply the associated CSS properties to allH1,H2andPelements on the web page. (Need to be more 'selective' with your selectors? Try using classes and IDs.) - The multi-word
Times New Romanvalue must be enclosed in single (or double) quotation marks. - The value for the
font-familyproperty declared for theH2and thePelements 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 thefontelement 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:
| <~ BACK | TOP | NEXT ~> |
