【问题标题】:Horizontal CSS subnav issues!水平 CSS 子导航问题!
【发布时间】:2010-06-24 03:12:52
【问题描述】:

我一直在尝试让我的子导航在相关页面上处于活动状态,并且已经在论坛等网站上搜索了几天/几周!!我对 CSS 很陌生,如果有任何帮助,我将不胜感激! 我已经设法使用#current 使顶部导航按钮处于活动状态,但子导航没有跟随。我知道我可能错过了一些非常基本的东西,但我就是不明白!我希望它以类似于 www.forbes.com 的方式工作(通过另一个人的问题看到这一点,以为我也很接近!!) 任何帮助都会很棒。 谢谢 丽贝卡

请参阅下面的 CSS .....

<style>
ul#topnav {
 margin: 0 ; padding: 0 ;
 float: left;
 width: 955;
 list-style: none;
 position: relative;
 font-size: 12px;
 background: url(images/testbut.jpg) repeat-x;
}
ul#topnav :hover {
 font-family:Arial, Helvetica, sans-serif; 
 font-size:12px; color: #CCCCCC;
}
ul#topnav li {
 float: left;
 margin: 0; padding: 0;
 border-right: 1px solid #ffffff; /*--Divider for each parent level links--*/
}
ul#topnav li a {
 padding: 10px 27.5px;
 display: block;
 color: #f0f0f0;
 background-image: url(images/testbut.jpg) repeat-x;
 text-decoration: none;
 font-family:Arial, Helvetica, sans-serif;
 font-size:12px;
} 
ul#topnav li span {
 float: left;
 padding: 10px 0;
 position: absolute;
 left: 0; top:35px;
 display: none; 
 width: 100%;
 color: #fff;
 /*--Bottom right rounded corner--*/
 -moz-border-radius-bottomright: 5px;
 -khtml-border-radius-bottomright: 5px;
 -webkit-border-bottom-right-radius: 5px;
 /*--Bottom left rounded corner--*/
 -moz-border-radius-bottomleft: 5px;
 -khtml-border-radius-bottomleft: 5px;
 -webkit-border-bottom-left-radius: 5px;
}
ul#topnav li:hover span { display: block; background: #6a6a6a url() repeat-x;} /*--Show subnav on hover--*/
ul#topnav li span a { display: inline;} 
ul#topnav li span a:hover { font-family:Arial, Helvetica, sans-serif; font-size:12px;}
ul#topnav a#current { background: #6a6a6a url() repeat-x;}
</style>

【问题讨论】:

标签: css


【解决方案1】:

我不太确定从哪里开始使用您现有的代码,因此,我从头开始整理了一个工作示例。请注意,提供的示例非常基础,只是实现既定目标的一种方式。我已经在 Firefox 3.6 上对其进行了测试,仅此而已。可能存在跨浏览器兼容性问题,尤其是 Internet Explorer 中的 :hover 伪类。这段代码背后的想法只是为您提供一个简单的工作框架。

我强烈建议您查看以下文章,因为它们解释了嵌套列表的使用、跨浏览器兼容性问题、:hover 伪类 IE 问题的 Javascript 解决方案以及使用隐藏元素的可访问性问题display: none:

Suckerfish 下拉菜单
http://www.alistapart.com/articles/dropdowns

Suckerfish Dropdowns 之子
http://www.htmldog.com/articles/suckerfish/

用 CSS 隐藏:问题和解决方案
http://www.456bereastreet.com/archive/200905/hiding_with_css_problems_and_solutions/

好的,进入代码。首先,HTML。在这里,我使用嵌套的无序列表来创建菜单结构。这是一个很好的方法,因为导航菜单实际上只是一个嵌套的链接列表。对于那些不使用 CSS 的用户,这将正确显示,并且屏幕阅读器可以正确阅读。

您可能希望研究从活动页面中删除链接的方法。例如,如果活动页面是第 1-3 页,您可以将a 标记替换为span 标记。

HTML 应该是相当不言自明的。只需为活动菜单 UL 提供 active 类:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="test.css" media="screen" />
<title>Horizontal Menus</title>
</head>

<body>
    <div id="topnav">
        <ul>
            <li>Menu 1
                <ul class="active">
                    <li><a href="#">Page 1-1</a></li>
                    <li><a href="#">Page 1-2</a></li>
                    <li><a href="#">Page 1-3</a></li>
                    <li><a href="#">Page 1-4</a></li>
                </ul>
            </li>

            <li>Menu 2
                <ul>
                    <li><a href="#">Page 2-1</a></li>
                    <li><a href="#">Page 2-2</a></li>
                    <li><a href="#">Page 2-3</a></li>
                    <li><a href="#">Page 2-4</a></li>
                    <li><a href="#">Page 2-5</a></li>
                </ul>
            </li>
        </ul>
    </div>
</body>
</html>

CSS 中充满了 cmets,所以应该很容易理解。我认为最难的部分是正确格式化,并使子菜单水平而不是垂直。你已经做到了这一点,所以剩下的应该很容易:

/*
 * The contain DIV for the navigation menu.
 * Use this to position the menu on the page.
 */
#topnav {
    /*
     * Set the containing DIV position to
     * relative.  This allows the UL to be
     * positioned relative to this container.
     * See static, relative and absolute, here:
     * http://www.w3.org/TR/CSS2/visuren.html#choose-position
     */
    position: relative;
}

/*
 * All ULs (first and second level).
 */
#topnav ul {
    /*
     * Remove bullets from the UL.
     */
    list-style: none;
    /*
     * Zero margins and padding.
     */
    margin: 0;
    padding: 0;
    /* 
     * Take the UL out of the normal flows so
     * that it can be given absolute positioning
     * coordinates, relative to its containing
     * block (the #topnav DIV).
     */
    position: absolute;
    /*
     * Align the UL with the left of the #topnav DIV.
     */
    left: 0;
    /*
     * The list must be wide enough to enclose all
     * second level list items (5 x 10em).
     */
    width: 50em;
    /*
     * Give the UL height and a background colour.
     * This enables the UL that is activated during a
     * hover to mask the UL that is active.  See
     * the active and hover z-index settings.
     */
    height: 1.5em;
    background: #eeeeee;
}

/*
 * All LIs (first and second level).
 */
#topnav li {
    /*
     * Float the LIs left so that they all
     * appear on one line.
     */
    float: left;
    /*
     * Set the width of each LI.
     */
    width: 10em;
}

/*
 * Second level UL.
 */
#topnav ul ul {
    /*
     * Hide all second level LIs.
     * Set their position to absolute, then
     * move them left by 999em.
     */
    position: absolute;
    left: -999em;
}

/*
 * Second level UL.
 * Selected when first-level LI is hovered over,
 * or when first-level UI has the 'active' class.
 */
#topnav ul li:hover ul, #topnav ul ul.active {
    /*
     * Show active LIs.
     * Reset the left position to zero.
     */
    left: 0;
}

/*
 * Second level UL.
 * Selected when first-level LI is hovered over.
 */
#topnav ul li:hover ul {
    /*
     * Set the stacking level (z-index) so that it is
     * above the active UL.
     */ 
    z-index: 200;
    background: #cccccc;
}

/*
 * Second level UL.
 * Selected when first-level UI has the 'active' class.
 */
#topnav ul ul.active {
    /*
     * Set the stacking level (z-index) so that it is
     * below the UL that is displayed during hover.
     */ 
    z-index: 100;
    background: #aaaaaa;
}

祝你好运!

【讨论】:

  • 嗨迈克,我要试一试,我会让你知道我的进展如何!非常感谢您花时间帮助我,我真的很感激。
  • 嗨,迈克,我做到了!再次感谢你让我的生活变得如此轻松,我想我真的开始理解这一切了!!希望它可以在接下来的几周内全部上线!再次感谢。
  • @Beccs:我很高兴能帮上忙,也很高兴你能帮上忙。在最好的情况下,使用 CSS 进行布局可能会很棘手,但是当您添加浏览器兼容性问题以及隐藏和显示菜单等花哨的技巧时,它可能是一场噩梦。但是,如果您坚持使用符合 W3C 的代码,并关注像 A List Apart456 Berea Street 这样的好网站,仅举两个例子,它有望成为一种更愉快的追求。
【解决方案2】:

如果您使用的是静态页面,则为 HTML 中的链接/列表项设置一个活动类

例子:

index.html

<ul>
<li class="active">Home page</li>
<li>Content Page</li>
<li>Contact Page</li>
</ul>

内容.html

<ul>
<li>Home page</li>
<li class="active">Content Page</li>
<li>Contact Page</li>
</ul>

li{color: black;}
.active{color: red;}

如果您希望 subnav 处于活动状态,那么只需在 ul 上设置类即可:

<ul>
   <li>
   <ul class="active">
      <li class="active"></li>
      <li></li>
   </ul>
   </li>
   <li>
   <ul>
      <li class="active"></li>
      <li></li>
   </ul>
   </li>
</ul>

但是,如果您想要一个动态导航,它会变得有点棘手,但使用一些 PHP 就足够简单了。

【讨论】:

  • 嗨,感谢您的快速回复,但我的子导航代码在跨度标签内,它不是嵌套列表......而且活动的东西不想工作 - 或者我真的很昏暗!丽贝卡
  • 你的跨度没有意义。首先,它没有类,其次它没有结束标签!你有这个页面的在线版本吗?
  • 您好,它还没有上线,因为它不起作用!当我像在 中向它添加一个类时,它没有任何区别,它的结束标记是 不是吗?我有没有提到我是新手???!
  • 我看到您正在按照以下教程进行操作:sohtanaka.com/web-design/horizontal-sub-nav-with-css-jquery 活动的 CSS 类由 jquery 激活(隐藏和显示)。您确定在您的页面中添加了正确的 jquery 吗?
  • 该教程的标记不是很好,如果您是初学者,最好看看不使用 jquery 的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
相关资源
最近更新 更多