1

大家,我还是 HTML5 和 CSS3 的初学者

我希望有这样的效果。而且我尝试了很多次,但都没有实现。主要的

  • 尽管鼠标移动到它的孩子,但仍然保持颜色。

    导航示例

    CSS 代码应该是什么样子?

    要求的 HTML 5 代码。

    <!DOCTYPE html>
    <html>
    
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>HTML5, CSS3 and jQuery Navigation menu</title>
    <link rel="stylesheet" href="css/nav.css">
    <!--[if IE]>
        <script src="html5.js"></script>
    <![endif]-->
    </head>
    
    <body class="no-js">
        <nav id="topNav">
            <ul>
                <li><a href="#" title="Nav Link 1">HOME</a></li>
                <li><a href="#" title="Nav Link 1">SERVICES</a>
                    <ul>
                        <li><a href="#" title="Sub Nav Link 1">Sub Nav Link 1</a></li>
                        <li><a href="#" title="Sub Nav Link 2">Sub Nav Link 2</a></li>
                        <li><a href="#" title="Sub Nav Link 3">Sub Nav Link 3</a></li>
                        <li><a href="#" title="Sub Nav Link 4">Sub Nav Link 4</a></li>
                        <li class="last"><a href="#" title="Sub Nav Link 5">Sub Nav Link 5</a></li>
                    </ul>        
                </li>
                <li><a href="#" title="Nav Link 1">ABOUT US</a></li>
                <li class="last"><a href="#" title="Nav Link 1">ENQUITIES</a></li>
            </ul>
        </nav>      
        <p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."<p>
        <script src="js/jquery.js"></script>
        <script src="js/modernizr.js"></script>
    </body>
    
    </html>
    

    而这里是 CSS3

    /* JS disabled styles */
    .no-js nav li:hover ul 
    { 
        display:block; 
    }
    
    /* base nav styles */
    nav 
    {
        margin: 0 auto 20px;
        position: relative;
        background-color: #FFFFFF;
        font: 16px Tahoma, Sans-serif;
    }
    
    /*Temporaly Solution*/
    nav ul a 
    { 
       border-bottom: thin; 
       border-bottom-style: solid;      
       border-bottom-color: transparent;
    }
    /*Solution Ended*/
    
    nav ul 
    { 
        padding:0; 
        margin:0; 
    }
    
    nav li 
    { 
        position:relative; 
        float:left; 
        list-style-type:none; 
    }
    
    nav ul:after 
    { 
        content:"."; 
        display:block; 
        height:0; 
        clear:both; 
        visibility:hidden; 
    }
    
    nav ul a:hover 
    { 
        color: #F90; 
    }
    
    nav li a 
    {
        display: block;
        padding: 10px 20px;
        color: #333;
        text-decoration: none;
        font-weight: 600;
    }
    
    nav li a:focus 
    { 
        outline:none; 
        text-decoration:underline; 
    }
    
    nav li:first-child a 
    { 
        border-left:none; 
    }
    
    nav li.last a 
    { 
        border-right:none; 
    }
    
    nav a span 
    { 
        display:block; 
        float:right; 
        margin-left:5px; 
    }
    
    nav ul ul 
    { 
        display:none; 
        width:110%; 
        position:absolute; 
        left:0; 
        background:#FFFFFF; 
    }
    
    nav ul ul li 
    { 
        float:none; 
    }
    
    nav ul ul a 
    { 
        padding:5px 10px; 
        border-left:none; 
        border-right:none; 
        font-size:14px; 
    }
    
    /* CSS3 */
    
        .cssgradients nav li:hover { background-image:-moz-linear-gradient(0% 100px 90deg, #999, #222); background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#222), to(#555)); }
        .borderradius nav ul ul { -moz-border-radius-bottomleft:4px; -moz-border-radius-bottomright:4px; -webkit-border-bottom-left-radius:4px; -webkit-border-bottom-right-radius:4px; border-bottom-left-radius:4px; border-bottom-right-radius:4px; }
        .boxshadow.rgba nav ul ul {
            background-color: rgba(255,255,255,0.8);
            -moz-box-shadow: 2px 2px 2px rgba(0,0,0,.8);
            -webkit-box-shadow: 2px 2px 2px rgba(0,0,0,.8);
            box-shadow: 2px 2px 2px rgba(0,0,0,.8);
        }
        .rgba nav ul ul li { border-left:1px solid rgba(0,0,0,0.1); border-right:1px solid rgba(0,0,0,0.1); }
        .rgba nav ul ul a:hover {
            background-color: rgba(255,255,255,0.9);
        }
        .borderradius.rgba nav ul ul li.last { border-left:1px solid rgba(0,0,0,0.1); border-bottom:1px solid rgba(0,0,0,0.1); -moz-border-radius-bottomleft:4px; -moz-border-radius-bottomright:4px; -webkit-border-bottom-left-radius:4px; -webkit-border-bottom-right-radius:4px; border-bottom-left-radius:4px; border-bottom-right-radius:4px; }
        .csstransforms ul a span { -moz-transform:rotate(-180deg);-webkit-transform:rotate(-180deg); }
    
  • 4

    3 回答 3

    1

    试试这个:

    /* JS disabled styles */
    .no-js nav li:hover ul 
    { 
        display:block; 
    }
    
    /* base nav styles */
    nav 
    {
        margin: 0 auto 20px;
        position: relative;
        background-color: #FFFFFF;
        font: 16px Tahoma, Sans-serif;
    }
    
    /*Temporaly Solution*/
    nav ul a 
    { 
       border-bottom: thin; 
       border-bottom-style: solid;      
       border-bottom-color: transparent;
    }
    /*Solution Ended*/
    
    nav ul 
    { 
        padding:0; 
        margin:0; 
    }
    
    nav li 
    { 
        position:relative; 
        float:left; 
        list-style-type:none; 
    }
    
    nav ul:after 
    { 
        content:"."; 
        display:block; 
        height:0; 
        clear:both; 
        visibility:hidden; 
    }
    
    nav ul a:hover, nav ul > li:hover > a 
    { 
        color: #F90; 
    }
    
    nav li a 
    {
        display: block;
        padding: 10px 20px;
        color: #333;
        text-decoration: none;
        font-weight: 600;
    }
    
    nav li a:focus 
    { 
        outline:none; 
        text-decoration:underline; 
    }
    
    nav li:first-child a 
    { 
        border-left:none; 
    }
    
    nav li.last a 
    { 
        border-right:none; 
    }
    
    nav a span 
    { 
        display:block; 
        float:right; 
        margin-left:5px; 
    }
    
    nav ul ul 
    { 
        display:none; 
        width:110%; 
        position:absolute; 
        left:0; 
        background:#FFFFFF; 
    }
    
    nav ul ul li 
    { 
        float:none; 
    }
    
    nav ul ul a 
    { 
        padding:5px 10px; 
        border-left:none; 
        border-right:none; 
        font-size:14px; 
    }
    
    /* CSS3 */
    
        .cssgradients nav li:hover { background-image:-moz-linear-gradient(0% 100px 90deg, #999, #222); background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#222), to(#555)); }
        .borderradius nav ul ul { -moz-border-radius-bottomleft:4px; -moz-border-radius-bottomright:4px; -webkit-border-bottom-left-radius:4px; -webkit-border-bottom-right-radius:4px; border-bottom-left-radius:4px; border-bottom-right-radius:4px; }
        .boxshadow.rgba nav ul ul {
            background-color: rgba(255,255,255,0.8);
            -moz-box-shadow: 2px 2px 2px rgba(0,0,0,.8);
            -webkit-box-shadow: 2px 2px 2px rgba(0,0,0,.8);
            box-shadow: 2px 2px 2px rgba(0,0,0,.8);
        }
        .rgba nav ul ul li { border-left:1px solid rgba(0,0,0,0.1); border-right:1px solid rgba(0,0,0,0.1); }
        .rgba nav ul ul a:hover {
            background-color: rgba(255,255,255,0.9);
        }
        .borderradius.rgba nav ul ul li.last { border-left:1px solid rgba(0,0,0,0.1); border-bottom:1px solid rgba(0,0,0,0.1); -moz-border-radius-bottomleft:4px; -moz-border-radius-bottomright:4px; -webkit-border-bottom-left-radius:4px; -webkit-border-bottom-right-radius:4px; border-bottom-left-radius:4px; border-bottom-right-radius:4px; }
        .csstransforms ul a span { -moz-transform:rotate(-180deg);-webkit-transform:rotate(-180deg); }
    

    希望有效。

    于 2012-07-21T12:33:30.507 回答
    1

    您需要将:hover伪类分配给父 li 标签。

    nav ul li:hover > a 
    { 
        color: #F90; 
    }
    
    于 2012-07-21T13:19:46.583 回答
    1

    这将为所有链接元素添加颜色。要仅添加到第一个链接元素,请替换:

    nav ul a:hover 
    { 
        color: #F90; 
    }
    

    经过

    nav ul a:hover, nav ul > li:hover > a 
    { 
        color: #F90; 
    }
    

    这种方式有效。已经在本地试过了。

    于 2012-07-21T13:24:51.530 回答