gpt4 book ai didi

html - 悬停不适用于导航菜单中的嵌套元素

转载 作者:可可西里 更新时间:2023-11-01 13:43:18 25 4
gpt4 key购买 nike

我正在尝试创建一个带有下拉子菜单的导航菜单。当我尝试将嵌套的“显示”属性从“无”更改为“阻止”时,它不起作用。下面是代码。

在代码中,我在 class="nav"下创建了主导航菜单。悬停在导航类中的

  • 元素上时需要下拉菜单。

    body {
    margin: 0;
    padding: 0;
    }

    .head {
    padding: 15px;
    }

    #contact span {
    font-weight: bold;
    }

    #contact {
    position: absolute;
    top: 30px;
    right: 10px;
    line-height: 5px;
    text-align: right;
    }

    .nav {
    background: #000;
    color: #fff;
    overflow: auto;
    padding: 15px 0px;
    }

    .nav>ul {
    list-style-type: none;
    padding: 0px 10px;
    float: right;
    margin: 0px;
    }

    .nav>ul>li {
    display: inline;
    padding: 15px 10px;
    }

    #products {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 200px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    #products li {
    padding: 9px 0px;
    }

    #services {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 100px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    #services li {
    padding: 9px 0px;
    }

    .nav>ul>li:hover {
    background: red;
    }


    /*Please check Code Here.*/

    .nav>ul>li:hover ul {
    display: block;
    }
    <!DOCTYPE html>
    <html>

    <head>
    <title>BASIC HTML PAGE</title>

    </head>

    <body>

    <div class="head">
    <h1>BUSINESS NAME</h1>
    <div id="contact">
    <p><span>Mobile:</span>+918050000824</p>
    <p><span>EMAIL:</span>garg.ishu@gmail.com</p>
    </div>
    </div>

    <div class="nav">
    <ul>
    <li>HOME</li>
    <li>ABOUT</li>
    <li class="productshome">PRODUCTS</li>
    <ul id="products">
    <li>PRODUCT-1</li>
    <li>PRODUCT-2</li>
    <li>PRODUCT-3</li>
    <li>PRODUCT-4</li>

    </ul>
    <li id="serviceshome">SERVICES
    <ul id="services">
    <li>SERVICE-1</li>
    <li>SERVICE-2</li>
    <li>SERVICE-3</li>
    <li>SERVICE-4</li>
    </ul>

    </li>
    <li>CONTACT</li>
    </ul>
    </div>

    </body>

    </html>

  • 最佳答案

    在此样式中使用 !important,因为您应用于 id(在 CSS 中具有最高优先级)的样式不会被您的样式覆盖。

    .nav>ul>li:hover ul {
    display: block !important;
    }

    也包括产品。 ul 不在 li 元素内。

    body {
    margin: 0;
    padding: 0;
    }

    .head {
    padding: 15px;
    }

    #contact span {
    font-weight: bold;
    }

    #contact {
    position: absolute;
    top: 30px;
    right: 10px;
    line-height: 5px;
    text-align: right;
    }

    .nav {
    background: #000;
    color: #fff;
    overflow: auto;
    padding: 15px 0px;
    }

    .nav>ul {
    list-style-type: none;
    padding: 0px 10px;
    float: right;
    margin: 0px;
    }

    .nav>ul>li {
    display: inline;
    padding: 15px 10px;
    }

    #products {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 200px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    #products li {
    padding: 9px 0px;
    }

    #services {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 100px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    #services li {
    padding: 9px 0px;
    }

    .nav>ul>li:hover {
    background: red;
    }


    /*Please check Code Here.*/

    .nav>ul>li:hover ul {
    display: block !important;
    }
    <!DOCTYPE html>
    <html>

    <head>
    <title>BASIC HTML PAGE</title>

    </head>

    <body>

    <div class="head">
    <h1>BUSINESS NAME</h1>
    <div id="contact">
    <p><span>Mobile:</span>+918050000824</p>
    <p><span>EMAIL:</span>garg.ishu@gmail.com</p>
    </div>
    </div>

    <div class="nav">
    <ul>
    <li>HOME</li>
    <li>ABOUT</li>
    <li class="productshome">PRODUCTS
    <ul id="products">
    <li>PRODUCT-1</li>
    <li>PRODUCT-2</li>
    <li>PRODUCT-3</li>
    <li>PRODUCT-4</li>

    </ul>
    </li>

    <li id="serviceshome">SERVICES
    <ul id="services">
    <li>SERVICE-1</li>
    <li>SERVICE-2</li>
    <li>SERVICE-3</li>
    <li>SERVICE-4</li>
    </ul>
    </li>
    <li>CONTACT</li>
    </ul>
    </div>

    </body>

    </html>

    productsservices 的情况下将您的 id 更改为 class,您的代码也将在没有重要的情况下工作。

    body {
    margin: 0;
    padding: 0;
    }

    .head {
    padding: 15px;
    }

    #contact span {
    font-weight: bold;
    }

    #contact {
    position: absolute;
    top: 30px;
    right: 10px;
    line-height: 5px;
    text-align: right;
    }

    .nav {
    background: #000;
    color: #fff;
    overflow: auto;
    padding: 15px 0px;
    }

    .nav>ul {
    list-style-type: none;
    padding: 0px 10px;
    float: right;
    margin: 0px;
    }

    .nav>ul>li {
    display: inline;
    padding: 15px 10px;
    }

    .products {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 200px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    .products li {
    padding: 9px 0px;
    }

    .services {
    list-style-type: none;
    width: 100px;
    height: 140px;
    position: absolute;
    top: 150px;
    right: 100px;
    background: red;
    margin: 0px;
    padding: 10px 10px;
    text-align: center;
    display: none;
    }

    .services li {
    padding: 9px 0px;
    }

    .nav>ul>li:hover {
    background: red;
    }


    /*Please check Code Here.*/

    .nav>ul>li:hover ul {
    display: block;
    }
    <!DOCTYPE html>
    <html>

    <head>
    <title>BASIC HTML PAGE</title>

    </head>

    <body>

    <div class="head">
    <h1>BUSINESS NAME</h1>
    <div id="contact">
    <p><span>Mobile:</span>+918050000824</p>
    <p><span>EMAIL:</span>garg.ishu@gmail.com</p>
    </div>
    </div>

    <div class="nav">
    <ul>
    <li>HOME</li>
    <li>ABOUT</li>
    <li class="productshome">PRODUCTS
    <ul class="products">
    <li>PRODUCT-1</li>
    <li>PRODUCT-2</li>
    <li>PRODUCT-3</li>
    <li>PRODUCT-4</li>

    </ul>
    </li>

    <li id="serviceshome">SERVICES
    <ul class="services">
    <li>SERVICE-1</li>
    <li>SERVICE-2</li>
    <li>SERVICE-3</li>
    <li>SERVICE-4</li>
    </ul>
    </li>
    <li>CONTACT</li>
    </ul>
    </div>

    </body>

    </html>

    关于html - 悬停不适用于导航菜单中的嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50899226/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com