spacer

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

home / programming / css_utopia / chap3 / current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]

HTML Utopia: Chapter 3: Digging Below the Surface

Lotus Notes Developer
AMS Staffing Solutions
US-OH-Columbus

Justtechjobs.com Post A Job | Post A Resume
Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

This chapter completes our look at the “mechanics” of CSS: the background you need to have to work with the technology. It covers six major topics:

  • quick review of the three methods for assigning CSS properties to HTML documents

  • use of shorthand properties to group values for a related set of properties in a single statement

  • workings of the inheritance mechanism in style sheets

  • structure of a style, including variations on the use of selectors for determining with great precision exactly what is affected by a style

  • units and values that can appear in styles to express sizes, locations, and other properties, and how they are used

  • CSS comments, which can be used to place human-readable notes in your CSS code

Applying CSS to HTML Documents

In Chapter 1, Getting the Lay of the Land, I introduced the three methods for applying style sheet properties to HTML documents. I will briefly review them here to jog your memory.

  • Inline styles: We can use the style attribute, which is available for the vast majority of HTML elements, to directly assign CSS properties to HTML elements.

    <h1 style="font-family: 'Comic Sans'; color: blue;">
      Welcome</h1>

    This method is best reserved for when you want to try out quickly one or more CSS properties to see how they affect an element. You should never use this method in a practical Web site, as it misses almost every advantage that CSS has to offer.

  • Embedded styles: We can use the <style> tag in the head portion of any HTML document to declare CSS rules that apply to the elements of the page.

    <style type="text/css">
    <!--
    h1, h2 {
      color: green;
    }
    h3 {
      color: blue;
    }
    -->
    </style

Using Shorthand Properties

Most property names take a single item as a value. When you define a property with a collection of related values (e.g. a list of fonts for the font-family property), the values are separated from one another by commas and, if any of the values include embedded white space or reserved characters such as colons, they may need to be enclosed in quotation marks.

In addition, there is a special set of properties called shorthand properties. Such properties let you use a single property declaration to assign values to a number of related properties. This sounds more complicated than it is.

The best-known shorthand property is font. CSS beginners are usually accustomed to defining font properties one by one:

h1 {
  font-weight: bold;
  font-size: 12pt;
  line-height: 14pt;
  font-family: Helvetica;
}

But CSS provides a shorthand property, font, that allows this same rule to be defined much more succinctly:

h1 {
  font: bold 12pt/14pt Helvetica;
}

All shorthand properties are identified as such in Appendix C, CSS Property Reference.

How Inheritance Works in CSS

Before you can grasp some of the syntax and behavior of CSS rules, you need a basic understanding of the inheritance CSS uses.

Every element on an HTML page belongs to the document’s inheritance tree. The root of that tree is always the html element, even in documents that fail to include the html tag explicitly.

Commonly, the html element has only two direct descendants in the inheritance tree: head and body.

Figure 3.1 shows a simple HTML inheritance tree for a small document.

Figure 3.1. Sample HTML Inheritance Tree Diagram

Sample HTML Inheritance Tree Diagram

As you can see, the document has in its head the standard title and link elements, the latter of which probably links to an external style sheet. It also includes a meta element (most likely to set the document's character set).

The body consists of five elements: an h1, an h2, a p element (labeled p1 so we can refer to it easily), a div and a list (ul) element. The div element, in turn, contains two paragraph elements, one of which has an emphasis (em) element, and the other of which contains an anchor (a) element. The ul element includes three list item (li) elements, one of which includes an emphasis (em) element, while another contains a paragraph element labeled p4.

Paragraph element p1 is a direct descendant of the body.

Each element in an HTML document has a parent element (with the exception of the root html element), and is said to be a child of its parent element. In Figure 3.1, for example, p2’s parent is the div. p2 would be described as a child of the div.

Some elements in an HTML document—and most of them in a complex document—are descendants of more than one element. For example, in Figure 3.1, the paragraph element p1 is a descendant of body and html. Similarly, paragraph elements p2 and p3 are descendants of the div element, the body element, and the html element. Paragraph element p4 is tied with several other elements in the document for the most ancestors: an li, the ul, the body, and the html elements. This notion of element hierarchy is important for two reasons.

First, the proper use of some of the CSS selectors you’ll work with depends on your understanding of the document hierarchy. There is, for example, an important difference between a descendant selector and a parent-child selector. These are covered in detail in Section , “Selectors and Structure of CSS Rules”.

Second, many properties for which you don’t supply a specific value for a particular element will take on the value assigned to the parent element. This means, for example, that if you don’t explicitly define a font-family property for the h1 element in the document diagrammed in Figure 3.1, it will use the font defined in the body tag. If no explicit font-family is defined there either, then both body text and the h1 heading use the font defined by the browser as the default. In contrast, setting the width property of an element will not directly affect the width of child elements. font-family is an inherited property, width is not.

Inherited properties, properties that are inherited from ancestors by default, are indicated in Appendix C, CSS Property Reference. In addition, you can set any property to the special value inherit, to cause it to inherit the value assigned to the parent element.

This inheritance issue can be tricky to understand when you deal with fairly complex documents. It is particularly important when you’re starting with a site that’s been defined using the traditional table layout approach, in which style information is embedded in HTML tags. When a style sheet seems not to function properly, you’ll frequently find the problem lies in one of those embedded styles from which another element is inheriting a value.


home / programming / css_utopia / chap3 / current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
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

Created: March 27, 2003
Revised: June 10, 2003

URL: http://webreference.com/programming/css_utopia/chap3