Forum Moderators: not2easy

Message Too Old, No Replies

Link entire div

         

greencode

2:41 pm on Sep 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have the following code which is great but I would like the entire containing div (b1-m in the code) to be a link and not just the text that's contained within.

I've attempted to put the link directly after the class but when I do that it fails validation.

Any help would be greatly appreciated (please ignore the extra css - the .lf and .rt contain images as this is a rounded edged button where I'm unable to use CSS3).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>four boxes in a row</title>

<style type="text/css">
.b1-m {
float: left;
}

.b1-m a {
display: block;
}

.b1-m .lf,
.b1-m .rt {
height: 25px;
width: 20px;
float: left;
}

.b1-m .mn {
height: 25px;
width: auto;
float: left;
font-size: 12px;
font-weight: bold;
line-height: 26px;
padding-left: 5px;
padding-right: 5px;
}

.b1-m .mn {
background-color: #ffdb11;
color: #3b3b3b;
}

.b1-m a {
color: #3b3b3b;
}

.b1-m .lf {
background-color: #ffdb11;
}

.b1-m .rt {
background-color: #ffdb11;
}

</style>

</head>
<body>

<div class="b1-m">
<div class="lf"></div>
<div class="mn"><a href="#">Test Link</a></div>
<div class="rt"></div>
</div>

</body>
</html>

Major_Payne

5:13 pm on Sep 29, 2010 (gmt 0)



Do you really need all those divs inside a div? May be excessive div-itis here, but entire div contents can be made a link. Maybe try adding a link for "b1-m" itself and hide the text. Just be sure you do have a width/height for the div. Didn't try this, but maybe play around with the idea:

CSS:

.b1-m {
width: 100px;
height: 100px;
border: 1px solid #f00;
}

.b1-m a {
width: 100px;
height: 100px;
display: block;
}

.b1-m a span {
margin: -9999px;
}


HTML:

<div class="b1-m"><a href="#nogo"><span>blah</span></a>
.
.
.
</div>


Maybe just play with that. I put a border in so you can see the entire area is a link.

greencode

5:34 pm on Sep 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Yes, unfortunately I do need those divs as it's box that has rounded corners and so I have a left column, center column, right column as well as the containing div.

I've tried using what you've suggested but I'm not totally convinced this is the correct way to do it. Obviously that being said I've currently got no idea what is!

Major_Payne

7:21 pm on Sep 29, 2010 (gmt 0)



The code worked for me to make the entire div clickable. I didn't put in the other divs, but will try it if that is ALL the CSS and HTML you are using.

BTW, those other divs are appearing outside the main one. Just what size do you need for the "b1-m" div?

rocknbil

10:15 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That **is** the totally correct way - not only are anchors the natural click/navigation object, they have a semantic meaning whereas a div does not. Give it a try, set the anchor display to block and it will work just like a clickable div.

greencode

10:28 pm on Sep 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'll give it a go tomorrow and report back

greencode

10:58 pm on Sep 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



By using this method so I have to specify the containing DIV size? The reason why I ask is that I have html text on the buttons and therefore these could be large or small depending on the link text.

milosevic

12:44 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



Yes, unfortunately I do need those divs as it's box that has rounded corners and so I have a left column, center column, right column as well as the containing div.


Unless you can't use JavaScript on the site for some reason, I would recommend using a rounded corners script such as this one [malsup.com...] which takes only a few minutes to set up and will mean you don't have to bother with these extra divs/columns hassles again and this issue you are posting about would be fixed in one go. It would also no doubt save you much time with similar headaches in future.

I can't say how much development time this script has saved me. Well, I think I just wouldn't bother with rounded corners if it wasn't for this script.

greencode

2:29 pm on Sep 30, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Ok, I'm yet to try the link I originally mentioned in this post but "milosevic" thanks so much for this... This is a fantastic script and one I've just started to implement. I'll just need to take an hour or two now to go through my files and remove the unnecessary divs. Is there any downside of using this script? I'm already using javascripts and also jquery so I'm presuming not. I've tested in IE6 and it works in that so I'm guessing it's okay to use.

milosevic

2:45 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



No problems greencode, glad you are finding it useful - as I mentioned it's been a great time-saver for me.

I guess it can look slightly jaggy in IE rendering when small border radius values are used because anti-aliasing is not used on the corners, and it creates a load of extra divs on the page (entirely behind the scenes). But the advantage of this particular script is that for browsers that support CSS3 it will use a CSS border-radius property instead, meaning it is very future proof and your site will look good in the latest browsers.

greencode

4:50 pm on Sep 30, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks Major_Payne. Had a proper go with it today and it works a treat. I think I was getting a little confused with the -9999 on the span as I wanted text to be visible. I've removed that now so all's good. Thanks again.

Major_Payne

5:05 pm on Sep 30, 2010 (gmt 0)



You're welcome. If you just wanted Rounded Corners, these are CSS methods:

[css3.info...]
[cssjuice.com...]
[kalsey.com...]
[malsup.com...]
[html.it...]
[devinrolsen.com...]