Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 125
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Hyperlink a div

 
0
  #1
Mar 8th, 2006
Is it possible to make an entire <div> into a hyperlink using CSS only, as can be easily accomplished with mouse events in JavaScript? I'm assuming it's too good to be true since CSS isn't interactive? Nevermind ... I guess I just answered my own question
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Solved Threads: 8
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

 
0
  #2
Oct 17th, 2006
The way I do it is as follows (for example):

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div style="position:absolute; left:0px; top:0px; border-color:#FFFFFF;
  2. border-style:solid" CLASS="divname"
  3. onmouseover="style.backgroundColor='#FFFFFF';
  4. this.style.cursor='pointer'" onmouseout="style.backgroundColor=''"
  5. onmouseup="somepage.htm"></div>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: remiremi is an unknown quantity at this point 
Solved Threads: 1
remiremi remiremi is offline Offline
Newbie Poster

Re: Hyperlink a div

 
0
  #3
Oct 28th, 2006
[html]a.divlink, a.divlink:link {
display:block;
width:100%; /*width needs to be specified to work in IE*/
height:100%; /*height needs to be specified to work in IE*/
/*Other browsers expand the link to full width and height.*/
line-height:0;
font-size:0;
}



<div><a class="divlink">link</a></div>[/html]

Hope this helps you buddy
Last edited by tgreer; Oct 28th, 2006 at 10:15 am. Reason: Put code in code tags.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Hyperlink a div

 
0
  #4
Oct 28th, 2006
Originally Posted by cscgal View Post
Is it possible to make an entire <div> into a hyperlink using CSS only, as can be easily accomplished with mouse events in JavaScript? I'm assuming it's too good to be true since CSS isn't interactive? Nevermind ... I guess I just answered my own question
Nope, unfortunately you're right. You can't emulate links on any other HTML element with CSS. You'll have to use JS for that.
If its an issue of support for javascript than I don't think there's any way around it, but if its just code choice then you could just attach say a class to the div element and have JS run through and give it link properties.

eg:
[HTML]
<div class="dlink" href="page.html">click me</div>[/HTML]

[HTML]
<script>
// assuming you can select elements by class
document.prototype.getElmentsByClass = function(className) { ... };

var dlinks = document.getElmentsByClass('dlink');
for(var x in dlinks) {
var dlink = dlinks[x]; // closure (ie: dlink will be available in closure scope for onlick function when it is triggered in the window scope later on)
dlink.onclick = function() { document.location.href = dlink.href; };
}
</script>
[/HTML]

Even though its using JS, it emulates what you would normally do with CSS. However if you're working in an environment with only CSS on the client, then I don't believe it can be done.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Hyperlink a div

 
0
  #5
Oct 28th, 2006
Btw: adding a href attribute to a div will make it invalid xHTML1.0. So even if you found a way to do it with CSS, you'd run into that problem. The solution would be to create a custom DTD for your document.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Solved Threads: 8
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

 
0
  #6
Oct 28th, 2006
Am I missing something?

The question was
Is it possible to make an entire <div> into a hyperlink using CSS only...
.

The solution I offered may not be what you'd class as 'only css' but it doesn't have a H REF and it assigns properties to a class. Is that wrong?
Last edited by SnowDog; Oct 28th, 2006 at 12:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Hyperlink a div

 
0
  #7
Oct 28th, 2006
you can hyperlink the entire contents of a div... put an enormous image inside it, set the overflow to hidden, and hyperlink the image

i have a q, i was gonna start a thread but this is close enough to my question:

can you hyperlink a form submit field? O_o it seems trivial but it isn't proving easy... I want a form with a normal submit button (so I can't use the form action) and another button that just opens a new page... i don't want to use JS, and I don't mind how much markup i need on the page.. (actually, i'm using "button" rather than input/submit) so i guess, i can stick i giant hyperlinked image in a overflow controlled div in a typeless button.

messy =)

any better solutions..? o_O

EDIT: bah... that doesn't work though! who decided you can't have hyperlinked anchors in buttons? as i'm overriding the button graphical style anyway, i guess i can just emulate it with a div... i have to use JS to toggle the edges and padding to give a "clicked" appearance... but at least i don't have JS depandant functionality...

another solution.. is to absolutely place another form and button next to the submit button and use the form action... methinks that's messier.
Last edited by MattEvans; Oct 28th, 2006 at 2:49 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Solved Threads: 8
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

 
0
  #8
Oct 28th, 2006
I am really, REALLY missing something here.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div style="position:absolute; left:0px; top:0px; border-color:#FFFFFF;
  2. border-style:solid" CLASS="divname"
  3. onmouseover="style.backgroundColor='#FFFFFF';
  4. this.style.cursor='pointer'" onmouseout="style.backgroundColor=''"
  5. onmouseup="somepage.htm"></div>
makes the entire div a link.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Hyperlink a div

 
0
  #9
Oct 28th, 2006
onmouseup="somepage.htm"
is that not JS though?

EDIT: infact, it's not just JS, it's erroneous JS, should be

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. onmouseup="window.location='somepage.htm'"
Last edited by MattEvans; Oct 28th, 2006 at 3:05 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Solved Threads: 8
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

 
0
  #10
Oct 28th, 2006
Well, no.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC