Link to home
Start Free TrialLog in
Avatar of driven_13
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.
Avatar of kaufmed
kaufmed
Flag of United States of America image

CSS is for styling, so it wouldn't make sense that you could affect the behavior of a page via CSS. I think Javascript is your friend on this one.

Try:

<!DOCTYPE html>
<html>
  <body>
    <div id="div-to-target">this is the content</div>

    <script type="text/javascript">
        var targetDiv = document.getElementById('div-to-target');

        if (targetDiv != null) {
            targetDiv.onclick = function() {
                window.location = "http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/CSS/Q_27975011.html";
            }
        }
    </script>
  </body>
</html>

Open in new window

Avatar of driven_13
driven_13

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.
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:
<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>

Open in new window

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.
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:

<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>

Open in new window


jqeury

$('.i_act_as_a_link').click(function(){
     var link = 'hidden'+$(this).attr('id');
    $('#'+link).trigger('click');

});

Open in new window

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:

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

Open in new window


and this is what is in that file:

$(function(){
   $('.divlinkopen').click(function(){
      var url $(this).attr('id');
      window.location = url;
   });
});

Open in new window


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:
$(function(){
   $('.divlinkopen').click(function(){
      var url = $(this).attr('id');
      window.location = url;
   });
});

Open in new window

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.
Your URL goes in the <a> tag of your html

Consider this:

 <a href="here_goes_your_url.com/somepage.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.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ChrisStanyon is right. That is even simpler and is html 5 compliant. Some pointies would be nice though.
Gentlemen, I am sorry but none of the solutions are working.

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>

Open in new window


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>

Open in new window


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>
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>
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>
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.
OK. Just change the window.open line:

$('.clicker').click(function(){
   window.location = $(this).data('url');
});

Open in new window

Ok, it is now opening in the same window but with the same wrong URL as before:

http://mydomainname.com/undefined

--d.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Post your code as you have it - the HTML and the jQuery.
That did it....!!!!

Thank you both for your help and immense patience with me.

Have a Merry Christmas.

Best,

--d.
You're welcome...

Happy Christmas ;)
Cheers, enjoy your holidays.