| | |
Hyperlink a div
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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
The way I do it is as follows (for example):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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:
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 10: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
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 12:49 pm.
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 2:49 pm.
Plato forgot the nullahedron..
I am really, REALLY missing something here.
makes the entire div a link.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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>
•
•
•
•
onmouseup="somepage.htm"
EDIT: infact, it's not just JS, it's erroneous JS, should be
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
onmouseup="window.location='somepage.htm'"
Last edited by MattEvans; Oct 28th, 2006 at 3:05 pm.
Plato forgot the nullahedron..
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Calculate number of days between two dates minus weekends
- Next Thread: Javascript gives error on localhost
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor decimal dependent design disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms google gwt gxt hiddenvalue highlightedword html htmlform ie8 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl listbox math media microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php player post problem progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n






