tags:

views:

1508

answers:

10

I know this is probably the dumbest question ever, however I am a total beginner when it comes to CSS; how do you hyperlink an image on a webpage using an image which is sourced from CSS? I am trying to set the title image on my website linkable to the frontpage. Thanks!

Edit: Just to make it clear, I'm sourcing my image from CSS, the CSS code for the header div is as follows:-

#header
{
    width: 1000px;
    margin: 0px auto;
    padding: 0px 15px 0px 15px;
    border: none;
    background: url(images/title.png) no-repeat bottom;
    width: 1000px;
    height: 100px;
}

I want to know how to make this div hyperlinked on my webpage without having to make it an anchor rather than a div.

+3  A: 

That's really not a CSS thing. You still need your A tag to make that work. (But use CSS to make sure the image border is either removed, or designed to your required spec.)

<a href="index.html"><img src="foo" class="whatever" alt="foo alt" /></a>

EDIT: Taking original intent (updated question) into account, a new code sample is below:

<a href="index.html"><img id="header" alt="foo alt" /></a>

You're still in an HTML world for links, as described by other answers on this question.

John Rudy
see above clarification.
kronoz
kronoz, we know what you mean, no clarification is necessary. This is still an HTML matter, not a CSS matter. If you want a link, then use an <a> element. That's what links are. A <div> element is not a link. You can emulate it with JavaScript, but that is a bad idea, and still not CSS.
Jim
+1  A: 

You still create links in HTML with 'a' (anchor) tags just like normal. CSS does not have anything that can specify if something is a link to somewhere or not.

Edit The comments of mine and others still apply. To clarify, you can use JavaScript to make a div act as a link:

<div id="header" onclick="window.location='http://google.com';"&gt;My Header</div>

That isn't really great for usability however as people without JavaScript enabled will be unable to click that and have it act as a link.

Also, you may want to add a cursor: pointer; line to your CSS to give the header div the correct mouse cursor for a link.

ctcherry
see above clarification.
kronoz
edited to add more details.
ctcherry
+10  A: 

You control design and styles with CSS, not the behavior of your content.

You're going to have to use something like <a id="header" href="[your link]">Logo</a> and then have a CSS block such as:

a#header {
  background-image: url(...);
  display: block;
  width: ..;
  height: ...;
}

You cannot nest a div inside <a> and still have 'valid' code. <a> is an inline element that cannot legally contain a block element. The only non-Javascript way to make a link is with the <a> element.

You can nest your <a> tag inside <div> and then put your image inside :)

If you don't want that, you're going to have to use JavaScript to make your <div> clickable:

Document.getElementById("header").onclick = function() {
    window.location='...'; 
}
Swati
A: 

CSS is for presentation only, not content. A link is content and should be put into the HTML of the site using a standard <a href=""> tag. You can then style this link (or add an image to the link) using CSS.

Rich Adams
+1  A: 
<a href="linkto_title_page.html" class="titleLink"></a>

then in your css

.titleLink {
  background-image: url(imageUrl);
}
Xian
+3  A: 

To link a css-sourced background-image:

#header { 
display:block;

margin: 0px auto;
padding: 0px 15px 0px 15px;
border: none;
background: url(images/title.png) no-repeat bottom;
width: 1000px;
height: 100px;    
}    

<a id="header" href="blah.html" class="linkedImage">

The key thing here is to turn the anchor tag into a block element, so height and width work. Otherwise it's an inline element and will ignore height.

davebug
thanks, I had the same problem and I like your clarifying the main problem which is display: block; usage.
Xabatcha
A: 

HTML is the only way to create links - it defines the structure and content of a web site.

CSS stands for Cascading Style Sheets - it only affects how things look.

Although normally an <a> tag is the only way to create a link, you can make a <div> clickable with JavaScript. I'd use jQuery:

$("div#header").click(function() {window.location=XXXXXX;});
Nathan Long
A: 

You have to use an anchor element, wrapped in a container. On your homepage, your title would normally be an h1, but then on content pages it would probably change to a div. You should also always have text in the anchor element for people without CSS support and/or screen readers. The easiest way to hide that is through CSS. Here are both examples:

<h1 id="title"><a title="Home" href="index.html>My Title</a></h1>
<div id="title"><a title="Home" href="index.html>My Title</a></div>

and the CSS:

#title {
position:relative; /*Makes this a containing element*/
}
#title a {
background: transparent url(../images/logo.png) no-repeat scroll 0 0;
display:block;
text-indent:-9999px; /*Hides the anchor text*/
height:50px; /*Set height and width to the exact size of your image*/
width:200px;
}

Depending on the rest of your stylesheet you may need to adjus it for the h1 to make it look the same as the div, check out CSS Resets for possible solutions to this.

roryf
A: 

The HTML5 spec-in-progress allows linking for several more attributes, div included. For example:

<div href="http://stackoverflow.com"&gt; .... </div>

Until HTML5 is widely adopted or something else comes along, the way to create a hypertext link is via the anchor element.

Carl Camera
A: 

Try this - use an H1 as the seat of your graphic instead. Saved my butt time and time again:

<h1 class="technique-six">
    CSS-Tricks
</h1>

h1.technique-six {
    width: 350px;
    padding: 75px 0 0 0;
    height: 0;
    background: url("images/header-image.jpg") no-repeat;
    overflow: hidden;
}

Accessible, and also solid across browsers IE6 and > . You could also link the H1.

John Dunagan