<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
You should place the correct doctype at the very top of your HTML document (even before the <html> tag). The full list of valid doctypes is here:
http://www.w3.org/QA/2002/div
{
background-color:blue;
}
The selector for defining a tag style is simply the tag name.
.favorite
{
background-color:blue;
}
The selector for defining a class style is: .classname.
<div class="favorite>Here is a div example</div>
<a class="favorite">Here is an anchor example</div>
Remember to always have class="classname" in the elements that you want to be styled.
#myblue
{
background-color:blue;
}
The selector to define a style for a particular ID is: #id.
:hover
{
background-color:blue;
}
The selector for defining a style for a pseudo-class/element is: :pseudo_class_name
[valign="middle"]
{
background-color:blue;
}
The syntax for defining a style based on the elements attribute is: [attribute="option"]. You can style an element that has any value for an attrubute using: [attribute].
.csstest:hover
{
background-color:blue;
}
You could also have written it:
:hover.csstest
{
background-color:blue;
}
Here is an example of a more complicated selector:
div.csstest#mydiv[valign="middle"]:hover
{
background-color:blue;
}
ul.list, ol.special
{
background-color:blue;
}
To group selectors, separate them with a comma.
div span
{
background-color:blue;
}
The syntax for defining the style of a descendant is: ancestor *space* descendant. The style will only apply to the descendant, not to the ancestor.
<div>
Here is the span: <span>SPAN!</span>
</div>
Even if the descendant is not a direct child of the ancestor, it will still have the style. The span below will also have a blue background.
<div>
<ul>
<li><span>Here is the span.</span></li>
</ul>
</div>
You can define as many ancestors as you want. Below is an example:
div div span
{
background-color:blue;
}
The above style will apply to a span that is the descendant of two divs.
ul.nav li:hover ul
{
position:absolute;
left:100%;
visibility:visible;
}
The above style could be used as a stepping stone toward making a navigation bar.
<div id="div1">
Here is span #1: <span>span1</span>
</div>
<div id="div2">
<ul>
<li>Here is <span>span2</span></li>
</ul>
</div>
Span1 is a child of div1 because span1 is nested inside of div1 and div1 is only one level up from span1. On the other hand, span2 is
not a child of div2 because span2 is also nested inside of a ul element and a li element. In fact, span2's actual parent is the li because the li is one level up from span2.
div > span
{
background-color:blue;
}
The syntax for this selector is: parent > child. If this style were applied to the html before, span1 would have a blue background, but span2 would not.
img + p
{
background-color:blue;
}
The syntax for styling a sibling is: sibling + sibling_to_style.
<div>
<img src="myimage.jpg" alt="myimage" />
<p>Here is paragraphA</p>
</div>
<div>
<p>Here is paragraphB</p>
</div>
In the above code, the first paragraph element (A) will have a blue background because both it and the img element are nested inside the same div element. However, the second paragraph element (B) will not have a blue background because its parent does not also have an img child.
div * + ul
{
background-color:blue;
}
The above code references any ul that has any type of sibling inside a div element.
p
{
color:red;
}
.blueclass
{
background-color:blue;
}
<p class="blueclass">What color will this paragraph be?</p>
The paragraph above will have a red color and a blue background. Why? As long as the properties don't conflict with one another, the element inherits each property.
/* Number 1 */
.classred
{
background-color:red;
}
/* Number 2: */
div ul li a
{
background-color:blue;
}
At first it appears that number 2 is more specific, but style number 1 is the one that will be used. Why? Because number 1 has a class definition, but number 2 does not -- it doesn't matter that number 2 has four tag selectors because tag selectors are not as important as class selectors.
.test
{
background-color:blue;
}
.test
{
background-color:red;
}
In this case, the element will have a red background.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (9)
Author
Commented:The browser should resolve conflicts in the order proximity, specificity, then the latter wins. If the proximities are the same, then the browser looks at the specificity. If the specificities are the same, then whichever style is declared last will take precedence.
So, in a way
* proximity trumps inheritance
* proximity trumps specificity
* specificity trumps the latter style
Remember that styles "cascade" into a virtual style. One style doesn't completely "trump" out the others-- they combine. Precedence comes into play only if there are different values for a particular property (like color).
Open in new window
Commented:
Awesome condensed primer on the use of styles! I quickly read through it and gotta say, this is a nice primer that I can use to 'enlighten' some colleagues! They always congratulate me on using my 'magic' this might lift the veil of mysticism a little.
As for proximity/spcificty, inline should probably be highlighted with more emphasis, since it always overrules specificity, while local styles are treated as the 'last stylesheet' so that specificity can still overrule, unless I'm very much mistaken :) That's about the only thing I would change if I were pressed for comments ;)
Author
Commented:Thanks for the constructive criticism! I'm always open to suggestions =D I'm at a bit of a loss, though, as to what you think I should change. Could you clarify? =)
Author
Commented:Commented:
Specificity
But say that you have two conflicting styles located in the same place. Now what happens?
In this case, the importance of the style is calculated using specificity. Here's how it works:
0) Inline styles will always overrule stylesheets (<p style="font-weight:bold">)
1) The style with the most id selectors (#id) will take precedence.
2) The style with the most class, attribute, and pseudo-class selectors will take precedence.
3) The style with the most tag selectors will take precedence.
***snipped for condensed reply***
The Latter Wins
And what happens if they have the same specificity? Simply put, the latter style wins. Whichever style is declared later in the style section will be the style that takes precedence. This is really the same rule as Proximity, but applied within a single style sheet or style block.
Styles defined in a style block within the document are viewed as the 'last added stylesheet' and as such will always be the latter style in equal specificity conflicts.
Still an awesome article though :)
View More