如何在<a> tag?</a>中使用z-index

时间:2010-10-01 12:37:32

标签: css

我有以下html,我希望它与现在看起来完全相反,但我已经尝试了很多,没有什么进展顺利,具体我想要A链接在顶部链接,B在它下面,B在B下面,依此类推...... 我试图将z-index设置为它们,但它没有奏效。 请向我解释我的错。

HTML:

<html>
<head>
    <title>Test page</title>
    <link href="style.css" type="text/css" rel="Stylesheet" media="screen" />
</head>
<body>
    <div class="d1">
        <a href="#" class="a1" style="background-color: #000;">A</a>
        <ul class="b1">
            <li>
                <a href="#" class="a2" style="background-color: #222;">B</a>
            </li>
            <li>
                <a href="#" class="a2" style="background-color: #444;">C</a>
            </li>
            <li>
                <a href="#" class="a2" style="background-color: #666;">D</a>
            </li>
            <li>
                <a href="#" class="a2" style="background-color: #888;">E</a>
            </li>
        </ul>
    </div>
</body>
</html>

CSS:

div.d1
{
    width: 100px;
    height: 160px;
    float: left;
    margin: 0 auto;
    padding: 10px 50px 15px 50px;

    background-color: Red;
}

a.a1
{
    display: block;
    width: 100px;
    height: 130px;
    float: left;

    margin: 0 auto;
    padding: 30px 0 0 0;
}
a.a1:hover
{
    color: Red;
}

ul.b1
{
    display: block;
    width: 100px;
    float: left;

    position: relative;
    left: 25px;
    top: -160px;

    list-style: none;
    margin: 0;
    padding: 0;
}
ul.b1 li
{
    display: block;
    width: 25px;
    height: 160px;
    float: left;
    z-index: -1;
}
ul.b1 li a.a2
{
    display: block;
    width: 100px;
    height: 130px;

    margin: 0 auto;
    padding: 30px 0 0 0;
}
ul.b1 li a.a2:hover
{
    color: Red;
}

2 个答案:

答案 0 :(得分:7)

您需要提供<a>定位,因为要z-index工作,需要将定位设置为static以外的值,最简单的方法是将其设置为relative a { position: relative; z-index: 5; }

示例: http://jsfiddle.net/2JsvU/

{{1}}

答案 1 :(得分:1)

z-index要求position为相对或绝对。将<a>标记设置为position:relative;,它应该开始服从z-index。