Debugging CSS with DevTools

Beginner · 23 min

A working method for finding why a style is not applying, using the Styles and Computed panes, forced states, layout overlays and the Rendering tab.

Before thisCSS Modules and CSS-in-JS

Kevin Powell · 23 min

Debugging CSS with DevTools

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Debugging CSS is answering two questions: which rules apply to this element, and why did the one I wanted lose. The Styles pane shows the first and the Computed pane shows the second.
  • A struck-through declaration lost the cascade or is overridden. A warning icon beside a declaration means the property or value is invalid or has no effect where it is used.
  • Force `:hover`, `:focus` and `:active` from the Styles pane, rather than trying to hold the mouse still while inspecting.
  • To find what causes horizontal overflow, outline everything (`* { outline: 1px solid red }`) or inspect candidates and check which box extends past the viewport. The culprit is rarely the element you first suspect.
  • The Rendering tab emulates user preferences (`prefers-reduced-motion`, `prefers-color-scheme`, forced colours) and print media, so you can test those branches of your CSS without changing your operating system.

From guessing to looking

When a style does not work, the usual first move is to change something and reload. That works for small problems and fails for the rest. The browser already knows exactly why the element looks the way it does: it computed every property from every matching rule. DevTools is a window into that computation. Debugging gets faster once you treat the browser as a witness.

This lesson names the panes and the moves. The names differ slightly between Chrome, Edge and Firefox, but the features are the same.

The method

  1. Inspect the element (right-click, Inspect). Check you picked the element you meant, not its parent or a pseudo-element.
  2. Read the Styles pane. It lists every matching rule, most specific first. A struck-through declaration lost, either to a more specific rule, a later one, a higher cascade layer or !important, or it was invalid.
  3. Open the Computed pane. It shows the final value of every property and, when you expand one, which rule supplied it. This is the quickest answer to “why is this blue”.
  4. Heed the warnings. A yellow icon beside a declaration marks something that is invalid or has no effect. top on a position: static element, width on an inline element, flex-direction on something that is not a flex container are all flagged, with a tooltip explaining.
  5. Change values live, then copy what worked back to the source. Arrow keys nudge a number, and Shift+arrow moves by ten. Edits are lost on reload, so the Changes tab (Chrome) lists what you altered.

The cascade questions here are exactly the ones from specificity and the cascade. DevTools shows @layer names beside rules in Chrome and Firefox, which makes the layer ordering from cascade layers visible.

Forcing states

Hover and focus are hard to inspect, because the mouse has to leave the page to reach the inspector. The Styles pane has a toggle (:hov) that forces :hover, :active, :focus, :focus-visible and :visited on the selected element. Use it to check hover styles, focus rings and the effect of :focus-within on parents.

Layout overlays

Flexbox, Grid and Container Query containers get small badges in the Elements panel. Clicking one draws an overlay on the page: grid lines and track sizes, flex item boundaries and free space, or the container’s size. For grid, the Layout pane can show line numbers and area names. These are the fastest way to find out whether your tracks are the size you think. They also help to spot a gap that you assumed was zero.

Hunting overflow

Horizontal scrolling on mobile almost always means something is wider than the viewport. Some ways to find it:

  • Outline everything. * { outline: 1px solid red; } draws a box around every element without changing layout (unlike border). Scroll sideways until you see the box that sticks out.
  • Delete and watch. Remove sections of the DOM in the Elements panel until the scrollbar goes away.
  • Check usual suspects. A fixed width in px, 100vw plus a vertical scrollbar, long unbreakable strings, negative margins, and transforms that move content off-screen.

Firefox marks elements that cause scrolling with a “scroll” badge, which helps for exactly this. The overflow and scroll containers lesson explains the rules, and display and normal flow covers why block elements fill the width by default.

The Rendering tab: emulating the environment

Chrome’s Rendering tab (open the command menu with Ctrl/Cmd+Shift+P and type “Rendering”) emulates things you would otherwise need a different computer for:

  • Emulate CSS media features: prefers-color-scheme, prefers-reduced-motion, prefers-contrast, forced-colors. This is how you test the branches described in user preference queries and accessible CSS.
  • Emulate CSS media type: print, so you can see the page as the print stylesheet will style it before wasting paper. See print stylesheets.
  • Paint flashing and Layout Shift Regions, for animation performance and layout shift.
  • Device toolbar (the phone icon) to try viewport sizes and touch.

Typical culprits, quickly

Symptom First thing to check
Style does not apply at all Selector typo or wrong element, then a struck-through declaration
Applies in one place, not another Specificity or source order, shown in Styles
z-index has no effect The element’s stacking context and whether it is positioned
Margin is “missing” Margin collapsing in the box diagram of the Computed pane
Weird width box-sizing and the box model diagram, see the box model
Variable shows wrong value The Computed pane value of the custom property on that element

For performance and accessibility there are further tools (the Performance panel and the Accessibility pane), which come up in the following lessons.

What the video covers

Kevin Powell’s “How I find and debug issues in my CSS” (April 2024, about 23 minutes) has chapters on DevTools basics (1:00), typos and invalid properties and values (3:40), valid properties in places they will not work (5:08), responsiveness, overflows and layout issues (7:42), finding overflows with outlines (9:47) and with background-color (12:00), DevTools features for Grid and Flexbox (12:45), extra visualisations in Firefox (16:20), VisBug (18:12) and Polypane (19:28). That matches the sections above closely. Polypane is a paid browser that he has an affiliate link for, as the description states. The lesson’s table and the Rendering tab sections go beyond the video. I could not read a transcript. His shorter 2025 “CSS Debugging Tips” is in further viewing.

Where this fits

These tools pay off most with the cascade and layout knowledge from the earlier groups. Debugging also covers what the page does for people, not just how it looks. Accessible CSS is next.

Check yourself

  1. 01A declaration in the Styles pane is struck through. What does it mean, and how do you find the rule that beat it?

    Another declaration for the same property won the cascade (higher specificity, later order, a higher-priority layer, or `!important`) or the declaration is invalid. Look up the Styles pane for the unstruck rule for that property, or open the Computed pane, expand the property, and see which selector and file the final value came from.

  2. 02You set `top: 10px` on an element and nothing moves. The Styles pane shows the declaration with a warning icon. What is the likely cause?

    The property has no effect where it is used, for example `top` on an element whose `position` is `static`. Hover the icon: DevTools explains why, and often suggests the fix (here, setting `position`).

  3. 03A page scrolls sideways on mobile but you do not know which element is too wide. What is a quick technique?

    Add `* { outline: 1px solid red; }` (outlines take no space, unlike borders) and look for a box poking past the viewport edge, or remove sections in the Elements panel until the sideways scroll disappears. Common culprits are fixed widths, negative margins, long unbroken text and 100vw combined with a vertical scrollbar.

  4. 04How can you check a page's dark mode or reduced-motion styles without changing your computer's settings?

    Open the Rendering tab in Chrome DevTools (from the command menu or the three-dot menu under More tools) and use the Emulate CSS media feature options, such as `prefers-color-scheme` and `prefers-reduced-motion`. Firefox has similar buttons in the Inspector.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark