•
•
•
•
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
![]() |
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation:
Rep Power: 32
Solved Threads: 107
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
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
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>
•
•
Join Date: Oct 2006
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 1
[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
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.
•
•
•
•
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Am I missing something? 
The question was .
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?

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.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation:
Rep Power: 5
Solved Threads: 47
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.

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.
I am really, REALLY missing something here.
makes the entire div a link.
<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>
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation:
Rep Power: 5
Solved Threads: 47
•
•
•
•
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Previous Thread: javascript pop-up div
- Next Thread: Javascript gives error on localhost



Linear Mode