Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

html - Hover Content is not showing in Drop Down Menu (Web Design)

I am writing a web and the drop-down menu button is not working. The hoverable content is not shown. May anyone tell me the reason, please. Thanks a lot!!!

Here is the code

        * {
            box-sizing: border-box;
            font-family: Arial, Helvetica, sans-serif;
        }

        h1.header {
            text-align: center;
            color: #66F3ED;
        }

        body {
            margin: 0;
        }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #808080;
            height: 70px;
            font-size: 18px;
        }
        li {
            float: left;
            height: 70px;
        }
        li:last-child {
                border-bottom: none;
            }
        li a {
                display: block;
                color: #66f3ed;
                text-align: center;
                padding: 23px 60px;
                text-decoration: none;
            }
        li a:hover:not(.active) {
             background-color: #111;
        }

        .active {
            background-color: #808080;
            border-bottom: 5px solid #66f3ed;
        }

        /* Create three unequal columns that floats next to each other */
        .column {
            float: left;
            height: 800px;
        }
        /* Left and right column */
        .column.side {
                width: 6%;
                background-color: #868686;
                overflow: hidden;
                z-index:2;
        }
        /* Middle column */
        .column.middle {
            width: 70%;
            background-color: #777777;
            z-index:1;
        }
        .column.right {
            width: 24%;
            background-color: #919191;
        }
        /* Clear floats after the columns */
        .row:after {
            content: "";
            display: table;
            clear: both;
        }

 /*Drop down Menu*/
 .dropbtn {
  background-color: transparent;
  color: #66F3ED;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown {
  display: inline-block;
  z-index: 10!important;
}

.dropdown-content {
   display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  left:100%;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


        /* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
        @media screen and (max-width: 600px) {
            .column.side, .column.middle {
                width: 100%;
            }
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS Website Layout</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="row">
        <!--Side Menu Bar-->
        <div class="column side">
            <div class="dropdown">
                <button class="dropbtn"><img src="Menu/Menu-1.png" style="width: 100%" alt="Menu"></button>
            </div>
            
            <br /><br />
            <img src="menu.jpg" alt="Menu">
        </div>
        <div class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
        </div>
        <div class="column middle">
            <ul>
               <li><img src="sa_crop.gif" style="height: 70px"alt="Logo"></li> 
                <li><a class="active" href="#home">Face Mask Detection</a></li>
                <li><a href="#news">Social Distance Detection</a></li>
            </ul>
            <br /><h1 class="header">Face Mask Detection</h1><br />
            <!--Face Mask Detection Area-->
        </div>

        <div class="column right">
            <ul>
                <li style="float:right"><img src="user_808080.jpg" style="height: 70px" alt="User"></li> 
            </ul>
         
         </div>
    </div>

</body>
</html>
question from:https://stackoverflow.com/questions/65869820/hover-content-is-not-showing-in-drop-down-menu-web-design

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have several issues in both your HTML and CSS to solve, so the menu will work correctly.

First: The <div class="dropdown"> should contins the button, image and dropdown-content inside </div>

for example, change:

        <div class="column side">
            <div class="dropdown">
                <button class="dropbtn"><img src="Menu/Menu-1.png" style="width: 100%" alt="Menu"></button>
            </div>
            
            <br /><br />
            <img src="menu.jpg" alt="Menu">
        </div>
        <div class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
        </div> 

to

           <div class="column side">
                <div class="dropdown">
                    <button class="dropbtn"><img src="Menu/Menu-1.png" style="width: 100%" alt="Menu"></button>
              
                    <br /><br />
                    <img src="menu.jpg" alt="Menu">
                    <div class="dropdown-content">
                        <a href="#">Link 1</a>
                        <a href="#">Link 2</a>
                    </div>
                </div>
          </div>

Second: in the style of .dropdown-content, using of position: absolute; is correct but you should change left:100%; to left:0;, therefore you get the dropdown-content beside the menu-item.

Third: Add the CSS style code for hovering over the menu item, to make the sub-menu-item appears as:

    .dropdown:hover .dropdown-content{
      display: block;
    }

Fourth: Change the background of the sub-menu-item when hovering, for example

    .dropdown-content a:hover {
      color: white;
      background-color: red;
    }

if you make the above four points, the menu will behave as expected.

and here is the full modified code of the question- just press Run the code snippet below:

        * {
            box-sizing: border-box;
            font-family: Arial, Helvetica, sans-serif;
        }

        h1.header {
            text-align: center;
            color: #66F3ED;
        }

        body {
            margin: 0;
        }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #808080;
            height: 70px;
            font-size: 18px;
        }
        li {
            float: left;
            height: 70px;
        }
        li:last-child {
                border-bottom: none;
            }
        li a {
                display: block;
                color: #66f3ed;
                text-align: center;
                padding: 23px 60px;
                text-decoration: none;
            }
        li a:hover:not(.active) {
             background-color: #111;
        }

        .active {
            background-color: #808080;
            border-bottom: 5px solid #66f3ed;
        }

        /* Create three unequal columns that floats next to each other */
        .column {
            float: left;
            height: 800px;
        }
        /* Left and right column */
        .column.side {
                width: 6%;
                background-color: #868686;
                overflow: hidden;
                z-index:2;
        }
        /* Middle column */
        .column.middle {
            width: 70%;
            background-color: #777777;
            z-index:1;
        }
        .column.right {
            width: 24%;
            background-color: #919191;
        }
        /* Clear floats after the columns */
        .row:after {
            content: "";
            display: table;
            clear: both;
        }

 /*Drop down Menu*/
 .dropbtn {
  background-color: transparent;
  color: #66F3ED;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown {
  display: inline-block;
  z-index: 10!important;
}

.dropdown-content {
   display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  left:0;    /* Modified */
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* adding */
.dropdown:hover .dropdown-content{
  display: block;
}

/* adding */
.dropdown-content a:hover {
  color: white;
  background-color: red;


        /* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
        @media screen and (max-width: 600px) {
            .column.side, .column.middle {
                width: 100%;
            }
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS Website Layout</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="row">
        <!--Side Menu Bar-->
        <div class="column side">
            <div class="dropdown">
                <button class="dropbtn"><img src="Menu/Menu-1.png" style="width: 100%" alt="Title"></button>
          
            <br /><br />
            <img src="menu.jpg" alt="MenuItem">
            <div class="dropdown-content">
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
            </div>
            </div>
      </div>
          <div class="column middle">
            <ul>
               <li><img src="sa_crop.gif" style="height: 70px"alt="Logo"></li> 
                <li><a class="active" href="#home">Face Mask Detection</a></li>
                <li><a href="#news">Social Distance Detection</a></li>
            </ul>
            <br /><h1 class="header">Face Mask Detection</h1><br />
            <!--Face Mask Detection Area-->
        </div>

        <div class="column right">
            <ul>
                <li style="float:right"><img src="user_808080.jpg" style="height: 70px" alt="User"></li> 
            </ul>
         
         </div>
    </div>

</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...