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)
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)
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.
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.
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.
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.