Implementing your own CSS Layouter

I've written a lot of websites during the last nearly two decades, and know pretty much of HTML, CSS, JavaScript and other web technologies because of that. But there is usually always a situation where I have to scratch my head and wonder how or why a browser is lay-outing a specific part of a website the way it does. And confusingly, why in the world other browsers are doing it in the exact same, weired way as well.

I recently had to implement a big part of CSS 2's visual formatting model for a new component I wrote, in C++. And while implementing this, I received answers to a lot of those questions. After I had a basically working HTML layout engine, it did exactly the same strange things in many situations as the browsers do. But with the difference that I now understood why: In most cases, because the implementation forced it this way. And the implementation seems to be the way it was, because it was the simplest way to make it work.

While testing my implementation, I also found bugs in some browsers layout engines: For example the original Opera apparently doesn't correctly layout blocks with left or right float in justified text flows. A bug my implementation funnily also had in a very similar way.

Anyway, I now understand much more how browsers do what they do. Not sure if this will be helpful when creating websites in the future, but it was definitely interesting. I can at least recommend to read through the specification one day, if you have nothing better to do.

three comments, already:

At least it is only boxes :)
I remember some awkward moments with my code when I tried implementing parts of XSL:FO (which, for the most part, works similar in that respect). Floats can be particularly nasty, especially when they aggregate in a place… (non-floating stuff is deceptively simple at first, but becomes trouble once floats enter the arena).
xaos - 23 06 15 - 12:20

Are you building a new gui framework for irrlicht?

If so you should look into librocket:
Kai Mast () (link) - 23 06 15 - 15:54

Float is tricky, yes :)
No, its for an internal project for a client. Didn’t try libRocket, but looks interesting!
niko - 24 06 15 - 09:39

Remember personal info?
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.