Looking for the previous guiStuff?

It's still here, the content didn't go anywhere. You may want to check out this new guiStuff though -- It's rather informative.

References/Tutorials:


Intro Documents:


guiStuff:






::Stuff for the multi-spec coder;

Coding, formats, standards, and other practical things.

 Home  //  CSS Articles  //  The Document Tree 

<!-- CSS Articles

The Document Tree

The Document Tree is the relational structure of the HTML/XHTML document. It is not a standard implementation of what is normally referred to as a "Tree Structure" in most programming languages, but is rather implemented differently in several aspects (how it differs from other tree structures may be rather technical to describe here, I just didn't want to give the impression that if you're familiar with one of the standard tree structure that the information here will be completely redundant). Elements within an HTML/XHTML document form a document tree, that "links" them together in different relations. Every element within a document tree has exactly one parent, except for the root of the tree, which has none. So, in HTML, the root would be the HTML element, which wraps all other elements, but is wrapped by none.

A visual representation helps a lot in this case. Here's a sample of HTML within a page:

<div id="container"> <h1>Main Heading</h1> <p>Most programmers <em>rely</em> on caffine</p> <p>Most programmers like Anime</p> <div class="lister"> <h2>The 'P' Interpreted Languages</h2> <ul> <li>Perl</li> <li>PHP</li> <li>Python</li> </ul> </div> </div>

Now, here's the visual representation of the document tree of the above HTML:

Document Tree 1

As you can see, the document tree, in relation to HTML, is all about which elements are contained within which other elements. The above example doesn't include the BODY, HEAD, or HTML elements, so this isn't a complete representation of an HTML document, but that doesn't matter: a document tree is still valid when it's just a "branch" of elements that was cut off from a main tree. You could say that this document has a pseudo root element, since the upper-most DIV has no parents within this context, but we know that it can't just float in space, and that in a valid HTML/XHTML document, it must be contained within other elements, and/or eventually the BODY of the page.



The Different Relationships

There are a set of different relationships that link the different elements together. Let's go over them one by one:


Here's another visual representation before we move on:

Document Tree 3

The above diagram brings into light another aspect: multiple relationships. One element can have more than one relationship with its surrounding elements, and often does. For example, the UL element is a child of the DIV of class="lister", a descendant of the DIV of id="container", has three LI children elements, and is a sibling of the H2 element. That's not just many relationships, it's many types of relationships. Simply having many relationships is easy: The DIV of id="container" has 4 children and 6 descendants.



More Subtle Relatioships

There are some more subtle relationships, some are just semantic, and some are specific to the document tree.

Here are two semantic ones:

The reason for these are mainly for the purpose of clarity in definitions. For example, instead of saying "The DIV of id="container" has 4 children and 6 descendants.", you could say: "The DIV of id="container" has 10 descendants, 4 of which are direct-descendants".

The following relationships are what really set the Document Tree apart from other tree structures - especially the Preceding/Following relationships, which go against many other standards, and may sometimes be difficult to understand. While these relationships bleed a bit into the Document Object model, they all apply to CSS in one way or another. Even the Following/Prededing apply when relating to the margin property, because of the margins-collapse effect. With the exception of the Prededing/Following relationships, they all deal solely with siblings, so this will narrow down the scope of thought as you go through them:



Wrapping Up

I hope this cleared up the relational structures that are present in the document tree. This is required in quite a few fields that have to do with W3C document formats, such as XML, XHTML, the DOM, and other derived document formats. It is also required in order to properly implement CSS selectors, and many JavaScript functions/methods that relate to the DOM. By the way, if you really want to dig into the DOM spec, you can find the entire thing here. It may be a bit intimidating at first, and rather abstract at times, but if you deal with many web-related standards then you'll be amazed at how many things it applies to, and how much of it you're already familiar with.





Return to the CSS Articles section, or go the to Main page.





Looking for the old guiStuff?

It's still here, the old content didn't go anywhere.