Notice
The Page Has Not Loaded Properly.
While Holding The Cmd Key Down,
Tap The R Key 5 To 7 Times.
Not all examples given are practical. They are there to make a point.
None of the examples include the appropriate document type, character set or name space information as required by the W3C Specifications.It also should be noted that a tag is not an element. An element normally includes a start tag, content and an end tag. Except those elements that are defined as normally open.
selector { declaration }
A declaration contains a property and a value:selector { property: value }
Notice the curly brackets ( { } ) surrounding the declaration, and the colon ( : ) separating the property from the value.Selectors and declarations can be grouped:
selector, selector { property: value }selector { property: value; property: value }
selector, selector { property: value; property: value }Notice the coma ( , ) separating the selectors and the semicolon ( ; ) separating the declarations.
The selector is the connection between the style sheets and the body of the page. The selector can be most HTML element types. A sampling of some of them that work with Webtv 2.8.2 are:body, h1, h2, h3, h4, h5, h6, p, b, i, ol, ul, li, table, td, big, cite, del, dfn, em, img, ins, kbd, pre, small, samp, s, sub, sup, tt, u, var, anchor pseudo-classes (link, visited link and active link).
The <div> (block) and <span> (inline) provide a generic means of adding style sheet properties and values with class and id, or inline.The basic properties and values are similar to what we're already used to, except that they're formatted differently. Allot of them reference color, size, style, family, alignment, margin, weight etc. There are however, many more we can use with CSS. Also, we can make changes to some elements that we never could without CSS.
Take the body as the selector for example. We can start out with the basic background and text information:
body {
background-image: url(urloftheimage.jpg);
background-color: #ffffff;
color: #000000;
}
body {
background: url(urloftheimage.jpg) #ffffff;
color: #000000;
}
body { background: url(urloftheimage.jpg) #ffffff; color: #000000; }
Or this way: body{background:url(urloftheimage.jpg)#ffffff;color:#000000;}However, it's always best to write code in a way that's easy to read, understand and edit.
There are parent/child relationships involved here. Every property and value we put in the body element will effect all the elements on the page, because in this case the body is the parent and all other elements are the children. We can change that for each element by giving them their own set of properties and values. For example, a page title:
h1 {
color: #dc143c;
font-size: 27pt;
font-weight: normal;
text-align: center;
}
This process can be continued for every element on the page by addressing each element with it's own properties and values.
p {Now all text within <p> and </p> will be dark blue, 23pt (size 5) in size and center aligned.
We can also use comments, which are basically notes and reminders to help keep style sheets organized. Comments will not be displayed on the page.This is a comment example:
/*main title*/We connect these rules to the body of the page by simply using the elements used as selectors.
The embed method is a very common way of adding and learning style sheets, where you put the code between the </title> and </head> tags.This is an example (without a background image), where
<style type="text/css"> is the opening tag,|
<html> <head> <title>CSS Example</title> <style type="text/css"> <!--
body { h1 { color: #dc143c; font-size: 27pt; font-weight: normal; text-align: center; }
/*larger and centered text*/ </style> </head> <body> <h1>Page Title</h1> The body text is here. <p>The larger and centered dark blue text is here.</p> </body> </body> </html> |
Which would look like this:
|
Page Title The body text is here.The larger and centered dark blue text is here. |
selector selector { property: value }
Notice the white space between the selectors.For example, if you want all emphasized text within bold text to be blue, you could do this:
b em { color: #0000ff; }Then in the body of the page you could put:
<b>I can't <em>emphasize</em> enough the <em>significance</em> of Cascading Style Sheets.</b>Which would render:
I can't emphasize enough the significance of Cascading Style Sheets.Contextual selectors can be grouped.
selector selector, selector selector { property: value }Notice the coma ( , ) between the selector groups.
So now if you want all emphasized text within bold and H4 text to be blue, you could do this:b em, h4 em { color: #0000ff; }
Then in the body of the page you could put:
<h4><em>Contextual</em> Selector Example</h4>
<b>I can't <em>emphasize</em> enough the <em>significance</em> of Cascading Style Sheets.</b>
We can name a class or id anything we want to, as long as it begins with a letter (a - z). After that the name can also have numbers (0 - 9) and hyphens ( - ). They can also include some characters from ISO 10646 and some escaped characters. I keep it simple by starting a class or id with a letter and then use letters and numbers after that. Although classes are not case sensitive, it is a good idea to use lower case, as the future of markup language will require that.
Here's an example of a class:p.txt1
Notice the dot/period between the element (in this case p) and the class name (in this case txt1).Then in the body any p element that has txt1 will be affected.
For example:
p.txt1 {
background: #f0f8ff;
color: #00008b;
font-size: 19pt;
font-style: italic;
}
<p class="txt1"> This is an example of using class to change text. </p>
Which would render:This is an example of using class to change text.
We can accomplish the same thing by dropping the element (in this case p) from the class:.txt1
Notice the period/dot ( . ) at the beginning.Then we can use that class on any element including span or div. This time I'll use span:
<span class="txt1"> This is an example of using class to change text. </span>This is an example of using class to change text.
Notice the difference between using p and span. With p the background color flows to the right side of the page. With span it's contained where the text is.With span we can affect a single word or letter with css:
This is an <span class="txt1">example</span> of using class to ch<span class="txt1">a</span>nge text.This is an example of using class to change text.
We can simplify it even more by using i as the selector:
i {
background: #f0f8ff;
color: #00008b;
font-size: 19pt;
font-style: italic;
}
This is an example of using class to change text.
Here's an example of id:#txt2
Notice the number sign ( # ) at the beginning.An id is pointed toward a specific, one time use. Unlike a class where we can use the same class more than once, an id is for one particular instance. For example:
#txt2 {Can be applied to one element only.
<div id="txt2">division content goes here</div>Now that it's been applied, we can't go ahead and use it on another element.
One example where this really comes into play is with absolute positioning. Where each absolutely positioned element has it's own id (identifier).Here are the selectors for link, vlink and alink:
a:linkNotice the colon ( : ) between the a and link, the a and visited, and the a and active.
Each one of these selectors can have completely different properties and values. They are independent of each other.Here are examples from this page:
a:link {As you can see I've kept them very basic, I only changed the color. I could have had all different sizes, alignment, family, style, decoration, and any other properties and values I wanted. An important point to make here is text-decoration: none. PC viewers find it anoyying that links are usually underlined. By using text-decoration: none, the underline will be removed from the links. Most PC viewers will appreciate that consideration.
|
<html> <head> <title>CSS Example</title> <style type="text/css"> <!--
body { color: #8b4513; font-size: 19pt; text-decoration: none; }
a:visited { color: #a9a9a9; font-size: 19pt; text-decoration: none; }
/*page title*/ p { color: #00008b; font-size: 23pt; text-align: center; }
/*contextual example*/ .txt1 { background: #f0f8ff; color: #00008b; font-size: 19pt; font-style: italic; }
--> <p>The larger and centered dark blue text is here.</p> <h4><em>Contextual</em> Selector Example</h4><b>I can't <em>emphasize</em> enough the <em>significance</em> of Cascading Style Sheets.</b> <p><span class="txt1">This is an example of using class to change text.</span> </p>
Visit These Sites ! |
Which would look like this:
|
Page Title The body text is here.The larger and centered dark blue text is here. Contextual Selector ExampleI can't emphasize enough the significance of Cascading Style Sheets.This is an example of using class to change text. Visit These Sites !Cherry-Drops Gifs Midnight Midis MsV's Magic |
This is a very basic example. Even with the limited compatibility, there's much more we can do with CSS to make our pages more efficient, simpler and W3C compliant.
Be sure to check out the "Go To" links at the top of the page for information on topics not yet covered. And of course, the references provided contain a wealth of information not found here.