I tend to approach this problem using a slightly less-pure method.
I nest the image within the h1, to give the following mark-up:
<div id="header">
<h1>This is the header<img src="img/stackoverflowLogo.png" /></h1>
</div>
And then use CSS to position the <h1>
relative, and the image with position:absolute
. This causes the image to taken out of the document flow and moved 'above' its parent element's text. The CSS I use is (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've posted a demo here: http://www.davidrhysthomas.co.uk/so/h1Img.html.
The first section shows the first step, positioning the image in relation to its parent, the image with a transparent background to give an idea about what's happening.
In the second and third sections I've used jQuery to assign to the <h1>
dimensions equal to that of the image, in order to prevent (as in the first version) the <h1>
text extending 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 is a little bit kludgy, it obviously relies upon javascript being enabled and also there's a bit of a page flicker on page-load as the document runs the script and applies the math/css.
The approach does meet your requirements of having text visible until the image itself loads, and it does use xhtml and CSS to hide the text. And if the image-dimensions are known beforehand it doesn't require the jQuery/js to prevent the overflow.
(My apologies to Jeff, Joel et al, for using the SO logo without any kind of permission, but it felt appropriate at the time... =) )