making a div a link

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WmTn

    #16
    I use span instead of div.
    Seems to work fine and validate XHTML Strict....

    <a href="http://"><span class="" style="height:7 3px; width:150px;"></span></a>

    Been using it for some time now since I found that using the div inside the link tags doesnt validate.
    No problems whatsoever so far.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #17
      Double-necro'd. And with absolutely nothing new to add. Bravo.

      Comment

      • Dave Wouters

        #18
        2 possible solutions

        Hi everyone,

        Two possible solutions:

        1) Put the link inside the <div> and wrap all inline elements within the link in <object> tags. So like this:

        Code:
        <div>
        <a href="#">
        <object><h3>Header text here</h3></object>
        <img src="#">
        <object><p>Paragraph text here</p></object>
        </a>
        </div>
        This validates and everything within the <a> tag now becomes clickable. You don't have to put blocklevel elements (like <img>) within <object> tags to make this validate.

        2) You can put your link inside a <span>, which is inside the <div>. You can then add a transparent image the size of the block you want to be clickable to the link (a completely transparent png or gif will work) and position this absolutely over the <div>. So like this:

        Code:
        <div>
        <span><a href="#"><img src="transparent.png" width="200px" height="150px"</a></span>
        <h3>Header text here</h3>
        <img src="#">
        <p>Paragraph text here</p>
        </div>
        To make it work you give the <span> the same width and height (using CSS) as your transparent image. So the CSS for the <span> would look something like this:

        Code:
        span {
        width:200px;
        height:150px;
        position:absolute;
        }
        In order for the absolute positioning to work you need to position the containing <div> relatively. So the CSS for the containing <div> would look something like this:

        Code:
        div {
        position:relative;
        }
        Hope this helps...
        Last edited by Niheel; Oct 8 '10, 04:20 PM.

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #19
          Maybe we can continue this thread another 3 years from now.

          Comment

          • SalcWeb

            #20
            Simple answer, put a blank transparent PNG inside the div, set it to the same height and width as the div and link the image.

            It's a bit heavy handed, but it works.

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #21
              Seriously...? Just put an inline-level element in the anchor and use CSS to make it a block. It's not nearly as complicated as you guys are making it. When someone has CSS turned off, it means that they don't care what the page looks like. They don't need block-element links.

              Now please... Let the thread die?

              Comment

              • luciminea
                New Member
                • Mar 2012
                • 2

                #22
                The problems are simple. YES a div can be a link, those who doesn't know it yet, should learn some more. The html code for this is as simple as you wrote initialy:
                Code:
                <a href="#"><div class="someclass"><div></a>
                But for this to work, should have in the .css file as a rule for you div, the folowing: a given height, a given width, and the rule display:block, but not for <a> element, but for "someclass" element.

                Do that and I gurantee this work without a single problem.

                For the list problem (ul and li) I wouldn't sugest you use this technique, better explain exactly what you want to achieve, and we'll help you with the right solution.

                Comment

                • drhowarddrfine
                  Recognized Expert Expert
                  • Sep 2006
                  • 7434

                  #23
                  Almost five years ago, WHEN THIS THREAD ORIGINALLY STARTED, trying to make a div a link was invalid. It has only been possible and valid to do with HTML5.

                  Comment

                  • luciminea
                    New Member
                    • Mar 2012
                    • 2

                    #24
                    Oh my god ! My bad, I didn't look at the date, my apology.

                    Comment

                    • kovik
                      Recognized Expert Top Contributor
                      • Jun 2007
                      • 1044

                      #25
                      The thread that never dies. At least it gives me a reason to check up on Bytes and read how much of a **** I used to be. Good times, man. Good times.

                      Comment

                      • rybo111
                        New Member
                        • Nov 2013
                        • 1

                        #26
                        Originally posted by drhowarddrfine
                        Maybe we can continue this thread another 3 years from now.
                        No worries.

                        HTML:
                        Code:
                        <div class="inner-link">
                          Go to <a href="https://google.com">Google</a>
                        </div>
                        jQuery:
                        Code:
                        $(".inner-link").click(function() {
                          window.location = $(this).find("a").attr("href");
                        });

                        Comment

                        Working...