views:

309

answers:

4
+2  Q: 

valid html header

Is there any way to write a valid header/link/image? Something like

<a href="index.html"><h1 id="logo">Company name</h1></a>

(with an image set as background and the text moved way to the left so its not visible via css)

+5  A: 

Something like:

<h1 id="logo"><a href="index.html">Company Name</a></h1>

#logo a {
    display: block;
    width: 200px; /* width and height equal to image size */
    height: 100px;
    background: transparent url(images/logo.png) no-repeat;
    text-indent:-9999px;
}

The problem with you original markup is that <a> is an inline element and <h1> is a block element. An inline element cannot contain a block element.

roryf
hiding the text using css is a bad idea. why not use the an image and the alt attribute? (that's what it's for)
Adrian Mester
+1  A: 

That is invalid HTML code, but you can use CSS with the valid code to achieve what you want:

HTML

<h1 id="logo"><a href="index.html">Company name</a></h1>

CSS

h1 a:link, h1 a:visited {
  display: block;
}

You may further style the A element to achieve what you want.

Ionuț G. Stan
You don’t need to address the status.
Gumbo
@Gumbo, I believe it's an instinct that I have due to some IE 6 bug, don't remember which though.
Ionuț G. Stan
+1  A: 

This is valid and i think achieves what you are looking for, i.e. the entire Image is a link to the home page.

<a href="index.html"><img src="logo.png" id="logo" alt="Company Name" /></a>

If all you do is wrap the H1 tag in your example, then the link is only clickable around the text. This will exclude the margin of the H1 tag.

This doesn't require CSS to hide text, as the Company name is in the ALT attribute, which is read by Google and screen readers.

Another Way:

If you actually need an image as a background and text in the foreground there is another way about it:

<body>
   <div id="header"> 
      <h1>Some Text Here</h1>
   </div>

   <div id="main-content">
       ... lots of content here ...
   </div>
 </body>

CSS:

#header { 
    height: 55px;   // The height of you image
    width: 780px;   // The width of the image
    background: url(picture.png);
    margin: 0 auto; // To keep this div centered 
}

#header h1 {
    font-family: 'Times New Roman';
    text-decoration: bold;
    margin-left: 55px;  // This makes the text come in 55px from 
                        // the left of the containing div. Adjust to suit.
    margin-top: 10px;   // Same as above.
}

This is a useful method if you want to change the text often. A quotes of the day type thing or if your boss is always asking you to tweak the background image.

Seanchán
+3  A: 

No, anchor elements cannot contain h1 elements.

Also, background images should be reserved for backgrounds - a logo is not a background, it is important information.

<h1><a href="/"><img src="/images/logo.png" alt="Company Name"></a></h1>

Remember that, while the identity of the company is probably the most important heading on the homepage, it probably is not on other pages - so the should usually be downgraded to a elsewhere.

David Dorward
Good point about not using <h1> on sub-pages. I think whether the logo should contain an <img> or not very much depends on the design in question though.
roryf
how does img alt stand vs h1 in SEO?
doug
AFAIK it doesn't make a difference, google would ignore the image and read the alt text anyway and it doesn't process CSS (I think...)
roryf
I'm 99% sure that H1 ranks *much* better than img alt text. I'm very fond of using the replace-H1-with-an-image CSS 'trick' myself.
da5id