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 1018356 of Debugging CSS

  • Revision slug: Learn/CSS/Introduction_to_CSS/Debugging_CSS
  • Revision title: Debugging CSS
  • Revision id: 1018356
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{draft}}

In the final article of this module, we take a look at the basics of debugging CSS, including exploring the CSS applied to a page, and other tools that can help you find errors in your CSS code.

Prerequisites: Basic computer literacy, basic software installed, basic knowledge of working with files, HTML basics (study Introduction to HTML), and an idea of How CSS works (study the previous articles in this module.)
Objective: To learn the basics of how CSS can be debugged.

Just like with debugging HTML, debugging CSS isn't really very scary compared to debugging a lot of other types of code. We'd recommend that you (re)read debugging isn't scary before continuing.

CSS and debugging

Just like HTML, CSS is permissive (read permissive code before continuing.) In CSS's case, if a declaration is invalid (contains a syntax error, or the browser doesn't support that feature), the browser just ignores it completely and moves on to the next one it finds.

If a selector is invalid, then it doesn't select anything, and the whole rule does nothing — again, the browser just moves on to the next rule.

This is great in a lot of ways — in most cases your content will be shown to your users, even if it isn't styled quite right. But this isn't very helpful when you are trying to debug the problem and you don't even get given any kind of error message to help you find it. This is even more of a pain when the content isn't viewable by your users — perhaps a critical style isn't being applied, resulting in the layout going badly wrong?

Fortunately there are some tools available to help you. Let's look at these now.

Inspecting the DOM and CSS

Nowadays, all web browsers provide developer tools made to help you inspect and understand web pages. Among the various tools they provide, there are two that are available in all browsers: The DOM Inspector and the CSS Editor. We already looked at the DOM Inspector in Debugging HTML and how it can be used to inspect HTML. Here we'll look at this and the CSS Editor, and how to use them together to debug CSS problems.

Note: In the following example, we are using Firefox, but all browsers provide the same kind of tools — they might just be available in slightly different places. Read Discover browser developer tools to find more about accessing them in different browsers.

  1. Open your web browser and load an HTML page (for example this one).
  2. To inspect an element, you have two choices:
    1. Right click on the element and chose "Inspect Element".
    2. Open the global menu on the right of the toolbar and pick "Developer > Inspector".
  3. Now you have access to the DOM tree of your document on the left panel. The right panel displays all of the CSS rules that apply to the selected element in the left panel.
  4. Feel free to fiddle with those tools as they let you change the DOM and update the CSS rules attached to them. For example, change the color of the text from green to red.

The page inspector tool helps you understand what happens, whether it is to find mistakes you've made or understand techniques used by other web developers.

Revision Source

<p>{{draft}}</p>

<p class="summary">In the final article of this module, we take a look at the basics of debugging CSS, including exploring the CSS applied to a page, and other tools that can help you find errors in your CSS code.</p>

<table class="learn-box standard-table">
 <tbody>
  <tr>
   <th scope="row">Prerequisites:</th>
   <td>Basic computer literacy, <a href="https://developer.mozilla.org/en-US/Learn/Getting_started_with_the_web/Installing_basic_software">basic software installed</a>, basic knowledge of <a href="https://developer.mozilla.org/en-US/Learn/Getting_started_with_the_web/Dealing_with_files">working with files</a>, HTML basics (study <a href="/en-US/docs/Learn/HTML/Introduction_to_HTML">Introduction to HTML</a>), and an idea of How CSS works (study the previous articles in <a href="/en-US/docs/Learn/CSS/Introduction_to_CSS">this module</a>.)</td>
  </tr>
  <tr>
   <th scope="row">Objective:</th>
   <td>To learn the basics of how CSS can be debugged.</td>
  </tr>
 </tbody>
</table>

<p>Just like with debugging HTML, debugging CSS isn't really very scary compared to debugging a lot of other types of code. We'd recommend that you (re)read <a href="/en-US/Learn/HTML/Introduction_to_HTML/Debugging_HTML#Debugging_isn%27t_scary">debugging isn't scary</a> before continuing.</p>

<h2>CSS and debugging</h2>

<p>Just like HTML, CSS is <em>permissive</em> (read <a href="/en-US/Learn/HTML/Introduction_to_HTML/Debugging_HTML#Permissive_code">permissive code</a> before continuing.) In CSS's case, if a declaration is invalid (contains a syntax error, or the browser doesn't support that feature), the browser just ignores it completely and moves on to the next one it finds.</p>

<p>If a selector is invalid, then it doesn't select anything, and the whole rule does nothing — again, the browser just moves on to the next rule.</p>

<p>This is great in a lot of ways — in most cases your content will be shown to your users, even if it isn't styled quite right. But this isn't very helpful when you are trying to debug the problem and you don't even get given any kind of error message to help you find it. This is even more of a pain when the content isn't viewable by your users — perhaps a critical style isn't being applied, resulting in the layout going badly wrong?</p>

<p>Fortunately there are some tools available to help you. Let's look at these now.</p>

<h3 id="Inspecting_the_DOM_and_CSS">Inspecting the DOM and CSS</h3>

<p>Nowadays, all web browsers provide developer tools made to help you inspect and understand web pages. Among the various tools they provide, there are two that are available in all browsers: The DOM Inspector and the CSS Editor. We already looked at the DOM Inspector in <a href="/en-US/docs/Learn/HTML/Introduction_to_HTML/Debugging_HTML">Debugging HTML</a> and how it can be used to inspect HTML. Here we'll look at this <em>and</em> the CSS Editor, and how to use them together to debug CSS problems.</p>

<div class="note">
<p><strong>Note</strong>: In the following example, we are using <a href="https://www.mozilla.org/firefox/new">Firefox</a>, but all browsers provide the same kind of tools — they might just be available in slightly different places. Read Discover browser developer tools to find more about accessing them in different browsers.</p>
</div>

<ol>
 <li>Open your web browser and load an HTML page (for example <a href="/en-US/docs/Learn/CSS/Basics">this one</a>).</li>
 <li>To inspect an element, you have two choices:
  <ol>
   <li>Right click on the element and chose "Inspect Element".<br />
    <img alt="" src="https://mdn.mozillademos.org/files/11783/open-dev-tools-1.png" style="height:240px; width:390px" /></li>
   <li>Open the global menu on the right of the toolbar and pick "Developer &gt; Inspector".<br />
    <img alt="" src="https://mdn.mozillademos.org/files/11785/open-dev-tools-2.png" style="height:283px; width:391px" /></li>
  </ol>
 </li>
 <li>Now you have access to the DOM tree of your document on the left panel. The right panel displays all of the CSS rules that apply to the selected element in the left panel.<br />
  <img alt="" src="https://mdn.mozillademos.org/files/11787/dev-tools.png" style="height:241px; width:390px" /></li>
 <li>Feel free to fiddle with those tools as they let you change the DOM and update the CSS rules attached to them. For example, change the color of the text from green to red.</li>
</ol>

<p>The <a href="/en-US/docs/Tools/Page_Inspector">page inspector tool</a> helps you understand what happens, whether it is to find mistakes you've made or understand techniques used by other web developers.</p>
Revert to this revision