Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 808477 of How CSS works

  • Revision slug: Web/Guide/CSS/Getting_started/How_CSS_works
  • Revision title: How CSS works
  • Revision id: 808477
  • Created:
  • Creator: kokpingli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSTutorialTOC() }}

{{ previousPage("/en-US/docs/Web/Guide/CSS/Getting_Started/Why_use_CSS", "Why use CSS?") }}This third section of the CSS Getting Started tutorial explains how CSS works in your browser and the purpose of the Document Object Model (DOM). You'll also learn how to analyze your sample document.

Information: How CSS works

When a browser displays a document, it must combine the document's content with its style information. It processes the document in two stages:

  1. The browser converts the markup language and the CSS into the DOM (Document Object Model). The DOM represents the document in the computer's memory. It combines the document's content with its style.
  2. The browser displays the contents of the DOM.

A markup language uses elements to define the document's structure. You mark an element using tags, which are strings beginning with '<' and ending with '>'. Most elements have paired tags, a start tag and an end tag. For the start tag, place the element name between '<' and '>'. For the end tag, place a '/' after the '<' and before the element name.

Depending on the markup language, some elements have only a start tag, or a single tag where the '/' comes after the element name. An element can also be a container and include other elements between its start tag and end tag. Just remember to always close the tags inside the container.

A DOM has a tree-like structure. Each element, attribute and run of text in the markup language becomes a node in the tree structure. The nodes are defined by their relationship to other DOM nodes. Some elements are parents of child nodes, and child nodes have siblings.

Understanding the DOM helps you design, debug and maintain your CSS, because the DOM is where your CSS and the document's content meet up.

Example
 
In your sample document, the <p> tag and its end tag </p> create a container:
<p>
  <strong>C</strong>ascading
  <strong>S</strong>tyle
  <strong>S</strong>heets
</p>

{{ EmbedLiveSample('Information.3A_How_CSS_works', '', '', '', 'Web/Guide/CSS/Getting_started/How_CSS_works') }}

In the DOM, the corresponding P node is a parent. Its children are the STRONG nodes and the text nodes. The STRONG nodes are themselves parents, with text nodes as their children:

P
├─STRONG
│ └─"C"
├─"ascading"
├─STRONG
│ └─"S"
├─"tyle"
├─STRONG
│ └─"S"
└─"heets"

Action: Analyzing a DOM

Using DOM Inspector

To analyze a DOM, you need special software. You can use Mozilla's DOM Inspector (DOMi) add-on to analyze a DOM. You simply need to install the add-on (see More details below).

  1. Use your Mozilla browser to open your sample HTML document.
  2. From your browser's menu bar, choose Tools > DOM Inspector, or Tools > Web Development > DOM Inspector.
    More details

    If your Mozilla browser does not have DOMi, you can install it from the Add-ons site and restart the browser. Then return to this tutorial.

    If you do not want to install DOMi (or you're using a non-Mozilla browser), you can use Web X-Ray Goggles, as described in the next section. Or you can skip this section and go straight to the next page. Skipping this section does not interfere with the rest of the tutorial.

  3. In DOMi, expand your document's nodes by clicking on their arrows.

    Note:  Spacing in your HTML file may cause DOMi to show some empty text nodes, which you can ignore.

    Part of the result might look like this, depending on which nodes you have expanded:

    │ ▼╴P
    │ │ │ ▼╴STRONG
    │ │ └#text
    │ ├╴#text
    │ ►╴STRONG
    │ │

    When you select any node, you can use DOMi's right-hand pane to find out more about it. For example, when you select a text node, DOMi shows you the text in the right-hand pane.

    When you select an element node, DOMi analyzes it and provides a huge amount of information in its right-hand pane. Style information is just part of the information it provides.

Challenge

In DOMi, click on a STRONG node.

Use DOMi's right-hand pane to find out where the node's color is set to red, and where its appearance is made bolder than normal text.

Possible solution

In the menu above the right-hand pane, choose CSS Rules. You see two items listed, one that references an internal resource and one that references your stylesheet file. The internal resource defines the font-weight property as bolder; your stylesheet defines the color property as red.

Hide solution
See a solution for the challenge.

Using Web X-Ray Goggles

Web X-Ray Goggles shows less information than DOM Inspector, but is simpler to install and use.

  1. Go to the home page for Web X-Ray Goggles.
  2. Drag the bookmarklet link in that page to your browser toolbar.
  3. Open your sample HTML document.
  4. Activate Web X-Ray Goggles by clicking the bookmarklet on the toolbar.
  5. Move your mouse pointer around over your document to see the elements in the document.

What next?

{{ nextPage("/en-US/docs/Web/Guide/CSS/Getting_Started/Cascading_and_inheritance", "Cascading & Inheritance") }}If you took the challenge, you saw that style information from more than one place interacts to create the final style for an element. The next page explains more about these interactions.

Revision Source

<p>{{ CSSTutorialTOC() }}</p>

<p>{{ previousPage("/en-US/docs/Web/Guide/CSS/Getting_Started/Why_use_CSS", "Why use CSS?") }}<span class="seoSummary">This third section of the <a href="/en-US/docs/Web/Guide/CSS/Getting_started" title="en-US/docs/Web/Guide/CSS/Getting_started">CSS Getting Started</a> tutorial explains how CSS works in your browser and the purpose of the Document Object Model (DOM). You'll also learn how to analyze your sample document.</span></p>

<h2 class="clearLeft" id="Information.3A_How_CSS_works">Information: How CSS works</h2>

<p>When a browser displays a document, it must combine the document's content with its style information. It processes the document in two stages:</p>

<ol>
 <li>The browser converts the markup language and the CSS into the&nbsp;<a href="/en-US/docs/DOM" title="/en-US/docs/HTML/DOM"><em>DOM</em></a> (Document Object Model). The DOM represents the document in the computer's memory. It combines the document's content with its style.</li>
 <li>The browser displays the contents of the DOM.</li>
</ol>

<p>A markup language uses <em>elements</em> to define the document's structure. You mark an element using <em>tags</em>, which are strings beginning with '<code>&lt;</code>' and ending with '<code>&gt;</code>'. Most elements have paired tags, a start tag and an end tag. For the start tag, place the element name between '<code>&lt;</code>' and '<code>&gt;</code>'. For the end tag, place a '<code>/</code>' after the '<code>&lt;</code>' and before the element name.</p>

<p>Depending on the markup language, some elements have only a start tag, or a single tag where the '<code>/</code>' comes after the element name. An element can also be a container and include other elements between its start tag and end tag. Just remember to always close the tags inside the container.</p>

<p>A DOM has a tree-like structure. Each element, attribute and run of text in the markup language becomes a <em>node</em> in the tree structure. The nodes are defined by their relationship to other DOM nodes. Some elements are parents of child nodes, and child nodes have siblings.</p>

<p>Understanding the DOM helps you design, debug and maintain your CSS, because the DOM is where your CSS and the document's content meet up.</p>

<div class="tuto_example">
<div class="tuto_type">Example</div>

<div class="tuto_type">&nbsp;</div>
In your sample document, the <code>&lt;p&gt;</code> tag and its end tag <code>&lt;/p&gt;</code> create a container:

<pre class="brush:html">
&lt;p&gt;
  &lt;strong&gt;C&lt;/strong&gt;ascading
  &lt;strong&gt;S&lt;/strong&gt;tyle
  &lt;strong&gt;S&lt;/strong&gt;heets
&lt;/p&gt;
</pre>

<p>{{ EmbedLiveSample('Information.3A_How_CSS_works', '', '', '', 'Web/Guide/CSS/Getting_started/How_CSS_works') }}</p>

<p>In the DOM, the corresponding P node is a parent. Its children are the <small>STRONG</small> nodes and the text nodes. The <small>STRONG</small> nodes are themselves parents, with text nodes as their children:</p>

<pre>
<span style="color:black">P</span>
├─<span style="color:black">STRONG</span>
│ └─"<span style="color:black">C</span>"
├─"<span style="color:black">ascading</span>"
├─<span style="color:black">STRONG</span>
│ └─"<span style="color:black">S</span>"
├─"<span style="color:black">tyle</span>"
├─<span style="color:black">STRONG</span>
│ └─"<span style="color:black">S</span>"
└─"<span style="color:black">heets</span>"</pre>
</div>

<h2 id="Action.3A_Analyzing_a_DOM">Action: Analyzing a DOM</h2>

<h3 id="Using_DOM_Inspector">Using DOM&nbsp;Inspector</h3>

<p>To analyze a DOM, you need special software. You can use Mozilla's <a href="/en/DOM_Inspector" title="en/DOM_Inspector">DOM Inspector</a> (DOMi) add-on to analyze a DOM. You simply need to install the add-on (see More details below).</p>

<ol>
 <li>Use your Mozilla browser to open your sample HTML document.</li>
 <li>From your browser's menu bar, choose <strong>Tools &gt; DOM Inspector</strong>, or <strong>Tools &gt; Web Development &gt; DOM Inspector</strong>.
  <div class="tuto_details">
  <div class="tuto_type">More details</div>

  <p>If your Mozilla browser does not have DOMi, you can <a class="link-https" href="https://addons.mozilla.org/en-US/firefox/addon/6622/" title="https://addons.mozilla.org/en-US/firefox/addon/6622/">install it from the Add-ons site</a> and restart the browser. Then return to this tutorial.</p>

  <p>If you do not want to install DOMi (or you're using a non-Mozilla browser), you can use Web X-Ray Goggles, as described in the next section. Or you can skip this section and go straight to the next page. Skipping this section does not interfere with the rest of the tutorial.</p>
  </div>
 </li>
 <li>In DOMi, expand your document's nodes by clicking on their arrows.
  <p><strong>Note:&nbsp;</strong> Spacing in your HTML file may cause DOMi to show some empty text nodes, which you can ignore.</p>

  <p>Part of the result might look like this, depending on which nodes you have expanded:</p>

  <pre>
│ ▼╴<span style="color:black">P</span>
│ │ │ ▼╴<span style="color:black">STRONG</span>
│ │ └<span style="color:darkblue">#text</span>
│ ├╴<span style="color:darkblue">#text</span>
│ ►╴<span style="color:black">STRONG</span>
│ │</pre>

  <p>When you select any node, you can use DOMi's right-hand pane to find out more about it. For example, when you select a text node, DOMi shows you the text in the right-hand pane.</p>

  <p>When you select an element node, DOMi analyzes it and provides a huge amount of information in its right-hand pane. Style information is just part of the information it provides.</p>
 </li>
</ol>

<div class="tuto_example">
<div class="tuto_type">Challenge</div>

<p>In DOMi, click on a <small>STRONG</small> node.</p>

<p>Use DOMi's right-hand pane to find out where the node's color is set to red, and where its appearance is made bolder than normal text.</p>

<div class="tuto_details" id="tutochallenge">
<div class="tuto_type">Possible solution</div>

<p>In the menu above the right-hand pane, choose <strong>CSS Rules</strong>. You see two items listed, one that references an internal resource and one that references your stylesheet file. The internal resource defines the <strong>font-weight</strong> property as <code>bolder</code>; your stylesheet defines the <strong>color</strong> property as <code>red</code>.</p>
<a class="hideAnswer" href="#challenge">Hide solution</a></div>
<a href="#tutochallenge" title="Display a possible solution for the challenge">See a solution for the challenge.</a></div>

<h3 id="Using_Web_X-Ray_Goggles">Using Web X-Ray Goggles</h3>

<p><a class="link-https" href="https://goggles.webmaker.org/" title="https://secure.toolness.com/webxray/">Web X-Ray Goggles </a>shows less information than DOM&nbsp;Inspector, but is simpler to install and use.</p>

<ol>
 <li>Go to the home page for <a class="link-https" href="https://goggles.webmaker.org/" title="https://secure.toolness.com/webxray/">Web X-Ray Goggles</a>.</li>
 <li>Drag the bookmarklet link in that page to your browser toolbar.</li>
 <li>Open your sample HTML&nbsp;document.</li>
 <li>Activate Web X-Ray Goggles by clicking the bookmarklet on the toolbar.</li>
 <li>Move your mouse pointer around over your document to see the elements in the document.</li>
</ol>

<h2 id="What_next.3F">What next?</h2>

<p>{{ nextPage("/en-US/docs/Web/Guide/CSS/Getting_Started/Cascading_and_inheritance", "Cascading &amp; Inheritance") }}If you took the challenge, you saw that style information from more than one place interacts to create the final style for an element. The <a href="/en-US/docs/Web/Guide/CSS/Getting_Started/Cascading_and_inheritance" title="/en-US/docs/Web/Guide/CSS/Getting_Started/Cascading_and_inheritance">next page</a> explains more about these interactions.</p>
Revert to this revision