CSS下拉菜单问题

时间:2010-03-21 19:01:04

标签: css

任何人都可以帮助解决一个小问题。

我有一个很简单的CSS下拉菜单

http://www.cinderellahair.co.uk/new/CSSDropdown.html

我遇到的问题是当您翻转一个菜单项时,该菜单项的内容比内容宽,它会向右推整个菜单。

除了缩短子菜单链接时,我可以对我的CSS进行调整以阻止这种情况发生吗?

CSS代码:

    /* General */
#cssdropdown, #cssdropdown ul { list-style: none; }
#cssdropdown, #cssdropdown * { padding: 0; margin: 0; }
#cssdropdown {padding:43px 0px 0px 0px;}

/* Head links */
#cssdropdown li.headlink { margin:0px 40px 0px -1px; float: left; background-color: #e9e9e9;}
#cssdropdown li.headlink a { display: block; padding: 0px 0px 0px 5px; text-decoration:none; }
#cssdropdown li.headlink a:hover { text-decoration:underline; }

/* Child lists and links */
#cssdropdown li.headlink ul { display: none; text-align: left; padding:10px 0px 0px 0px; font-size:12px; float:left;}
#cssdropdown li.headlink:hover ul { display: block; }
#cssdropdown li.headlink ul li a { padding: 5px; height: 17px; }
#cssdropdown li.headlink ul li a:hover { background-color: #333; }

/* Pretty styling */
body { font-family:Georgia, "Times New Roman", Times, serif; font-size: 16px; }
#cssdropdown a { color: grey; } #cssdropdown ul li a:hover { text-decoration: none; }
#cssdropdown li.headlink { background-color: white; }
#cssdropdown li.headlink ul { padding-bottom: 10px;}

HTML:

    <ul id="cssdropdown">
    <li class="headlink"><a href="http://www.cinderellahair.co.uk/new/index.php">HOME</a></li>
    <li class="headlink"><a href="http://www.cinderellahair.co.uk/new/gallery/gallery.php">GALLERY</a>
        <ul>
            <li><a href="http://amazon.com/">CELEBRITY</a></li>
            <li><a href="http://ebay.com/">BEFORE &amp; AFTER</a></li>
            <li><a href="http://craigslist.com/">HAIR TYPES</a></li>
        </ul>
    </li>
    <li class="headlink"><a href="http://www.cinderellahair.co.uk/new/about-cinderella-hair-extensions/about-us.php">ABOUT US</a>
        <ul>
            <li><a href="http://amazon.com/">WHY CHOOSE CINDERELLA</a></li>
            <li><a href="http://ebay.com/">TESTIMONIALS</a></li>
            <li><a href="http://craigslist.com/">MINI VIDEO CLIPS</a></li>
            <li><a href="http://craigslist.com/">OUR HAIR PRODUCTS</a></li>
        </ul>
    </li>
    <li class="headlink"><a href="http://www.cinderellahair.co.uk/new/news-and-offers/news.php">NEWS &amp; OFFERS</a>
        <ul>
            <li><a href="http://amazon.com/">VERA WANG FREE GIVEAWAY</a></li>
            <li><a href="http://ebay.com/">CINDERELLA ON TV</a></li>
            <li><a href="http://craigslist.com/">CINDERELLA IN THE PRESS</a></li>
            <li><a href="http://craigslist.com/">CINDRELLA NEWSLETTERS</a></li>
        </ul>
    </li>
    <li class="headlink"><a href="http://www.cinderellahair.co.uk/new/cinderella-salon/salon-finder.php">SALON FINDER</a></li>


</ul>

JS代码:

    $(document).ready(function(){
    $('#cssdropdown li.headlink').hover(
        function() { $('ul', this).css('display', 'block'); },
        function() { $('ul', this).css('display', 'none'); });
});

完整代码位于上面的链接上,只是查看源代码。

1 个答案:

答案 0 :(得分:1)

DEMO: http://jsbin.com/owuqe/edit

您需要像这样

设置UL菜单的位置

容器UL

  <ul id="cssdropdown" style="position:relative">

所有内部UL

  <ul style="position:absolute">