Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 139 ยท Getting Started

Inspecting CSS with DevTools

Use browser DevTools to see which CSS rules apply to an element, why declarations are crossed out, edit styles live, read the box model and force hover states.

Sooner or later, every CSS developer writes a rule that "doesn't work". The color doesn't change, the margin is the wrong size, or a style appears out of nowhere. Guessing and reloading is a slow way to find out why. The browser already knows exactly which rules apply to every element, which ones lost and why, and it will show you if you ask. The tool for asking is DevTools.

You may have met DevTools in the HTML course, in Viewing Source and Using DevTools. This lesson focuses on the CSS side: the Styles and Computed panes, crossed-out declarations, live editing, the box model diagram and forcing states like :hover. Every major browser has these tools. The examples below describe Chromium-based browsers (Chrome, Edge, Opera, Brave) and Firefox; names and positions differ slightly between them, and between versions, but the ideas are the same everywhere.

Opening DevTools on an element

The fastest way in is to right-click an element and choose Inspect (in Firefox, Inspect, or Inspect Element in older versions). DevTools opens with that element already selected. Other ways to open it:

  • F12, or Ctrl + Shift + I on Windows and Linux, or Cmd + Option + I on macOS.
  • Ctrl + Shift + C (Cmd + Shift + C on macOS) turns on the element picker: hover anything on the page and click to select it.

In Safari, first turn on the developer features in the settings (the Advanced section), then use the Develop menu.

The examples on this site run inside a frame, and DevTools can inspect those too. Right-click inside any preview and choose Inspect, and you'll land on the element inside the example.

Something to inspect

Inspect the first paragraph and keep DevTools open while you read the next section.

The Styles pane

When you select an element, the Elements panel (called Inspector in Firefox) shows the HTML tree on one side and, next to it, the Styles pane (Firefox calls it Rules). This lists every rule that matches the selected element, with the most important one at the top:

  • element.style (Firefox: element) at the very top shows inline styles from the style attribute. You can also click into it to add new inline declarations.
  • Matching rules follow, ordered by how strongly they win in the cascade: a later or more specific rule appears above the ones it beats. Each rule shows its source on the right, such as a file name and line number, or <style> for internal styles. Click it to jump to the source.
  • The browser's defaults are labeled user agent stylesheet in Chromium. (Firefox hides browser styles by default; you can turn them on in the DevTools settings.) These are why a heading is bold or a list is indented before you write any CSS.
  • Inherited rules come last, in sections such as Inherited from body. Only inheritable properties, like color and font-family, are shown there. Inheritance explains which properties inherit.

For the paragraph you inspected, you should see the two .intro rules above the p rule, then (in Chromium) a short user agent rule for p with its default margins.

Crossed-out declarations

Look closely and you'll see some declarations struck through. A line through a declaration means one of two things:

  • It was overridden. Another declaration for the same property won the cascade. Here, color: gray from the p rule is crossed out because .intro sets color too and is more specific. The winning declaration is the one not crossed out, higher up the list.
  • It is invalid. The browser couldn't understand it, so it ignored it. Invalid declarations are struck through and marked with a warning icon. Hover it for a hint such as "Invalid property value". In the example, font-size: 22pz is invalid (pz is not a unit), which is why the paragraph keeps font-size: 18px.

Both browsers also flag declarations that are valid but have no effect in context, such as width on an inline element. Firefox greys them out; Chromium shows an info icon. Hover the icon for an explanation of why the property isn't working and what would fix it. These hints catch many classic mistakes.

The first question for any "my CSS isn't working" bug: does the rule appear in the Styles pane at all? If it doesn't, the selector doesn't match the element (check spelling and class names) or the stylesheet didn't load. If it appears but is crossed out, something else wins, and the declaration above it tells you what.

Editing styles live

Everything in the Styles pane is editable, and changes apply instantly:

  • Click a value to edit it. With a number selected, the Up and Down arrow keys change it by 1; hold Shift for steps of 10, or Alt (Option on macOS) for steps of 0.1.
  • Click a property name to rename it, for example to fix a typo.
  • Click inside a rule's braces after the last declaration to add a new one. Start typing and DevTools suggests property names and values.
  • Toggle a declaration with the checkbox that appears when you hover it. This is the quickest way to answer "what does this line actually do?"
  • Click a color swatch to open a color picker, with contrast information in Chromium.
  • Add a new rule with the + button, or use .cls to add and remove classes on the selected element.
Tune this card live

Inspect the card and click the 12px padding value, then press Up a few times. Try the same with border-radius, toggle the box-shadow on and off, and add a border: 1px solid #ccd; declaration.

Live edits are temporary. Everything you change in DevTools disappears when the page reloads. Once you're happy, copy the changes into your real stylesheet. Both browsers have a Changes view that lists what you edited, which makes this easier. Chromium can also save edits directly to files on disk through its Workspaces feature.

The Computed pane

The Styles pane shows rules. The Computed pane shows results: the final value of every property on the selected element, after the cascade, inheritance and unit conversion are done. Relative values are resolved, so 2em shows up as a pixel value such as 32px.

Use it when you need to know the actual value, not how it got there. Find a property with the filter box, then expand it (click the arrow) to see every declaration that tried to set it, with the winner first. Clicking a source jumps back to the rule in the Styles pane.

By default only properties that something has set are listed. Tick Show all to see every property, including those at their initial values.

The box model diagram

Every element is a rectangular box made of four layers: content, padding, border and margin. You'll learn the box model properly later, but DevTools shows it from day one. In Chromium, the diagram appears in the Computed pane (and at the bottom of the Styles pane); in Firefox, in the Layout pane.

The diagram is a set of nested rectangles, labeled with the size of each layer on each side, and the content size in the middle. Hover any layer in the diagram, or any element in the HTML tree, and the browser highlights it on the page, usually with the content in blue, padding in green, border in yellow and margin in orange. You can double-click numbers in the diagram to edit them, too.

See the box model

Inspect the badge and compare the diagram with the CSS: 10px padding top and bottom, 20px left and right, a 4px border, and 24px margin all around. Then hover the paragraph: its 40px top margin shows up as an orange band above it. When two elements are mysteriously far apart, this is how you find out which margin or padding is responsible.

Next to the box model, the Layout tools (in both browsers) add overlays for Flexbox and Grid containers. You'll use them in the Flexbox and Grid chapters.

Forcing states like :hover

Some styles only apply in a particular state: when the pointer is over an element, when it has focus, or while it's being clicked. Those are hard to inspect, because moving the mouse to DevTools ends the hover.

The solution is to force the state. In the Styles (Rules) pane, click the :hov button and tick the states you want, such as :hover, :focus, :focus-visible or :active. The element now behaves as if it were in that state until you untick it, and the matching rules appear in the Styles pane, ready to edit. In Chromium you can also right-click an element in the HTML tree and choose Force state.

Force the hover state

Inspect the button, open :hov and tick :hover. The .button:hover rule appears in the Styles pane, and the button stays dark even when your mouse is elsewhere. Now edit its box-shadow live, then try forcing :focus-visible to see the focus ring. You'll learn all about these states in Pseudo-classes.

A debugging routine

When a style doesn't behave, work through these steps in DevTools:

  1. Select the element you expect to be styled. Make sure it's really the element you think it is, not a parent or a child.
  2. Find the rule in the Styles pane. If it's missing, the selector doesn't match or the file didn't load.
  3. Look for strikethroughs. Overridden? Find the winner above it. Invalid? Check the warning icon for a typo.
  4. Check for "no effect" hints that explain why a valid property is ignored.
  5. Check the Computed pane for the final value, and the box model for unexpected spacing.
  6. Experiment live until it works, then copy the fix into your code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A declaration in the Styles pane is crossed out with no warning icon. What does that usually mean?

    A plain strikethrough means overridden. Invalid declarations are crossed out too, but they also carry a warning icon.
  2. 2. Your new rule doesn't appear in the Styles pane at all. What is the most likely cause?

    Overridden rules are still listed, just crossed out. A missing rule means it never matched the selected element.
  3. 3. Which pane shows the final value of font-size after the cascade and unit conversion?

    The Computed pane lists resolved final values, and you can expand each one to see which declaration set it.
  4. 4. How can you inspect the styles a button gets only while hovered?

    Forcing a state keeps the element in that state while you inspect and edit the matching rules.
  5. 5. You fixed a layout by editing values in DevTools. What happens when you reload the page?

    DevTools edits are temporary. Copy them into your code, using the Changes view to see what you edited (or set up a Workspace in Chromium).

Summary

  • Right-click any element and choose Inspect to open DevTools with it selected.
  • The Styles (Firefox: Rules) pane lists every matching rule, strongest first, with its source.
  • Crossed-out declarations were overridden; with a warning icon, they're invalid. Hints explain valid properties that have no effect.
  • Edit values, toggle declarations and add rules live, but copy the changes into your code, because edits vanish on reload.
  • The Computed pane shows final values; the box model diagram shows content, padding, border and margin.
  • Use :hov to force states like :hover and :focus-visible.

You now have everything you need to write, organize and debug CSS. Next: Selector Basics, the start of a chapter on choosing exactly the elements you want to style.