Problem
I am writing a web page where a user can click on a part of an image to trigger a Javascript event . However, I found that if I put a fixed binding ( a
) / button ( input type=button
) on the image ( img
), the user cannot click on it in IE (only), even if it is on top of the image.
Demo
<style type="text/css"> .MyDiv { position:relative; left:0px; top:0px; width:275px; height:95px; } .MyDiv .MyImageDiv { position:absolute; left:0px; top:0px; width:275px; height:95px; background-image:url('https://www.google.com/images/srpr/logo3w.png'); } .MyDiv .MyButton { position:absolute; left:162px; top:20px; width:53px; height:80px; display:inline-block; background-color:transparent; border:0px; cursor:pointer; } </style> <div class='MyDiv'> <img src='https://www.google.com/images/srpr/logo3w.png' /> <input type='button' class='MyButton' onClick="alert('You clicked g!');"/> </div> <div class='MyDiv'> <img src='https://www.google.com/images/srpr/logo3w.png' /> <a href='#' class='MyButton' onClick="alert('You clicked g!');"></a> </div> <hr/> <div class='MyDiv'> <div class='MyImageDiv'></div> <input type='button' class='MyButton' onClick="alert('You clicked g!');"/> </div> <div class='MyDiv'> <div class='MyImageDiv'></div> <a href='#' class='MyButton' onClick="alert('You clicked g!');"></a> </div>
Demo version
To check, press the small letter g in
- Demo1 (original)
- Demo2 (full, w3c checked)
- Demo3 (full, w3c verified, with
text-indent
as suggested by @ Sparky672).
Strike> EDIT (2012-12-05): Migration demo to jsfiddle.
More details
I can click and run the two lower warnings in all browsers; however, the first two CANs DO NOT start in IE (see test results below). This is normal, because I can always remind myself not to write this way, but is there any reasonable explanation why this is happening?
PS I tried to use z-index
on the anchor / button, but that didn't help.
EDIT (2012-06-01): Tried to use display:block
/ display:inline-block
on the anchor / button (according to @Zeta's comment), but that didn't help.
I also tried using the IE9 development tools (F12) to try to debug the page. If I use the arrow tool (Ctrl + B) to select the anchor / button, it cannot be selected; but if I select the anchor / button element in HTML Developer Tools, it will correctly display the position and size of the anchor / button. Strange enough.
Test results
+-------------------------------------------------------+---------------------------------------+ | | Can click on anchor / button on image | +-------------------------------------------------------+---------------------------------------+ | IE 6.0.2900.2180.xpsp_sp2_gdr.100216-1441, on Windows | **NO** | | IE 9.0.8112.16421, on Windows | **NO** | | Opera 11.64, on Windows | Yes | | Opera 12.00, on Windows | Yes | | Opera 12.02, on Windows | Yes | | Opera 12.11, on Windows | Yes | | Opera 11.64, on Mac | Yes | | Opera 12.00, on Mac | Yes | | Opera Mini 7.0.4, on iPhone | Yes | | Firefox 12.0, on Windows | Yes | | Firefox 14.0.1, on Windows | Yes | | Firefox 15.0.1, on Windows | Yes | | Firefox 13.0, on Mac | Yes | | Chrome 19.0.1084.52m, on Windows | Yes | | Chrome 22.0.1229.79 m, on Windows | Yes | | Chrome 23.0.1271.95 m, on Windows | Yes | | Chrome 19.0.1084.54, on Mac | Yes | | Chrome 21.0.1180.82, on iPhone | Yes | | Safari 5.1.5, on Windows | Yes | | Safari 5.1.7 (7534.57.2), on Windows | Yes | | Safari 5.1.7 (7534.57.2), on Mac | Yes | | Safari, on iOS 4.3.5 | Yes | | Browser, on Android 3.1 | Yes | +-------------------------------------------------------+---------------------------------------+