The first part probably isn't too hard, since most web browsers go to great lengths to render non-standard html in a sensible way, I'm not too sure about the second part. I'm guessing the jpeg spec has some variable length space in some kind of file header that the html for the page can be put in to.
I read something similar a while back (I think it was called a Jafar attack) where a clever person worked out how to create a file that was both a valid .gif image and .jar java executable.
Apparently, the jpeg format allows this.
Neat hack.
What is surprising is that web browsers just ignore the 24 bytes of binary data between the start of the file and the start of the HTML.
Edit: Misread your comment...the bytes at the beginning of the file are hidden by CSS (as pointed out by others).
The browser actually picks that "text" up and shows it on page. It's just the html content itself contains some css rule to make that text not visible.