简体   繁体   English

使用display:block链接时IE出现问题

[英]Problem with IE when using display:block for links

This is my HTML: 这是我的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: 这些是CSS样式:

#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)? 这会显示一个链接列表,问题是在IE中,我只能通过直接单击文本链接来单击链接,而其他浏览器则不是这种情况(您可以在任意位置单击文本链接或其他任何位置,只要单击因为它在链接块中),对此是否有任何修复方法(只有CSS,没有javascript)?

Please note that I don't want to specify a width for the links or the div. 请注意,我不想为链接或div指定宽度。

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. 一种非常不舒服的解决方案,但将其设置为透明gif却是完美的解决方案,但效果很好。 Only needs to be 1x1 px as it will repeat. 只需重复1x1像素即可。

#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. 除了向服务器发出一个附加请求外,这似乎没有任何副作用。

Put position:relative; 放置位置:相对; in your CSS at #links a{ } 在CSS中的#links a {}

like this 像这样

It will fix it :) 它将解决它:)

我不知道为什么,但是给锚定背景颜色似乎可以解决这个问题。

Enclose the link text in a span element. 将链接文本括在span元素中。 Then it will accept clicks anywhere within its bounds. 然后,它将接受其范围内任意位置的点击。

Setting the background color to #FFF and an opacity of 0 worked for me in IE9, Chrome and Firefox. 在IE9,Chrome和Firefox中,将背景颜色设置为#FFF且不透明度为0可以为我工作。 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. 这具有纯CSS和跨浏览器的优势,因此也许是更好的选择。

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;
}

Insert this inside your a -tag style: 将其插入您a -tag样式中:

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

where dot.png is a 1x1 transparent image. 其中dot.png是1x1透明图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM