# Web Browser Engineering builds a browser in about 3,000 lines of Python

**Summary:** Web Browser Engineering uses a small Python browser to explain how pages become pixels. The learning project connects the components while keeping its compatibility and safety limits explicit.

- Canonical: https://markhuang.ai/news/web-browser-engineering-python
- Language: en
- Author: [Mark Huang](https://markhuang.ai/about)
- Published: 2026-11-17
- Section: News
- Tags: Web Browser Engineering, Python, Browser Internals, Web Development, Learning
- Source: [Web Browser Engineering](https://browser.engineering/)
- License: https://creativecommons.org/licenses/by-nc/4.0/

---

![A blue paper browser frame opens onto layered page panels and a branching folded-paper structure](https://cdn.markhuang.ai/news/web-browser-engineering-python/hero.webp)

*Conceptual illustration: taking a page apart makes the work behind its appearance easier to see. Generated with imagegen.*

Drawing a colored background behind a paragraph sounds simple. First, though, a browser has to work out how wide the paragraph is, how tall its wrapped lines make it, and where it belongs. In [Web Browser Engineering](https://browser.engineering/), adding that background leads readers to rebuild how their browser arranges a page.

Pavel Panchekha and Chris Harrelson explain how browsers work by having readers construct one in Python. Their [preface puts the final implementation at about 3,000 lines](https://browser.engineering/preface.html). It is a learning browser with substantial limitations, but it connects downloading pages, displaying documents, and running interactive scripts in one project you can follow.

Building the browser lets you see how those pieces depend on each other. A page stops being something that simply appears after you enter an address. You can trace the decisions that turn a document into something visible, then change a decision and examine the result.

## A paragraph has structure before it has a position

The book's [layout chapter](https://browser.engineering/layout.html) supplies a particularly good example. HTML describes a document's structure: a paragraph belongs inside a section, which belongs inside the page. The browser needs a separate representation of how that content occupies space.

The chapter builds a layout tree, a set of connected objects that describe the arrangement of the page. It computes sizes and positions for those objects and then draws them. Nested backgrounds make the need concrete: handling an opening tag and a closing tag separately does not give you the dimensions of the whole element between them.

```mermaid
flowchart LR
    A["HTML document tree"] --> B["Create layout objects"]
    B --> C["Compute sizes and positions"]
    C --> D["Draw the page"]
```

This is the simplified flow taught in that chapter. The distinction also appears in [MDN's explanation of browser rendering](https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/How_browsers_work): parsing creates a representation of the document, layout determines geometry, and painting turns the result into pixels. Full browsers have more work to coordinate, including styles and compositing.

When a page looks wrong, these stages give you somewhere to start. Has the browser misunderstood the document, calculated the wrong position, or drawn the right shape incorrectly? Those are different places to investigate, even when all you initially see is a paragraph that looks wrong.

## The same project eventually has to handle a click

The [table of contents](https://browser.engineering/) takes the project beyond displaying text. Readers add links and form submissions, then JavaScript that changes the DOM and reacts to events. The DOM is the browser's representation of the document that scripts can inspect and modify. The book then introduces cookies and logins, alongside security problems such as cross-site scripting.

Later chapters cover task scheduling, animation, and accessibility, including keyboard input, zooming, and the accessibility tree. That tree exposes the page's meaning to assistive technology. A browser has to make a document usable as well as draw it.

I like the continuity here. Each topic has a place in a program the reader is already building. Networking gets the document; layout arranges it; scripts can change it. You have a reason to understand the connection between chapters, rather than collect definitions of browser terminology.

## A small browser is still a substantial project

The authors estimate that an average chapter takes four to six hours to read, implement, and debug for someone with a few years of programming experience. They also say the advanced chapters are longer. That is an estimate for a particular reader, not a completion promise for someone learning to program at the same time.

One reader raises a practical concern in the [Hacker News discussion](https://news.ycombinator.com/item?id=41848186). Reader austin-cheney suggests separating JavaScript execution from the chapter's DOM and event material. Panchekha replies that it might be best read in two sittings. Even a compact program can take substantial work to understand.

Nor does the line count mean readers implement every dependency. The authors' [account of what the book leaves out](https://browser.engineering/skipped.html) explains that the browser uses DukPy for JavaScript evaluation and Skia for graphics. It also omits more elaborate layout systems such as flexbox and grids. Those omissions leave room to explain how the components fit together.

The preface is explicit about the result: it implements only a sliver of HTML, CSS, and JavaScript, mishandles errors, runs slowly, and is not resilient to malicious inputs. Treat it as a learning project. It does not establish that a short Python implementation can replace an everyday browser.

## Give the browser one question to answer

A useful starting exercise is to take a simple local document with a paragraph and a background, then predict what should happen when its available width changes. Which lines should wrap differently? What should happen to the background's height? Follow the layout chapter's code and compare your prediction with the output.

My suggested exercise follows the approach I described in [Finished the Tutorial. Still Can't Build?](/blog/finished-the-tutorial-still-cant-build): change one requirement and investigate the result, instead of checking only whether your work matches the example.

The question is deliberately small. Explaining why a paragraph's background grows when its text wraps connects a visible effect to document structure, measurement, and drawing. Try to follow that chain through your own code and explain why the page changed.
