首页 > 解决方案 > CSS)为什么我的“悬停”不起作用?我搜索了谷歌,但我无法修复它

问题描述

我首先设置.submenudisplay:none;. 然后我设置.recipe:hover .submenu{display:block;} . 当我将鼠标悬停在 上时.recipe,我想做.submenu display: block;. 但它不起作用。你能告诉我为什么它不起作用吗?我实在想不通。使用javascript是我解决这个问题的唯一方法吗?这是我的代码。谢谢

<!DOCTYPE html>
<html>
   <meta charset="utf-8">
</html>
<head>
    <title>
       TEST
    </title>
    <style>
        body{
            margin: 0px;
        }
        #logo{
            height: 56px;
            padding-top: 15px;
            padding-left: 45px;
            font-size: 17px;
            font-weight: 500;
        }
        .title{
            padding-left: 30px;
            padding-bottom: 15px;
        }
        .mainmenu{
            border: 1px solid rgb(155,155,155);
            text-align: center;
            height: 30px;
            padding-top: 17px;
            padding-bottom: 5px;
            font-size: 17px;
            font-weight: 400;
            display: flex;
            list-style-type: none;
            margin: 0px;
            padding-top: 11px;
        }
        a:nth-of-type(1){
              flex-grow: 1;
            border-right: 1px solid rgb(155,155,155);
        }
        a:nth-of-type(2){
              flex-grow: 1;
            border-right: 1px solid rgb(155,155,155);
        }
        a:nth-of-type(3){
              flex-grow: 1;
        }
        .submenu{
            background-color: #f2efed;
            height: 190px;
            font-size: 16px;
            font-weight: 300;
            padding-top: 10px;
            padding-left: 10px;
            text-decoration: underline;
            position: absolute;
            margin-top: 36px;
            width: 100%;
            display: none;
        }
        a{
            color: black;
            text-decoration: none;
        }
        .recipe:hover .submenu{
            display: block;
        }
        @font-face { font-family: 'IBMPlexSansKR-Light'; src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_20-07@1.0/IBMPlexSansKR-Light.woff') format('woff'); font-weight: normal; font-style: normal; }
    </style>
</head>
<body>
 <div id="logo">
    <img width=45px src="icons8-solo-cup-100.png">
   <span class="title">cafe in my home</span>
 </div>
   <div class="mainmenu">
       <a href="Home.html">home</a>
       <a href="Recipe.html">Recipe</a>
       <a href="QNA.html">QNA</a>
       <div class="submenu">
           coffee
</div>
</div>
</body>

标签: javascripthtmlcsshover

解决方案


我不是最好的,但我想说这可能与选择.recipe:hover然后.submenu在它之后选择有关:

    .recipe:hover .submenu{
        display: block;
    }

推荐阅读