Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

DevTools: CSS inspector, Computed styles, and the box model

Find which CSS declarations affect an element, read its property values and dimensions, and test a forced pseudo-class state and temporary style edits.

Version: Browser DevTools workflow; UI and feature availability vary by browser and version

Syntax

DevTools is a browser interface, not CSS syntax. The available controls and the names and locations of panels depend on the browser and its DevTools version, not on the CSS specification version. Typical workflow: Inspect an element → Rules or Styles → Computed → box model. To test a state rule such as `.demo:hover { background-color: #ffe8a3; }`, force `:hover` in the inspector.

DevTools are the browser's developer tools, not CSS syntax. Right-click an element and choose Inspect, or select it in the DOM tree. In Rules or Styles, look for matching rules and relevant inherited styles. A crossed-out declaration can mean it lost in the cascade; also check whether the tool is flagging an error.

Use Computed to see property values for the selected element rather than just the text written in the stylesheet. Do not equate the panel with the formal CSS computed-value stage: some displayed values may already reflect layout. Panel names and locations vary by browser.

The box-model diagram shows content, padding, border, and margin. With box-sizing: content-box, width sets the content width. To get the width from one outer border edge to the other, add the left and right padding and borders, but leave out margins.

Force :hover in the inspector to test matching styles without keeping the pointer over the element. Edit or disable a declaration in Rules or Styles to see the effect immediately. An ordinary live edit does not change the source file and disappears on reload. To keep the change, apply it to the source file or explicitly use a save feature if the tool provides one.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Inspect the CSS box model</title>
  <style id="demo-styles">
    .demo {
      display: block;
      box-sizing: content-box;
      width: 160px;
      padding: 12px;
      border: 2px solid #333;
      margin: 16px;
      background-color: #e7f0ff;
    }

    .demo:hover {
      background-color: #ffe8a3;
    }
  </style>
</head>
<body>
  <div class="demo" role="img" aria-label="Demo box"></div>
  <p id="measurements"></p>
  <script>
    // Measure isolated copies so the visible box stays available for inspection.
    const css = document.getElementById('demo-styles').textContent;
    const source = `<!doctype html><style>${css}</style><div class="demo"></div>`;
    const report = document.getElementById('measurements');

    function loadCopy() {
      return new Promise(resolve => {
        const frame = document.createElement('iframe');
        frame.setAttribute('aria-hidden', 'true');
        frame.style.cssText = 'position:fixed;left:-1000px;top:0;width:400px;height:160px;visibility:hidden;pointer-events:none';
        frame.addEventListener('load', () => resolve(frame), { once: true });
        frame.srcdoc = source;
        document.body.append(frame);
      });
    }

    function measure(frame) {
      const box = frame.contentDocument.querySelector('.demo');
      const style = frame.contentWindow.getComputedStyle(box);
      const sides = ['Top', 'Right', 'Bottom', 'Left'];
      function uniform(property, suffix = '') {
        const values = sides.map(side => style[property + side + suffix]);
        if (!values.every(value => value === values[0])) {
          throw new Error(`${property} differs by side`);
        }
        return values[0];
      }
      return {
        background: '#' + style.backgroundColor.match(/\d+/g)
          .map(component => Number(component).toString(16).padStart(2, '0')).join(''),
        content: style.width,
        padding: uniform('padding'),
        border: uniform('border', 'Width'),
        margin: uniform('margin'),
        outer: `${box.getBoundingClientRect().width}px`
      };
    }

    async function showMeasurements() {
      const frame = await loadCopy();
      let initial, hover, edited;
      try {
        initial = measure(frame);
        const [boxRule, hoverRule] = frame.contentDocument.styleSheets[0].cssRules;
        const originalSelector = hoverRule.selectorText;
        // Page scripts cannot force a DevTools state. Match the same rule
        // at the same specificity in this copy to measure its effect.
        hoverRule.selectorText = '.demo.demo';
        hover = measure(frame);
        hoverRule.selectorText = originalSelector;
        if (hover.background === initial.background ||
            JSON.stringify({ ...hover, background: initial.background }) !== JSON.stringify(initial)) {
          throw new Error('The hover rule did not change only the background');
        }
        boxRule.style.padding = '20px';
        edited = measure(frame);
      } finally {
        frame.remove();
      }

      // A fresh document reads the original stylesheet, not the live edit.
      const fresh = await loadCopy();
      let restored;
      try {
        restored = measure(fresh);
      } finally {
        fresh.remove();
      }
      if (JSON.stringify(restored) !== JSON.stringify(initial)) {
        throw new Error('A fresh load did not restore the original measurements');
      }

      report.textContent = [
        `Expected in a blank page with no other author styles: with the pointer outside the box and no forced state, the background is ${initial.background}.`,
        `The content is ${initial.content} wide, with ${initial.padding} padding, a ${initial.border} border, and a ${initial.margin} margin on each side.`,
        `The border-to-border width is ${initial.outer}, excluding margin.`,
        `Forcing :hover changes the background to ${hover.background} without changing the width.`,
        `Editing padding to ${edited.padding} in Rules/Styles increases the border-to-border width to ${edited.outer}.`,
        `Reloading without saving or overrides restores ${restored.padding} padding and a ${restored.outer} border-to-border width.`
      ].join(' ');
    }

    showMeasurements().catch(error => {
      report.textContent = `Could not produce the box measurements: ${error.message}.`;
    });
  </script>
</body>
</html>

Result

Expected in a blank page with no other author styles: with the pointer outside the box and no forced state, the background is #e7f0ff. The content is 160px wide, with 12px padding, a 2px border, and a 16px margin on each side. The border-to-border width is 188px, excluding margin. Forcing :hover changes the background to #ffe8a3 without changing the width. Editing padding to 20px in Rules/Styles increases the border-to-border width to 204px. Reloading without saving or overrides restores 12px padding and a 188px border-to-border width.

Open the HTML example in a browser. Select the demo box in the inspector, move the pointer away, and compare Computed with the box-model diagram. Forcing `:hover` changes only the background. Turn off the forced state, edit `padding` from `12px` to `20px`, and check that the border-to-border width grows from `188px` to `204px`, excluding margins. Reload without saving the edit or using persistent overrides: `12px` and `188px` return.

Related lessons

  • CSS · Lesson 8 · Basic · Version: 3

Sources