Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperlinking an image using CSS

Tags:

html

css

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.

like image 436
ljs Avatar asked Sep 22 '08 21:09

ljs


3 Answers

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='...'; 
}
like image 123
Swati Avatar answered Sep 20 '22 05:09

Swati


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.

like image 28
Dave Rutledge Avatar answered Sep 23 '22 05:09

Dave Rutledge


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.

like image 23
John Rudy Avatar answered Sep 22 '22 05:09

John Rudy