spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

Selectors So Far

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

In tutorial 5, we introduced some basic types of selectors. Specifically, a selector can be an element name by itself, in which case it will match all elements of that type. It can also be an element name followed by a period (.) and a string, in which case it will match all elements of that type that have a CLASS attribute with the string as a value. It can also be just a period and a string, in which case it will match all elements with that CLASS, regardless of their type. Replacing the period with a hash mark (#) matches the ID attribute instead of CLASS. Replacing the period with a colon (:) matches elements of a specific pseudoclass, that is elements that have a special characteristic. We looked at the link, visited and active pseudoclasses, that denote anchors that are unvisited hyperlink heads, visited hyperlink heads, or active hyperlink heads.

Here are some examples to freshen your memory:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
 "http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<TITLE>Example HTML Document Showcasing 
   Basic CSS Selectors</TITLE>
<STYLE TYPE="text/css">
<!--
H1 { color: red; /* A */ }
H1.special { font-style: italic /* B */ }
.wierd { color: magenta; /* C */ }
#unique { color: blue; /* D */ }
P#different { color: yellow; /* E */ }
A:link { color: green; /* F */ }
A:visited { color: darkgreen; /* G */ }
A:active { color: purple; /* H */ }
-->
</STYLE>
<BODY>

<H1 CLASS="special">A and B apply</H1>

<P CLASS="special">No rules apply.</P>

<P CLASS="wierd">C applies.</P>

<P ID="unique">D applies.</P>

<P><SPAN ID="unique">D applies</SPAN>. 
<SPAN ID="different">No rules
apply.</SPAN>. 
<A HREF="http://www.webreference.com/">F, G, or H apply
depending on whether the link is visited, 
unvisited or active</A>. <A
NAME="bar">No rules 
apply</A>.</P>

Those of you using Netscape Navigator 4.0 might have noticed a small discrepancy on the part of the browser. Netscape Navigator 4.0 colors the second SPAN element yellow, which means it matched the P#different selector even though it is a SPAN element, not a P element. Internet Explorer handles this correctly, however.

So how do we work around this? If you think about it, there's not much of a problem in the first place. Since only one element in a document can have the same ID attribute, a selector that matches ID will most probably be intended to match a single element anyway. A problem arises if, and only if, a style sheet with such a selector applies to more than one HTML document. If two such documents contain two elements with the same ID but different type, and a selector exists in the stylesheet that matches this ID but only one of the element types, Navigator will apply the ruleset to both elements. The way to work around this, put simply, is not to do it! There are very few cases where you would want this, and usually you can just assign a different ID to the two elements.

Front Page1234567

http://www.internet.com

Produced by Stephanos Piperoglou


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Workers Say Telework Is More Productive, Bosses Not So Sure · Kingston Debuts Power-Saving Memory Upgrades · Social Networking is King: Facebook Edges Google


All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/html/tutorial6/4.html
Created: September 24, 1998
Revised: September 24, 1998