I tend to approach this problem using a slightly less clean method.
I set the image inside h1 to put the following inscription:
<div id="header"> <h1>This is the header<img src="img/stackoverflowLogo.png" /></h1> </div>
Then use CSS to place <h1> relative, and an image with position:absolute . This leads to the fact that the image is removed from the document flow and moves "above" its flow of the parent element. The CSS I use (similar to):
#header h1 {position: relative; border: 0 none transparent; font-size: 2em; margin: 1em auto; border: 1px solid #ccc; } #header img {position: absolute; top: 0; left: 0; }
I posted a demo here: http://www.davidrhysthomas.co.uk/so/h1Img.html
The first section shows the first step, positioning the image relative to its parent, an image with a transparent background, to give an idea of what is happening.
In the second and third sections, I used jQuery to assign <h1> sizes equal to the size of the image, to prevent (as in the first version) the text <h1> going beyond the image itself, as follows:
$(window).load( function() { var w = $('#logo').outerWidth(); var h = $('#logo').outerHeight() ; $('#two h1').css('width',w,'height',h); $('#three h1').css('width',w,'height',h); } );
It's a bit of a shred, it obviously relies on javascript being turned on, and the page also flickers a bit when the page loads, as the document runs the script and applies the / css math.
The approach meets your requirements for the text to be displayed before the image itself is loaded, and it uses xhtml and CSS to hide the text. And if the image sizes are known in advance, it does not require jQuery / js to prevent overflow.
(I apologize to Jeff Joel et al for using the SO logo without any permission, but he considered it necessary at that time ... =))