Problem with IE when using display:block for links

2019-03-18 05:45发布

This is my HTML:

<div id="links">
  <a href="">Link 1</a>
  <a href="">Link 2</a>
  <a href="">Link 3</a>
  <a href="">Link 4</a>
</div>

And these are the CSS styles:

#links {
    position: absolute;
    border: 1px solid #000;
}

#links a {
    display: block;
}

#links a:hover {
    background-color: #CCC;
}

This displays a list of links, the problem is that in IE, I can only click a link by directly clicking the text link, which is not the case with other browsers (where you can click anywhere whether the text link or anywhere else as long as it's in the link block), is there any fix for that (with only CSS, no javascript)?

Please note that I don't want to specify a width for the links or the div.

7条回答
来,给爷笑一个
2楼-- · 2019-03-18 06:25

Ok, the fix for this problem is to give the anchors a background property other than transparent. Some proposed to give the anchors a transparent background image. I have an addition to this: The image does not have to exist. You can simply write any path and it will make it work:

a {
  background:url('dummy/doesnotexist.png') no-repeat;
}
查看更多
▲ chillily
3楼-- · 2019-03-18 06:29

Enclose the link text in a span element. Then it will accept clicks anywhere within its bounds.

查看更多
Rolldiameter
4楼-- · 2019-03-18 06:42

Setting the background color to #FFF and an opacity of 0 worked for me in IE9, Chrome and Firefox. Don't know about other versions though. Setting it to transparent didn't help me.

This has the advantage of being pure CSS and cross-browser, so maybe it could be a better alternative.

查看更多
够拽才男人
5楼-- · 2019-03-18 06:43

I have no idea why, but giving the anchor a background color seemed to fix this problem for me.

查看更多
▲ chillily
6楼-- · 2019-03-18 06:45

I have had the same problem and none of the solutions above worked for me. I also needed the background of the links to be transparent.

A very uncomfortable solution, but one that worked perfectly is to set the background to a transparent gif. Only needs to be 1x1 px as it will repeat.

#links a
{
    display: block;
    background: url(/images/interface/blank/1dot.gif);
}

This seems to have no side effects apart from one additional request to the server.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-03-18 06:46

Insert this inside your a-tag style:

background:url('images/dot.png') no-repeat;

where dot.png is a 1x1 transparent image.

查看更多
登录 后发表回答