This lecture notes contain the following:
Embedded style and .css files contain selectors and not HTML tags. There are three different types of selectors and one combination selector as follows:
All selectors contain a name, property and value. The property and value are enclosed in curly braces. If you wanted to style a p tag with the font color blue, your CSS style would appear like this in the CSS code:
p {color: blue}
The selector parts are described in this figure:
If you have multiple properties, they are separated by semi-colons. So, if you add a font-family of Times New Roman to the blue p type selector, it would look like this:
p { color: blue;
font-family: Times New Roman;
}
Instructor Note:
The blank spaces and returns have no bearing on how browsers interpret this code, just as those things don't matter in HTML also. The only required elements are the braces, colons and semi-colons as shown.
There is also a cascading or preference order to how a Web browser will interpret the styling in these elements. Type selectors are the most basic, class selectors will override type selectors, and ID selectors will override class and type selectors.
In other words, if you have created a p type selector that gives a blue font color for all of the paragraphs on the page, then any class selectors that are applied to paragraphs on the page and have a red font will make those paragraphs red instead of blue. Also, if a div tag on the page has been given an ID that specifies a green font color, then all of the paragraphs inside that div tag will be green, regardless of whether they have class applied to them or not.
ID selectors are given a magnitude of 100 times preference in terms of browser weighting of importance, while class selectors are 10 times, and type selectors are only 1 times preference. In other words, ID selectors are 100 times more important and relevant than type selectors and 10 times more important than class selectors based on this weighting system.
As a best practice, I recommend the use of the selectors as follows:
Instructor Note:
To accomplish this in Dreamweaver:
1. Click on the paragraph in Design view.
2. Select the paragraph by clicking on the <p> tag on the status bar below the page -- notice that the entire paragraph will highlight.
3. Right click on the <p> tag on the status bar.
4. Move your mouse to Set Class.
5. Choose the class you created, in this case .redfont, from the list of classes.
6. Note that the paragraph now conforms to that class you chose.
You will see that in the HTML a class attribute used. It will look like this in the HTML:
<p class="redfont">
Selectors can be used in combination with one another. Let's say you have several list items on a page, but you want to add 50 pixels of left padding only to the list items within the div called #navcontainer. In the CSS, you can follow the ID selector #navcontainer with the type selector li so that only those list items are affected. In the CSS it would look like this:
#navcontainer li {padding-left: 50px}
You'll see many more combinations like this as you work through the CSS exercises. Also note, Dreamweaver refers to Descendant selectors as Compound selectors -- it means the same thing.
What happens when you have two style rules that seem to state opposite ideas? Which one wins? Consider this example:
In your style sheet, you have these two selectors
#content p { color: blue; }
p { color: red; }
The first selector (the Compound) is suggested that all p tags within the div called "content" are to be colored blue, but the tag selector for all the p tags on the page says that all of them, no matter what div they are in, are to be colored red.
So which one gets the style?
The one that's more "specific" is the one that would style the element.
The way browsers work is that, to determine which one is being more specific, they assign a numerical value to the selectors used, with these value:
Instructor Note:
tag selectors getting a 1 point value, class selectors getting a 10 point value, and ID selectors getting a 100 point value.
Compounds add all their selectors together. The highest numerical value in total for the selector or Compound will win.
In the case above,
#content = 100 + p = 1, which is a total of 101
Since the other is just a p or a value of 1, then the Compound wins, so those p tags inside div#content would be blue, but the ones outside div#content would still be red.
Check out this site for a really good article that explains the concept of specificity in better detail: HTML Dog: Specificity.