When you open a page, the browser doesn't display your file as text — it parses it. It reads the tags, figures out which elements contain which, and builds an in-memory tree called the DOM (Document Object Model). That tree is what actually gets painted, and what JavaScript later manipulates.
This is why a missing closing tag can wreck a layout: the browser guesses where the element ends, and its guess shapes the whole tree. Clean, well-nested markup gives the parser an easy job and a predictable result.