Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Break the <FONT ...> Habit: Contextual Selectors

In the previous page we looked at using classes to apply a style to specific HTML elements. Another way to set styles that only sometimes apply to a certain element is to indicate that the rules apply only when that element is within another type of element. With this type of selector, called a contextual selector, you put the name of the outer element, then a space, then the name of the inner element. For example, suppose that you want to italicize anchors which are within <H1 ...> elements. You could do that with the following style rule (in a STYLE tag or in a style sheet file):

H1 A
   {font-style:italic;}

You then write the HTML as usual:

<H1>New Products, <A HREF="april.html">April 2001</A></H1>

which gives us

New Products, April 2001

Contextual selectors can also be used in conjunction with classes. For example, suppose you wanted to create a <DIV ...> element where the text is larger than usual, and just for anchors are italicized. That could be done with this style rule:

.topofpage
   {
   font-size:180%;
   }
.topofpage A
   {
   font-style:italic;
   }

The first rule says that all topofpage elements have a font size of 180%. The second rule says that <A ...> elements within topofpage elements are in italic. We can then apply the style to a <DIV ...> element like this:

<DIV CLASS="topofpage">
Links: 
[ <A HREF="./">Home</A> ] 
[ <A HREF="list.html">Listings</A> ] 
[ <A HREF="price.html">Price Chart</A> ] 
</DIV>

which gives us

Links: [ Home ] [ Listings ] [ Price Chart ]

 
 
  Copyright 1997-2002 Idocs inc. Published under the terms of the Open Content License Design by Interspire