Skip to content

HTML Editor Preview Media Query Uses Iframe Width: Why Mobile CSS Applies on a Desktop

admin9 min read
HTML editor preview media query uses iframe width: a narrow preview frame inside a wide desktop screen

If a rule inside @media (max-width: 600px) switches on while your desktop browser window is wide, nothing is broken. The HTML editor preview media query uses iframe width: the HTML Code Editor draws your page inside an iframe, and a width-based query is evaluated against that frame’s viewport, not against the whole browser window. A wide monitor with a narrow preview pane therefore matches a “mobile” breakpoint.

The input is an HTML document with inline CSS, and the output is a live preview. Below I show a small page that reports its own width, how to read the numbers before you judge a breakpoint, and how to test the downloaded page separately. I checked the product behavior described here on November 12, 2026.

Narrow preview frame inside a wide desktop screen, illustrating how a media query measures the iframe

Why the HTML editor preview media query uses iframe width

The HTML Code Editor page says its live preview renders the HTML you enter in an iframe using srcdoc. That is a current product description, not a promise about every preview tool. An iframe is a nested browsing context with its own document and its own viewport. Width-based media features such as width and max-width are evaluated against the viewport of the document that contains the stylesheet, as described in Media Queries Level 4 and in the MDN article on viewport concepts.

In practice, your page inside the preview does not know how wide the desktop window is. It only knows how wide its own frame is. If that frame is 600 CSS pixels wide or narrower, (max-width: 600px) matches, even on a large monitor. A CSS pixel is the unit CSS uses for layout, and it is not always one physical screen pixel.

The wording matters too. Calling a rule a “mobile style” describes what you intended when you wrote it. The browser does not detect a phone. It compares one number, the viewport width, with the number in your query. For max-width: 600px, a width of exactly 600px still matches, because the condition includes its boundary, as the MDN media query fundamentals lesson explains.

I would also avoid assuming anything else about the frame. Its width is not a fixed value, and it is not necessarily the outer size of the visible pane. Measure it instead, as the next sections show.

Laptop with a split blue and pink layout showing a breakpoint color change in an HTML preview

A minimal example to paste into the HTML Code Editor

Here is a small page with inline CSS and an inline script. Paste it into the editor’s input pane. The light blue background is the default, and pink means the preview viewport is at or below 600 CSS pixels. The content is fictitious and contains no personal data.

<!doctype html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <title>Breakpoint check</title>
  <style>
    body { font-family: sans-serif; padding: 16px; background: #e0f2fe; }
    @media (max-width: 600px) {
      body { background: #fce7f3; }
    }
  </style>
</head>
<body>
  <h1>Breakpoint check</h1>
  <p id='report'>Measuring...</p>
  <script>
    const query = '(max-width: 600px)';
    const report = document.getElementById('report');
    function update() {
      const matches = window.matchMedia(query).matches;
      report.textContent = 'innerWidth: ' + window.innerWidth + ' CSS px, matches ' + query + ': ' + matches;
    }
    update();
    window.addEventListener('resize', update);
  </script>
</body>
</html>

The stylesheet is inline, so the example needs no external file. The script writes two facts into the paragraph: the width of the window it runs in and whether the exact query matches right now. Because the script runs inside the same iframe as the stylesheet, both facts describe the same viewport that the media query uses.

With a preview pane wider than 600 CSS pixels, expect a blue background and false. With a pane at or below 600, expect pink and true. The product page says inline scripts can run in its sandboxed preview. I would treat that as a current claim about this editor only, and I would only run scripts you wrote yourself.

Measuring gauge held against a preview frame to check its width instead of the desktop window

Measure the preview before judging the breakpoint

Do not judge the breakpoint by the width of your desktop window. Read the numbers the preview document reports about itself. The example prints window.innerWidth, which is the width of the iframe window’s layout viewport in CSS pixels, and the result of window.matchMedia(query).matches, which says whether the document matches that exact query at this moment. MDN documents both: Window.innerWidth and Window.matchMedia().

  1. Paste the example and wait for the preview to update.
  2. Read the reported innerWidth and the true or false value.
  3. Change the available space around the preview, for example by resizing the browser window, then read both values again.
  4. Compare the width with 600. At 600 or less you should see true and a pink background. Above 600 you should see false and light blue.

One detail can cause small disagreements. innerWidth includes a vertical scrollbar when one is present, while document.documentElement.clientWidth excludes it, so the two numbers can differ slightly. If you are testing close to a boundary, trust the matchMedia result for whether the query matches, and use the width only as a guide.

The CSS Editor is a different tool for a different job. It previews your stylesheet on a fixed sample page, so it never sees your own markup and cannot show how your page reacts at a breakpoint. For the code-first workflow it does support, see my guide to using a visual CSS editor with a live preview. When you want your own elements to respond to a query, write the CSS inline in the HTML Code Editor instead.

Test the downloaded page outside the iframe

The preview confirms how your HTML responds inside the editor’s iframe. It does not replace testing the page as a standalone document. To test the breakpoint outside the editor, take the HTML out of the preview and open it on its own.

  1. Copy or download the page from the editor and save it as a local file such as breakpoint.html.
  2. Open that file in your browser as its own page. Now the viewport is the browser tab, not a frame.
  3. Resize the window, or use the browser’s responsive design tools, and watch the reported width and the background color change around 600.
  4. If mobile behavior matters, open the file on a real phone or tablet as well. A narrow desktop viewport does not establish how the device itself behaves.

Keep the viewport meta tag in the page while you test for mobile. Without it, a mobile browser can lay the page out at a wider layout viewport, and a narrow-width query that you expected to match may not. MDN describes this in its reference for the viewport meta element.

The same distinction applies to pages that contain their own frames. A video or widget inside your page is another iframe with its own viewport, which is why making an iframe embed responsive on phones needs rules on the embed itself instead of relying on the page-level query alone.

Why a breakpoint can still look wrong after you measure

Once you know the frame width, most remaining surprises come from a short list of causes.

  • Wrong direction. max-width: 600px applies at 600 and below, while min-width: 600px applies at 600 and above. Swapping them makes a rule appear to fire on the wrong screens.
  • Boundary value. A width of exactly 600 matches max-width: 600px. Test one pixel above and below if the edge matters.
  • Rule order and specificity. A matching query only makes its rules eligible. A later or more specific rule can still win, and the CSS :where specificity article shows one case where a plain element selector beats a lower-specificity rule.
  • Missing viewport meta tag. This affects real mobile devices, not the editor preview, so a page can behave in the preview and still miss the query on a phone.
  • Changed CSS after minifying. If you shrink the stylesheet with the CSS Minifier, keep your readable source and repeat the same width checks on the output.

Also keep the checks separate. A matching query shows that one condition is true at one width. It does not prove the layout is usable, the CSS is valid, or the page works on a real device. The HTML Code Editor has no validator, and the CSS Editor has no linter, so verify by measuring, resizing and testing the standalone file.

Frequently asked questions

Does the HTML Code Editor preview simulate a phone?

No. The preview renders your HTML in an iframe, and a width-based query matches when that frame is narrow enough. It does not emulate a phone’s pixel density, touch input or mobile browser behavior, so test on a real device when mobile behavior matters.

Why does max-width: 600px match when the width is exactly 600?

The condition includes its boundary, so a viewport of 600 CSS pixels matches and 601 does not. If an exact edge matters, test one pixel on each side by resizing the available space and rereading the reported width.

How can I make the preview narrower or wider?

Change the space available around it, for example by resizing the browser window, if the interface allows. Then recheck the width and the matchMedia result printed by the example, because the preview frame size follows its surroundings.

Why can innerWidth and clientWidth show different numbers?

window.innerWidth includes a vertical scrollbar when one is present, while document.documentElement.clientWidth excludes it. A small difference is normal. Near a breakpoint, rely on the matchMedia result to see whether the query actually matches.

My query matched in the preview but not on a real phone. Why?

A common cause is a missing viewport meta tag, which can let a mobile browser use a wider layout viewport. Keep the meta tag in the page, then open the downloaded file on the device and compare the reported width with your query.

Is the CSS Editor preview good for testing my own breakpoint?

Not for your own markup. The CSS Editor applies your CSS to a fixed sample page, so selectors for elements that do not exist there show nothing. Write inline CSS in the HTML Code Editor when you want your own elements to respond.

Does the editor check that my media query is valid?

No. The HTML Code Editor has no validation and the CSS Editor has no linting. Confirm the result by measuring the viewport, reading the matchMedia value and testing the page as a standalone file.

Next steps

Because the HTML editor preview media query uses iframe width, a wide desktop window can still trigger your mobile rules inside the preview. The query measures the frame, and calling a rule a mobile style only describes your intent. Measure the frame, treat the preview as a check of the page inside the editor, and test the downloaded file in a normal browser tab and on a real device. As a next step, paste the example into the editor, note the width it reports, then open the same file standalone and compare the two readings.

Related tools and resources on HTML Editor Online:

Sources and further reading

Try it in your browser

Free online editors with syntax highlighting. Open one and start coding, no account needed.

All Tools