Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an empty anchor tag clickable in IE7?

I need to make an area within a background image clickable to generate an event for JavaScript use. So, I created an anchor tag and inside that I inserted some relevant text between semantically meaningless tags which I then made hidden:

<a href="#"><i>foo</i></a>

Then I gave the anchor tag 'display:block' properties, width and height values, and absolutely positioned it where I needed it to be in relation to the background image. In Firefox this works nicely - I hover over and my cursor changes as expected - I've got something clickable. IE7 however, doesn't like the fact that the anchor tag is 'empty' and therefore doesn't treat it as clickable. So I added this to the anchor tag in css:

background:url(/no-image.jpg); 

...which seems to fool IE7 into assuming something is there. IE7 now treats the area as clickable, even if no background image actually exists for the anchor tag. But this seems like a bit of a hack to me and I'm wondering if there is a more elegant way to deal with this problem. Any ideas would be greatly appreciated. Thanks.

like image 447
deluxe_247 Avatar asked Nov 02 '09 21:11

deluxe_247


People also ask

Is the anchor tag in HTML an empty tag?

It is not OK at all, since it breaks fundamental accessibility principles. There is no way to specify alternative text for a background image, the same way you can and should specify it using an alt attribute in an img tag for a content image. See WCAG 2.0 Guideline 1.1.

How do you make an anchor tag not clickable?

How do I make an anchor tag non clickable? In order to disable a HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function. This makes the HTML Anchor Link (HyperLink) disabled i.e. non-clickable.

What attribute and value can you use to cause an a element to open the link in a new browser tab?

The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.

What is tag in anchor?

The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination.


1 Answers

You've found a rendering problem with IE, and according to @Simon below the issue still exists at least through IE9.

Your background: hack will work, but the browser will make an HTTP request each time to resolve the bogus URL. This may hurt the performance of your page. To achieve the same result but not make an unnecessary HTTP request, I'd suggest using this URL instead:

background-image:url(about:blank);

about:blank is a special URL that browsers show as an empty page, so it won't affect how the element is displayed, but it also won't make any HTTP requests either.

BTW, the problem only happens when you have an absolutely or relatively-positioned A element (or an A element inside a positioned block). Regular non-positioned hyperlinks don't seem to have this problem under IE7.

like image 198
Justin Grant Avatar answered Sep 29 '22 18:09

Justin Grant