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


spacer

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

Selectors So Far

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

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.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info
Copyright 2009 WebMediaBrands Inc. All Rights Reserved.

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

webref The latest from WebReference.com Browse >
Consuming a Web Service using ASP.NET Ajax · Thickbox 3.1 · User Personalization with PHP: The Admin Section
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.