css - 如何让我的标签背景位于我的 div 背景之上?

标签 css html

新手问题。我正在尝试使用 z-index,但它似乎没有按我预期的方式工作。这是我的代码:

<a id="favoritelink" href="#" style="margin-left: 600px" class="addtofavorites" title="Add to Favorites"></a>
<div class="description" style="margin-top: -18px">
   Some description
</div>

在 css 中,我将 .description 的 z-index 指定为 1,将 .addtofavorites 的 z-index 指定为 10。#favoritelink 有一个背景图像和远离页面的文本(它本质上是一个图像链接)。 .description 的背景仍然位于 .addtofavorites 的背景之上。

我希望 .addtofavorites 背景位于顶部。

这是 .addtofavorites 和 .description 的 css:

.description
{
    background:#efefef;
    width:600px;
    max-height:500px;
    padding:12px;
    z-index:1;
}
.addtofavorites
{
    background:url(img/plus.png) no-repeat center;
    background-size:75%;
    display:block;
    text-indent:-9999px;
    width:51px;
    height:56px;
    margin-right:6px;
    z-index:10;
 }

最佳答案

您必须使用 position: relativeposition: absolute 才能使 z-index 正常工作。

编辑:http://jsfiddle.net/GndRj/4/ 根据您的代码,但在 .favoritelink 上添加了 position: relative 并减少了 margin-left,以便它显示在预览窗口中。

关于css - 如何让我的标签背景位于我的 div 背景之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620518/

相关文章:

css - 2px 差异 FF 和 Chrome

css - 为什么我的 <div> 没有漂浮在另一个漂浮的 <div> 周围,而其他元素却漂浮着?

css - flex 盒子不工作我希望它如何工作

javascript - 进度条动画不起作用

javascript - 检查元素是否存在并开始一些事件

c# - ASP.NET 中的跨度 ID 更改

javascript - 触摸事件后移动 div

javascript - 如何滚动到单击时移出屏幕的 div

javascript - 打开列表项作为新覆盖

css - 在 MVC 3 Razor 中垂直对齐文本框