Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Contextual Selectors


WebReview.com: Contextual Selectors

As we learned a few weeks ago, the first part of a style rule is the selector. It selects the tags to which the style properties will be applied. In general, the selector contains a list of one or more tag names, separated by commas, that define the tags affected by this rule. If you like, you can add class and pseudo-class names to the tag names to make the rule definition more specific. Even so, the rule is applied to every tag in your document without regard to position or tag nesting.

Contextual selectors give you some control over the styles applied to tags in a specific order in your document. Keep in mind that a well-formed HTML document consists of nested tags, starting with the outermost <html> tag and working inward to individual <p> tags. As content is created within frames, tables, divisions, and other flow control elements, you build a tag nesting within tags.

By using contextual selectors, you define styles that are only applied when certain tags are nested within other tags. For example, you might want paragraphs within certain table cells to be set in a smaller type face or to be aligned a certain way. With contextual selectors, paragraphs in table cells will get the desired style changes, but other paragraphs will not be affected.

Using contextual selectors

Let's stick with our example and create a contextual selector that causes all text with table cells to be smaller than the rest of our document:

     TABLE TR TD P { font-size : smaller }

In this selector, we've spelled out the complete sequence of nested tags that will contain our content, from the outermost <table> tag to the inner <p> tag. When the browser encounters this nesting sequence, it will apply the font-size : smaller property to the text.

This selector won't work for text in <th> cells, since they have a different nesting sequence. Realizing that tags get nested in lots of different ways, HTML lets you be fairly loose in how you specify the nesting. A better way to create this contextual selector would be to just use:

     TABLE P { font-size : smaller }

The browser will apply the style if it ever encounters a <p> tag nested somewhere within a <table> tag, regardless of the intermediate tags between the two.

You can create contextual selectors with classes to further refine the application of styles. For example,

     TABLE.smaller P { font-size : smaller }

creates a context with a <p> tag nested within a <table> tag whose class attribute is set to smaller. Paragraphs in any other tables will not be affected by this style rule.

Contextual selectors are powerful tools that give you tremendous control over complex document layouts. As we'll see in coming weeks, you can create all sorts of clever styles that get applied automatically as your document tags nest in certain patterns.


Previously in Tag of the Week:

A Special Kind of Class
Using IDs as Classes
How to Add Class to Your Tags


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.