DEV Community

Cover image for How to Preview and Debug HTML Online Without Installing Anything
Tusing1
Tusing1

Posted on

How to Preview and Debug HTML Online Without Installing Anything

You do not always need a full local development environment to answer the first question about a webpage: why is this HTML not working?

A browser-based editor can be enough to reproduce a problem, reduce it to a small example and test a correction. This is especially useful on a borrowed computer, a phone or a machine where installing software is not practical.

I maintain HTML Online Viewer, a browser workspace for editing and previewing HTML side by side. Building and testing it helped me turn my own debugging routine into the workflow below. This article was prepared with AI assistance, then reviewed and edited against working examples.

Start with one portable HTML file

When a problem involves HTML, CSS and a little JavaScript, put all three into one document first. That removes file paths, build tools and dependency setup from the investigation.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Button test</title>
    <style>
      button {
        padding: 0.75rem 1rem;
      }
    </style>
  </head>
  <body>
    <button id="greet">Show greeting</button>
    <p id="message"></p>

    <script>
      document.querySelector('#greet').addEventListener('click', () => {
        document.querySelector('#message').textContent = 'Hello!';
      });
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

If this self-contained version works, the original bug may be in a missing file, an incorrect path, a build step or the order in which scripts load. If it still fails, the problem is now small enough to inspect directly.

Use the smallest reproducible example

Consider this deliberately broken page:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Reading list</title>
    <style>
      .card { border: 1px solid #bbb; padding: 1rem; }
      .card button { background: navy; color: white; }
    </style>
  </head>
  <body>
    <main class="card">
      <h1>Reading list</h1>
      <p id="status">No book selected<p>
      <button id="choose">Choose a book</button>
    </main>

    <script>
      document.querySelector('#chosen').addEventListener('click', () => {
        document.querySelector('#status').textContent = 'HTML for Everyone';
      });
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

It contains two separate mistakes:

  1. The paragraph ends with another opening <p> instead of </p>.
  2. JavaScript searches for #chosen, but the button's ID is choose.

The browser may repair the paragraph automatically, so the page can look almost correct. The button still fails because document.querySelector('#chosen') returns null.

Do not keep adding code while debugging. Remove unrelated markup until the failure is easy to reproduce. A ten-line example that still fails is more useful than a thousand-line page that fails for several reasons.

Check HTML structure before styling

Browsers are forgiving. That is helpful for visitors, but it can hide mistakes from developers.

Before changing CSS, check:

  • opening and closing tags;
  • accidentally nested forms, links or buttons;
  • duplicate IDs;
  • whether content belongs inside <head> or <body>;
  • whether important elements are actually present;
  • whether quoted attribute values are closed.

Formatting the document can make nesting errors easier to see. Validation can point out invalid structure, but treat its messages as evidence to investigate rather than instructions to apply blindly.

The paragraph above should be:

<p id="status">No book selected</p>
Enter fullscreen mode Exit fullscreen mode

Test selectors one boundary at a time

When JavaScript does nothing, verify the boundary between the HTML and the script.

const button = document.querySelector('#choose');
const status = document.querySelector('#status');

console.log({ button, status });
Enter fullscreen mode Exit fullscreen mode

If either value is null, fix the selector or markup before inspecting the rest of the function. Then add the interaction:

button.addEventListener('click', () => {
  status.textContent = 'HTML for Everyone';
});
Enter fullscreen mode Exit fullscreen mode

Also confirm that the script runs after the required HTML exists. A script at the end of <body> works for this small example. A script in <head> should normally use defer or deliberately wait for the document.

Read the first useful error

One failure can produce several console messages. Start with the earliest message that points to your own code.

For the broken example, a browser may report an error similar to:

Cannot read properties of null (reading 'addEventListener')
Enter fullscreen mode Exit fullscreen mode

Translate it into plain language:

  • addEventListener was called;
  • the value before it was null;
  • the selector did not find an element.

Fix that cause, reload and check again. Later messages may disappear automatically.

Do not paste secrets, private source code or customer data into a third-party editor or an AI debugging prompt. Reduce the bug with invented data instead.

Check missing assets and relative paths

An online preview has its own document location. Relative paths from a project on your computer may not exist there:

<img src="../images/team.jpg" alt="Our team">
<script src="./scripts/app.js"></script>
Enter fullscreen mode Exit fullscreen mode

For a portable test, either:

  • include CSS and JavaScript directly in the HTML;
  • use a public test asset that you control;
  • import the related files if the editor supports them; or
  • replace the asset temporarily and test the layout's missing-image state.

If the portable version works, restore the real project paths only after returning to the project's actual folder structure.

Test phone, tablet and desktop widths

A page can be correct at one width and unusable at another. Check at least a narrow phone, a tablet and a desktop layout.

Look for:

  • horizontal scrolling;
  • text or controls extending outside a card;
  • fixed widths wider than the viewport;
  • images without max-width: 100%;
  • buttons that are too small to tap;
  • long URLs or code that refuse to wrap.

A useful baseline is:

* {
  box-sizing: border-box;
}

img {
  max-width: 100%;
  height: auto;
}

.card {
  width: min(100% - 2rem, 44rem);
  margin-inline: auto;
}
Enter fullscreen mode Exit fullscreen mode

Do not shrink everything until it fits. Find the element causing the overflow and correct its layout rule.

Preserve the exact test case

Once the fix works, export or save the self-contained HTML file. Give it a name that describes the behaviour, such as:

button-selector-fixed.html
Enter fullscreen mode Exit fullscreen mode

That file becomes a regression example. If the same feature breaks later, you already have a known-good version for comparison.

Import and export are also useful when a long document should not be copied through the clipboard. Confirm that an exported file opens correctly in a normal browser before calling the test complete.

Know when an online HTML preview is not enough

A browser-only editor is useful for client-side HTML, CSS and JavaScript. It is not a replacement for every development environment.

Move to the appropriate local or hosted environment when the example needs:

  • Node.js or another server runtime;
  • PHP, Python, Java or database code;
  • private packages or environment variables;
  • authentication callbacks;
  • unrestricted cross-origin requests;
  • a framework build pipeline;
  • filesystem or operating-system access.

The preview should help identify which boundary the problem crosses. It should not pretend to execute a backend that does not exist.

A repeatable browser-only debugging loop

Use this short sequence:

  1. Copy the failing feature into one self-contained HTML file.
  2. Remove unrelated code while keeping the failure reproducible.
  3. Format and inspect the document structure.
  4. Verify selectors and required elements.
  5. Read the first useful error from your own code.
  6. Test missing assets and narrow screens.
  7. Make one small change and preview again.
  8. Export the known-good example.
  9. Retest the fix inside the real project.

The corrected example is:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Reading list</title>
    <style>
      * { box-sizing: border-box; }
      .card {
        width: min(100% - 2rem, 44rem);
        margin: 2rem auto;
        border: 1px solid #bbb;
        padding: 1rem;
      }
      .card button { background: navy; color: white; padding: 0.75rem 1rem; }
    </style>
  </head>
  <body>
    <main class="card">
      <h1>Reading list</h1>
      <p id="status">No book selected</p>
      <button id="choose" type="button">Choose a book</button>
    </main>

    <script>
      const button = document.querySelector('#choose');
      const status = document.querySelector('#status');

      button.addEventListener('click', () => {
        status.textContent = 'HTML for Everyone';
      });
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

You can paste this example into any standards-based HTML preview. If you want to try the workflow in the tool I maintain, open HTML Online Viewer, change one selector deliberately and observe the failure before applying the fix.

What is the first thing you check when a webpage renders correctly but its JavaScript does nothing?

Top comments (0)