The Wayback Machine - https://web.archive.org/web/20081202150443/http://www.webreference.com/html/tutorial6/4.html


spacer

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

Selectors So Far

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

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

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Copyright 2008 Jupitermedia Corporation All Rights Reserved.

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags


All Rights Reserved. Legal Notices.

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.