Web Design Stuff
HTML Tutorials    CSS Tutorials    Web Hosting   Resources
Create a Web Page 101
Making Web Pages Intro What is a Web Page? Why Make a Web Page? The History of HTML Learn HTML or XHTML?
Basic HTML
Basic HTML Tutorials Basic HTML Necessities How to Make a Web Page How to Edit a Web Page The Basics of HTML Tags Basic HTML Page Structure HTML Attributes
HTML Font Codes
HTML Font Codes Intro HTML Font Color Codes HTML Font Size Codes HTML Font Style Codes HTML Bold/Italic Codes Combining Font Codes
Formatting Text
Formatting Text Intro Making Paragraphs Miscellaneous Formatting Headings & Subheadings Creating Hyperlinks
Using Graphics
Using Graphics on the Web Add Graphics to Your Pages Graphics and Accessibility How to Align Graphics Page Color & Background Graphics as Hyperlinks Horizontal Rules
Creating Tables
HTML Tables Tutorials HTML Table Fundamentals Background & Border Color Table Frames & Rules Table Width and Alignment Cells 1 -Space & Alignment Cells 2 -Row Column Span Cells 3 -Width & Height
Making Lists
HTML Lists Tutorials Bulleted Lists Numbered Lists Definition Lists
HTML Frames
HTML Frames Tutorials Using Frames for Layout Advanced Frame Layouts Putting Hyperlinks in Frames Frame Border Width Color, Margin and Control Problems with Frames SmartFrames: A Solution SSI: An Alternative to Frames
Web Page Forms
Making Feedback Forms A Simple Feedback Form Installing NMS FormMail Debugging Your Setup My Web Host is Out to Lunch User Input Components Text Fields Checkboxes & Radio Buttons Dropdown Menus Push Buttons Layout and Presentation
Basic CSS
Basic CSS Tutorials What is CSS? Why You Should Use CSS How to Use CSS Inline Styles Embedded Style Sheets External Style Sheets Class Selectors ID Selectors Combining Selectors
CSS Properties
CSS Properties Intro Font Styles Width, Height & Spacing Borders Backgrounds Position Float & Alignment Hyperlinks
All About Web Hosting
Hosting Your Own Website What is a Web Host? Your Website's Home Page Building a Website Offline About Free Web Hosting Best Free Web Hosting Commercial Web Hosting How to Get a Domain Name Ecommerce Web Hosting Web Hosting Terminology
Free Web Design Tools
Best Free Website Tools Best Free Text Editors Best Free Graphics Editors Free Website Analysis Tools
Setting Up HTML Kit
HTML Kit Introduction How to install HTML Kit Screenshot Breakdown Basic Configuration Overall Appearance Shortcuts and Startup Editing Window Customizing Toolbars Using the Favorites Tab Making a New Actions Bar Odds and Ends
Free Templates
Free Website Templates Two Column Fixed Width Three Column Liquid Layout Miscellaneous Templates Dynamic Menu Effects Two Column Experimental Terms of Use About These Templates
Website Templates Help
Getting Started Template Zip File Download How to Edit Your Template What to Edit in the HTML How to Add Your Logo Making a Website
Web Design Tips
Web Design Basics Tables vs. Tableless Using Tables for Layout Example Table Layouts World's Crappiest Web Page
Twitter Backgrounds
Twitter Backgrounds Intro Cool Twitter Backgrounds Cool Twitter Backgrounds 2 Plain Twitter Backgrounds Dark Twitter Backgrounds Best Twitter Backgrounds Cute Twitter Backgrounds Music Twitter Backgrounds Music Twitter Backgrounds 2 Twitter Backgrounds 101 TERMS OF USE
All About Web Browsers
What is a Web Browser? Mozilla Firefox Internet Explorer Opera How to Set Up Firefox Top 5 Firefox Extensions
 
Contact
Post Some Feedback Send Me An Email Iron Spider Blog About Iron Spider... Site Conventions
 
 
 

 

Combining Selectors

 
Now that you're familiar with how to use selectors in CSS rule sets including type selectors (HTML element names), class selectors and id selectors, we can move on to learning about other kinds of selectors to open up more complex and useful targetting possibilities.

Here we'll introduce a couple of advanced methods of using CSS selectors here which will help you:
  1. Apply style declarations to selectors contained inside other selectors
  2. Apply the same style declarations to a group of selectors
Both of the above involve using multiple selectors in the same rule sets which will —among other things— help you to economise your CSS and HTML coding.

We'll start with...


Descendant Selectors


Okay so let's say you want to have a site menu running across the top of your web page and you would like all the hyperlinks in the site menu (and only the hyperlinks) colored green. One way of doing it would be to create a class called, say, 'SiteMenu' and then define it in an external stylesheet:

Example 1A - RULESET FOR .SiteMenu CLASS

.SiteMenu {
   color: green;
}




Then we'd insert the class="SiteMenu" attribute/value pair in each anchor tag used in the menu like this:

Example 1B - HTML CODING FOR SITE MENU

<div>
<a class="SiteMenu" href="web address">Link text</a> -
<a class="SiteMenu" href="web address">Link text</a> -
<a class="SiteMenu" href="web address">Link text</a> -
<a class="SiteMenu" href="web address">Link text</a> -
<a class="SiteMenu" href="web address">Link text</a>
</div>



Simple enough? Well not really. It could be simpler. You might have noticed that I wrapped all those menu hyperlinks in DIV element. We can actually use that DIV element to apply CSS to any or all elements contained inside of it.

The way it works is any HTML elements contained directly inside this DIV are called child elements while any elements contained inside the DIV's child elements are called the DIV's grandchild elements (and so on and so forth). Hence all elements contained inside the DIV element are said to be its descendants and we can use CSS to apply styles to all those descendants using descendant selectors.

A descendant selector is written by declaring the 'parent' selector followed by the descendant to which the style declarations are applied. The two are separated only by a space (mandatory).

Let's rewrite our site menu using a descendant selector. First the ruleset in the stylesheet:

Example 2A - RULESET FOR DESCENDANT SELECTOR

.SiteMenu A {
   color: green;
}




And next, the HTML code that renders the menu itself:

Example 2B - HTML CODING FOR SITE MENU USING DESCENDANT SELECTOR

<div class="SiteMenu">
<a href="web address">Link text</a> -
<a href="web address">Link text</a> -
<a href="web address">Link text</a> -
<a href="web address">Link text</a> -
<a href="web address">Link text</a>
</div>



Here you'll notice that the class="SiteMenu" attribute/value pair is only declared once in the DIV 'wrapper' element. Essentially what the code in the stylesheet is saying is that any A elements that are contained in (descendants of) any element that is using the SiteMenu class will be colored green. Here the DIV element is using the SiteMenu class so the text in all its descendant A elements will be colored green. There is now no need to repeat the SiteMenu class in every individual anchor tag as in Example 1.

You are not confined to using just two selectors to create descendant selectors nor are you obliged to use any classes (or IDs). You can, if you like, just use a string of element names to create a complex descendant selector that walks down an element's 'family tree' to target specific elements anywhere in its heirarchy.

For example, the following CSS code:

Example 3A - DESCENDANT SELECTOR TARGETTING A GREAT GRAND CHILD ELEMENT

table table table table {
   border: 1px solid black;
}



...will target all tables contained within other tables contained within other tables contained within other tables. Simply put, this will target all tables nested inside other tables four levels deep.

The following illustrates some HTML code applicable to this situation:

Example 3B - NESTED TABLES GOING FOUR LEVELS DEEP


<table cellpadding="0" cellspacing="0" border="0"><tr><td>

<table cellpadding="0" cellspacing="0" border="0"><tr><td>

<table cellpadding="0" cellspacing="0" border="0"><tr><td>

<table cellpadding="0" cellspacing="0" border="0"><tr><td>
This is the table targetted by the descendant selector. 
It's border will be colored black and will be 1 pixel wide.
</td></tr></table>

</td></tr></table>

</td></tr></table>

</td></tr></table>

Be advised that most web standards gurus would have a litter of kittens over HTML code written like this. But it does exist (e.g., the default MySpace template uses tables nested six levels deep) and descendant selectors can be used to target elements in this manner.

Your typical usage of descendant selectors, however, will probably be much less insane such as creating a class to define a table's borders and then using the same class in various descendant selectors to style the table's cells and to style hyperlinks in the table's cells.

Here's an example stylesheet:

Example 4A - THE SECOND AND THIRD RULE SETS USE DESCENDANT SELECTORS

.SiteMenu2 {
   border: 1px solid black;
}

.SiteMenu2 TD {
   width: 100px;
}

.SiteMenu2 TD A {
   color: green;
}



And here's the applicable HTML coding:

Example 4B - A SITE MENU USING A TABLE STYLED WITH CSS

<table class="SiteMenu2"><tr>
<td><a href="web address">Green link text</a></td>
<td><a href="web address">Green link text</a></td>
<td><a href="web address">Green link text</a></td>
<td><a href="web address">Green link text</a></td>
<td><a href="web address">Green link text</a></td>
</tr></table>



The above example represents an efficient use of CSS by defining just a single class for the table and then using appropriate descendant selectors to target other elements inside the table. The .SiteMenu2 class selector is used to set the table border color black and at 1 pixel wide. The two following descendant selectors are used to set the width of all the table cells at 100 pixels and to set the color of all hyperlinks in each table cell green.


Grouping Selectors


This one's a lot easier. Basically, to avoid having to rewrite the same style declarations for a number different selectors, you can just group all the selectors in a comma-separated list and use it in one rule set. Let's say we wanted all paragraphs, DIVs and blockquotes in our web page to be displayed using Tahoma font with a 14 pixel font size.

We could —if we wanted to do it the hard way— write separate rule sets for each like this:

Example 1: USING INDIVIDUAL SELECTORS FOR EACH RULE SET

p {
   font-family: tahoma;
   font-size: 14px;
}

div {
   font-family: tahoma;
   font-size: 14px;
}

blockquote {
   font-family: tahoma;
   font-size: 14px;
}




BUT...

It would be a lot easier if we just put the p, div, and blockquote selectors in a comma-separated list thus defining it as a group and then just apply the style declarations to all of them at once like this:

Example 2: USING A GROUP OF SELECTORS IN ONE RULE SET

p, div, blockquote {
   font-family: tahoma;
   font-size: 14px;
}




Along with type selectors, you can also group class selectors, id selectors and even descendant selectors like this:

Example 3: GROUPING ALL KINDS OF SELECTORS

.MainText, #Heading1, p, p blockquote, {
   font-family: tahoma;
   font-size: 14px;
}




The above rule set applies styles to following:
  1. All elements using the MainText class
  2. All elements using the Heading1 ID
  3. All paragraphs
  4. All blockquotes contained inside paragraphs
Just remember to use commas to separate the list of selectors you wish to group together (the space after each comma is optional).


*   *   *


And that's a wrap on basic CSS. Now all you need is a handy list of CSS properties and values to help you remember what does what and what HTML elements they can be applied to...





Best Free Stuff
for webmasters

Free Text Editors
Free Graphics Editors
Website Analysis Tools
Free Website Templates

See also:

Best Free Web Hosting
How to Make a Web Page

 

If you need a .COM web address, you can get one quick and easy at...

www.GoDaddy.com
<~ BACK TOP NEXT ~>
 
HTML Tutorials |  CSS Tutorials |  Web Hosting |  Domain Names |  Web Design Resources
Iron Spider © Copyright 2004-2011 Robert Darrell