【问题标题】:Text over an image in Internet ExplorerInternet Explorer 中图像上的文本
【发布时间】:2012-02-09 18:29:03
【问题描述】:

我目前在 IE 中的 z-index 存在问题。

我有一个 div,这个 div 中有一个图像,这个图像上有一些文字。

这是我的 HTML 代码:

<section id="content_right">

                    <div class="mini_bloc_image">
                        <img alt="Camionnette VCI" src="img/mini_reparation_site_nb.png" />
                        <span><a href="#">R&eacute;paration sur site</a></span>
                        <span>Nous nous d&eacute;pla&ccedil;ons</span>
                    </div>

和 CSS:

#content_right {
width: 230px;
height: 484px;
float: right;
}

.mini_bloc_image {
height: 148px;
margin-bottom: 20px;
position: relative;
}

.mini_bloc_image > img {
position: absolute;
}

.mini_bloc_image > span:first-of-type {
display: block;
position: absolute;
top: 95px;
left: 0px;
font-size: 1.1em;
background-color: #ffffff;
padding: 4px 5px 4px 5px;
}

.mini_bloc_image > span:last-of-type {
display: block;
top: 95px;
left: 0px;
position: absolute;
left: 50px;
top: 125px;
color: #ffffff;
font-size: 1.1em;
font-family: 'Marck Script', cursive;
}

IE 不理解我的文字必须在图像上方...

我找到了一些类似 http://www.adrenatie.com/z-index-et-ie6/http://systembash.com/content/css-z-index-internet-explorer/ 的解决方案,但它不起作用。

有人可以帮帮我吗?

【问题讨论】:

  • 您是专门询问 IE6 的吗?
  • 你真的需要 :first-of-type 和 :last-of-type 吗?
  • 我猜是的,因为它们没有相同的字体和外观。

标签: html css internet-explorer z-index


【解决方案1】:

问题是您正在处理spans,默认情况下会内联呈现。如果你使用display:block,将使用z-index:

.mini_bloc_image > span:first-of-type {
display: block;
position: absolute;
z-index: 10;
font-size: 1.1em;
background-color: #ffffff;
padding: 4px 5px 4px 5px;
margin-top: 95px;
}

.mini_bloc_image > span:last-of-type {
display: block;
position: absolute;
z-index: 10;
color: #ffffff;
font-size: 1.1em;
font-family: 'Marck Script', cursive;
margin-left: 30%;
margin-top: 125px;
}

有关内联元素和定位的更多信息,请参阅this article

【讨论】:

  • @Greg 另请注意,IE 有一个“错误”,其中 z-index 堆栈将重置。我认为这不是您遇到的问题,Prutswonder 的解决方案看起来是正确的。但是,如果您遇到 z-index 问题,也可能是父元素没有 z-index 并且子元素 z-index 不会覆盖 DOM 树上更高的元素。这经常发生在下拉菜单中,我相信这是 IE 6、7 甚至 8 的问题。我相信 IE 9 没有这个问题。作为旁注。
  • 只是为了记录,在这种情况下您不需要 z-index,因为您的跨度在 HTML 和 DOM 树中的图像之后。根据经验,如果定位不遵循 DOM 树的流程,您只需要篡改 z-index。
  • 感谢您的回答。我试过了,我也试过用 div 替换我的跨度,但它没有用。我的文字总是在我的形象之下。也谢谢你的好文章。 [编辑:我刚刚阅读了 Fozzyuw 的评论并尝试了一些东西]
  • 刚刚注意到您使用的是position:absolute,但您没有设置职位。这可能就是文本显示在图像下方的原因。尝试使用lefttop 设置文本位置。 ;-)
  • 好吧.. 对不起,但它仍然不起作用,我必须做错事。我改变了:块显示,顶部,左侧,我删除了z-index(没用,正如你所说)。我改变了我最初的帖子,如果你想看...
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2015-01-23
  • 2015-10-16
相关资源
最近更新 更多