driven_13
asked on
Applying Link using CSS only
Hello all experts.
I was wondering if there was a way by which I could apply a link (A HREF) to a div or to an images using only CSS.
So for example, if there was a background image to a DIV, then if a person clicked inside that DIV, then they will be linked to the corresponding page.
Unfortunately, I cannot place an transparent image inside the DIV or have any content inside the DIV.
The linking has be happen, if at all possible, only via CSS.
Or, at the most, via some JavaScript, but that too is way beyond me...[:0(
Thanx in advance,
--d.
I was wondering if there was a way by which I could apply a link (A HREF) to a div or to an images using only CSS.
So for example, if there was a background image to a DIV, then if a person clicked inside that DIV, then they will be linked to the corresponding page.
Unfortunately, I cannot place an transparent image inside the DIV or have any content inside the DIV.
The linking has be happen, if at all possible, only via CSS.
Or, at the most, via some JavaScript, but that too is way beyond me...[:0(
Thanx in advance,
--d.
ASKER
Thanx for the response kaufmed.
That was exactly what I was looking for...[:0)
Two follow-up questions:
1.) Does the JavaScript have to sit AFTER the DIV..?? Because if I put it in the HEAD section it is not working.
2.) What if I have a couple of more DIVs that I want to behave this same way? Do I copy and paste this same code or is there a better way of accomplishing it..?
3.) What if I want to open the link in a separate window..?
Thanx again.
--d.
That was exactly what I was looking for...[:0)
Two follow-up questions:
1.) Does the JavaScript have to sit AFTER the DIV..?? Because if I put it in the HEAD section it is not working.
2.) What if I have a couple of more DIVs that I want to behave this same way? Do I copy and paste this same code or is there a better way of accomplishing it..?
3.) What if I want to open the link in a separate window..?
Thanx again.
--d.
To answer all of your follow-up questions I would use the jquery framework with which you overcome some browserspecific issues and can address all of your divs by just applying the same class of all the divs you want to behave like link tags and store the actual url where you want the user to go after clicking the div in the id of the div's.
add this in your head section of your html:
add this in your head section of your html:
<script type="text/javascript src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript>
//jquery code
$(function(){ // this will apply this functino to your divs only after the dom has been loaded completely, resolves the issue of code positioning. you can leave it in the head section now
$('.class_of_your_link_divs').click(function(){
var url $(this).attr('id');
window.location = url;
});
});
</script>
ASKER
Cool. Thanx much.
So, window,location = url means it will open in a new window..??
How do I mix and match; some opens in a new window while some do not...??
--d.
So, window,location = url means it will open in a new window..??
How do I mix and match; some opens in a new window while some do not...??
--d.
One solution could be to create hidden <a> tags with the target="_blank" (new window) or target="_self" (same window) attribute and trigger the click via jquery. The id of your div's is now holding the reference to the hidden <a> tag. Like so:
jqeury
<a href="some url" id="hidden-href-1" target="_what you prefer self or blank" class="apply_some_css_to_hide_it">
<div class="i_act_as_a_link" id="href-1" ></div>
jqeury
$('.i_act_as_a_link').click(function(){
var link = 'hidden'+$(this).attr('id');
$('#'+link).trigger('click');
});
ASKER
Thanx for the response mcnute.
I used the first solution (without opening a separate window) and it is working fine, except that in IE it is showing this error in the lower-left corner:
and this is what is in that file:
Line 3, Character 15 is the dollar-sign ($).
Any idea what is this error all about...??
Thanx,
--d.
I used the first solution (without opening a separate window) and it is working fine, except that in IE it is showing this error in the lower-left corner:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)
Timestamp: Fri, 21 Dec 2012 13:47:09 UTC
Message: Expected ';'
Line: 3
Char: 15
Code: 0
URI: http://mydomainname.com/js/activefunctions.js
and this is what is in that file:
$(function(){
$('.divlinkopen').click(function(){
var url $(this).attr('id');
window.location = url;
});
});
Line 3, Character 15 is the dollar-sign ($).
Any idea what is this error all about...??
Thanx,
--d.
I forgot an equal sign in there, sorry for that:
so:
so:
$(function(){
$('.divlinkopen').click(function(){
var url = $(this).attr('id');
window.location = url;
});
});
ASKER
Ok.
I am sorry but I am an absolute noob in all this.
I am not sure where to put the actual URL where I want to link to.
All I have is the code that you sent me and I inserted the name of the DIV that I want to be clickable. But where do I put the actual URL...???
My ignorance is getting in my way.
Sorry.
--d.
I am sorry but I am an absolute noob in all this.
I am not sure where to put the actual URL where I want to link to.
All I have is the code that you sent me and I inserted the name of the DIV that I want to be clickable. But where do I put the actual URL...???
My ignorance is getting in my way.
Sorry.
--d.
Your URL goes in the <a> tag of your html
Consider this:
<a href="here_goes_your_url.com/som epage.html " id="hidden-href-1" target="_blank" title="Some Title for the link" ></a> (I also forgot to close the a tag so please add that closing </a> after the opening <a ... in the script i sent you first.)
I hope I could explain it somehow.
Consider this:
<a href="here_goes_your_url.com/som
I hope I could explain it somehow.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ChrisStanyon is right. That is even simpler and is html 5 compliant. Some pointies would be nice though.
ASKER
Gentlemen, I am sorry but none of the solutions are working.
This is what I have in the HEAD:
This is what I have in the BODY:
There is no other content in this DIV but it has a width and height explicitly set. I want the whole DIV clickable as there is a background image. So people will think they are clicking on the actual image.
It is entirely possible I copied some code wrong as I hav no idea what I am doing.
Looking at the above can you tell me what I am doing wrong..??
Thanx again,
--d.
This is what I have in the HEAD:
<script type="text/javascript src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" >
$(function(){
$('.clicker').click(function(){
window.open($(this).data('url'));
});
});
</script>
This is what I have in the BODY:
<div id="homepageMain">
<div class="left"></div>
<div id="clicker" data-url="http://www.microsoft.com" class="right"></div>
</div>
There is no other content in this DIV but it has a width and height explicitly set. I want the whole DIV clickable as there is a background image. So people will think they are clicking on the actual image.
It is entirely possible I copied some code wrong as I hav no idea what I am doing.
Looking at the above can you tell me what I am doing wrong..??
Thanx again,
--d.
You need to change the $('.clicker') which would address an element with the CLASS clicker to $('#clicker') which addresses your div with an ID clicker. hash is for id's. period for classes. Then it should work.
In the HTML you have an ID of 'clicker', but the jQuery is looking for a class of clicker. An ID can only exist once in a page and has to be unique whereas a class can be used as often as needed. If you want more than one clicker then it has to use classes. Change you HTML to this:
<div class="clicker" data-url="http://www.microsoft.com" class="right"></div>
<div class="clicker" data-url="http://www.microsoft.com" class="right"></div>
Actually, just realised you already have a class assigned so just add clicker to it:
<div data-url="http://www.microsoft.com" class="right clicker"></div>
<div data-url="http://www.microsoft.com" class="right clicker"></div>
Just to clarify, in CSS (and jQuery) you select an element with an ID using # (hash) and you select elements with a class using . (period)
So:
$('.clicker') //jquery
.clicker { } //css
will select/style all elements with a class of 'clicker', for example
<div class="clicker">Clicker 1</div>
<div class="clicker">Clicker 2</div>
whereas
$('#clicker') //jquery
#clicker { } //css
will select/style a single element with an ID of #clicker, such as:
<div id=-"clicker">Clicker</div >
So:
$('.clicker') //jquery
.clicker { } //css
will select/style all elements with a class of 'clicker', for example
<div class="clicker">Clicker 1</div>
<div class="clicker">Clicker 2</div>
whereas
$('#clicker') //jquery
#clicker { } //css
will select/style a single element with an ID of #clicker, such as:
<div id=-"clicker">Clicker</div
ASKER
Ok, cool. Thanx for the quick lesson.
The solution is working to an extent.
Now, when I click on it, it is opening up a separate window with this URL:
http://mydomainname.com/undefined
I want it to stay in the same window and open the microsoft.com site.
So close...!!
--d.
The solution is working to an extent.
Now, when I click on it, it is opening up a separate window with this URL:
http://mydomainname.com/undefined
I want it to stay in the same window and open the microsoft.com site.
So close...!!
--d.
OK. Just change the window.open line:
$('.clicker').click(function(){
window.location = $(this).data('url');
});
ASKER
Ok, it is now opening in the same window but with the same wrong URL as before:
http://mydomainname.com/undefined
--d.
http://mydomainname.com/undefined
--d.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Post your code as you have it - the HTML and the jQuery.
ASKER
That did it....!!!!
Thank you both for your help and immense patience with me.
Have a Merry Christmas.
Best,
--d.
Thank you both for your help and immense patience with me.
Have a Merry Christmas.
Best,
--d.
You're welcome...
Happy Christmas ;)
Happy Christmas ;)
Cheers, enjoy your holidays.
Try:
Open in new window