如何让子菜单下拉到父菜单下面,而不是飞出?

时间:2012-12-11 09:47:10

标签: html css

我制作了一个快节奏菜单的jsFiddle:

http://jsfiddle.net/ZSMLg/1/

以下是我从以下帖子中获得的代码:

Simple CSS fly out list, not so simple?

<ul>
    <li>home</li>
    <li>products
        <ul>
            <li><a href="#">CPUs</a>
                <ul>
                    <li><a href="#">AMD<a>
                        <ul>
                            <li><a href="#">AM2</a></li>
                            <li><a href="#">AM3</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Intel</a></li>
                </ul>
            </li>
            <li><a href="#">Motherboards</a></li>
            <li><a href="#">PSUs</a></li>
            <li>Hard drives
                <ul>
                    <li><a href="#">HDD</a></li>
                    <li><a href="#">SSD</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li>Tracking</li>
</ul>

CSS如下:

ul {list-style-type: none; width: 8em; border: 1px solid #000; padding: 0;}
/* just to set the base-line for the ul, but note the width. It's important. */

ul li {position: relative; border-top: 1px solid #000; margin: 0; padding: 0; }
/* the position: relative is used to allow its child elements to be absolutely positioned */

ul li:first-child {border-top: 0 none transparent; }
/* to avoid a two-pixel border on the first li (1px li-border + 1px ul-border) */

ul li:hover {background-color: #f90; }
/* just to aid visually */

ul ul {position: absolute; top: -1px; left: 8em; display: none; }
/* sets up all ul elements beneath the parent ul, the -px is to counter the movement forced by the border, bear in mind that the li:first-child doesn't *have* a border, so adjust to taste */

ul > li:hover > ul {display: block; }
/* makes the nested list appear if the parent-li is hovered */

如果我希望子菜单位于父菜单下方,稍微向左推?那么CSS应该如何?

1 个答案:

答案 0 :(得分:0)

尝试删除position: absolute;,在css中替换此代码:

ul ul {position: absolute; top: -1px; left: 8em; display: none; }

ul > li:hover > ul {display: block; }

有了这个:

ul ul { display: none; }

ul > li:hover > ul { margin-left:20%; display: block; }