tags:

views:

126

answers:

5

If you look here and hover over "PONUDA", you will get that nice hover effect. The thing is that all li's have anchor in it that do have href value, so when you click on them some page will be opened. But because i have some sub menu shown when you hover over "PONUDA", i dont want it to be linked but i still want to have that hover effect. So thats why the code is like this:

<a>PONUDA</a>

Tbh i have never thought about using anchor without href value, so few questions:

  1. How valid is this technique for using :hover state of the anchor? I know i could use js, but i just want to know is it ok solution for when you just want to use that pseudo selector :hover, so just css without js.
  2. What about search engines? What do they do when they reach anchor tag, and they see that it does not have href? Do they expect every anchor tag to have href?

I know it a silly question, but any reference to this "problem" would be great!

A: 

you can use <a href="#">...</a> or <a href="javascript:;">...</a>

without href attribute is an invalid markup

303
Not so. an `a` element without an href attribute is called an "anchor"
Paul Butcher
Ill just copy my comment from other answer: html5, xhtml strict, xhtml transitional, none of those brings up anything that has to do with this anchor tag. also css is also valid, if you ignore some css3 styles that i am using.
GaVrA
Take a look at the context - it looks like a navigation, so it should have (imho) href attribute; if you place <a> within a content then it is fine; currently it doesn't make sense to me
303
Your humble opinion doesn't change the DTDs for any existing version of HTML or XHTML.
Paul Butcher
I agree, in that context you would expect anchor(with href), but my question is why should we use href when it is not invalid markup to not use it. like i said in comments to @galambalazs answer, if i put href value in that <a>PONUDA</a> all of a sudden it does makes sense to you?
GaVrA
+2  A: 

Most modern browsers support using the :hover pseudo-class on tags other than a, so you could just apply them to your li tags, and even with Internet Explorer there are plenty of workarounds. This workaround is quite a nice one if you don't want to use any Javascript.

What's nice about using :hover on the li elements themselves, in my opinion, is that the hovered state remains even when you move to one of the nested list elements (i.e. submenu elements). If you don't quite want that effect, though, I'd suggest sticking with the a tags (though doing as Pekka says).

JAB
:hover on non-anchor elements may be slow on IE7 and the usage of it is therefore questionable - see here: http://code.google.com/speed/page-speed/docs/rendering.html (search for ":hover" w/o quotes)
Joscha
@Joscha: You bring that up, but don't mention the fact that Whatever:hover uses expressions for callbacks? Anyway, while I find your link an interesting read with some nice tips, is it all that necessary for relatively small pages? That is, just how much is the impact on performance for a simple dropdown menu with one or two levels versus that for a page filled with hundreds of elements to be hovered over?
JAB
(On a side note, I haven't noticed any slowness in IE7 for the menu I recently implemented. What exactly does that mean by "some cases"? Or is it mentioned in the Microsoft bug report link and I should just create a Windows Live ID to read it?)
JAB
@JAB in small-scale programming most hacky practices tend to be forgiven ;) I just wanted to mention, that it _can_ be a problem - whether it actually is one for a certain scenario lies in the eye of the beholder.
Joscha
A: 

You don't have to have the element be an anchor, as far as I know the :hover psuedo-class works on any valid DOM element. If you are concerned, you can add href="#" to the tag.

W_P
:hover doesn't work in older IE versions unfortunately
scunliffe
@scunliffe can you be more precise on that "older versions"?
GaVrA
also, :hover on non-anchor elements may be slow on IE7 - see here: http://code.google.com/speed/page-speed/docs/rendering.html (there is also a bug report linked)
Joscha
@GaVrA scunliffe is talking about IE7 and IE8 without strict doctype - but even then it only doesn't work for non-anchor elements.
Joscha
@Joscha: And IE6, doctype or not. (If I remember correctly, at least.)
JAB
A: 
  1. Looks valid to me, but unless you need it to work in IE6 or something, earlier, there's no reason you couldn't just use span.

  2. The href attribute is optional on a elements.
    I don't see why search engines would expect any optional attribute to exist, Also, in the spirit of being liberal with what you accept. Even if it were compulsory, I'd imagine any decent search engine would be able to cope.

Paul Butcher
Dunno why someone downvoted... Anyhow, regarding SE, i was kinda more thinking about 404 and stuff like that, like does SE maybe count every anchor as a link and if so, does not having href maybe makes SE report that as a 404 error... O_o
GaVrA
I would be surprised if any search engine would waste resources trying to visit a page at the URL "", just because it found an empty element.Note that when I say it's valid, I'm using a strict definition of the term. Check out WAI and ARIA (http://www.w3.org/TR/wai-aria-practices/) to see what you should be doing.
Paul Butcher
+1  A: 

<a href="#"> would be fine.

But you might want to point the href to a back-up page instead, so that those without Javascript could still navigate to a page containing links to the sub-menu options. Then add an onclick handler that returns false, to prevent Javascript-enabled browsers (who can see your menu) from following the link if clicked.

That way, you've covered all your bases, as well as justifying to yourself why that href attribute is there :)

Ed Daniel
+1 but i do that anyhow, its special cases like this, where i dont have parent page which will contain sub navigation links(every link is child page)... If i had like a page called "ponuda" and then put every link from sub navigation to be a child page to the "ponuda" page, then i would do it just like you said, but again, its these weird case scenarios, where im just asking what would happen and what to do then... :)
GaVrA