User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 392,070 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,263 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 12364 | Replies: 19 | Solved
Reply
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 107
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Question Hyperlink a div

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Solution Re: Hyperlink a div

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

<div style="position:absolute; left:0px; top:0px; border-color:#FFFFFF;
border-style:solid" CLASS="divname" 
onmouseover="style.backgroundColor='#FFFFFF';
this.style.cursor='pointer'" onmouseout="style.backgroundColor=''" 
onmouseup="somepage.htm"></div>
Reply With Quote  
Join Date: Oct 2006
Posts: 10
Reputation: remiremi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
remiremi remiremi is offline Offline
Newbie Poster

Re: Hyperlink a div

  #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 9:15 am. Reason: Put code in code tags.
Reply With Quote  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is online now Online
Practically a Master Poster

Help Re: Hyperlink a div

  #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  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is online now Online
Practically a Master Poster

Re: Hyperlink a div

  #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  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

  #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 11:49 am.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Hyperlink a div

  #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 1:49 pm.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

  #8  
Oct 28th, 2006
I am really, REALLY missing something here.
<div style="position:absolute; left:0px; top:0px; border-color:#FFFFFF;
border-style:solid" CLASS="divname" 
onmouseover="style.backgroundColor='#FFFFFF';
this.style.cursor='pointer'" onmouseout="style.backgroundColor=''" 
onmouseup="somepage.htm"></div>
makes the entire div a link.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Hyperlink a div

  #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

onmouseup="window.location='somepage.htm'"
Last edited by MattEvans : Oct 28th, 2006 at 2:05 pm.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2006
Posts: 214
Reputation: SnowDog is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
SnowDog's Avatar
SnowDog SnowDog is offline Offline
Posting Whiz in Training

Re: Hyperlink a div

  #10  
Oct 28th, 2006
Well, no.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC