Example of using HTML+CSS to implement a secondary menu bar when the mouse is moved

Example of using HTML+CSS to implement a secondary menu bar when the mouse is moved

This article introduces an example of using HTML+CSS to implement a secondary menu bar when the mouse is moved. The details are as follows:

First, the effect picture:

1. The mouse is not on it

2. Place the mouse on the first-level menu to expand the second-level menu

3. Place the mouse on the secondary menu

Code:

<html>
<head>
    <title>Secondary menu test</title>
    <meta charset="utf-8">
    <style type="text/css">

    /*To center the menu*/
    body {
        padding-top:100px;
        text-align:center; 
    }
    
    
    /* -------------Menu css code----------begin---------- */
    .menuDiv { 
        border: 2px solid #aac; 
        overflow: hidden; 
        display:inline-block;
    }
    
    /* Remove the underline of the a tag*/
    .menuDiv a {
        text-decoration: none;
    }
    
    /* Set the style of ul and li */
    .menuDiv ul , .menuDiv li {
        list-style: none;
        margin: 0;
        padding: 0;
        float: left;
    } 
    
    /* Set the secondary menu to absolute positioning and hide it*/
    .menuDiv > ul > li > ul {
        position: absolute;
        display: none;
    }

    /* Set the style of the li of the secondary menu */
    .menuDiv > ul > li > ul > li {
        float: none;
    }
  
    /* Put the mouse on the first-level menu to display the second-level menu*/
    .menuDiv > ul > li:hover ul {
        display: block;
    }

    /* First level menu */
    .menuDiv > ul > li > a {
        width: 120px;
        line-height: 40px;
        color: black;
        background-color: #cfe;
        text-align: center;
        border-left: 1px solid #bbf;
        display: block;
    }
    
    /* In the first level menu, do not set the left border */
    .menuDiv > ul > li:first-child > a {
        border-left: none;
    }

    /* In the first level menu, the style of the mouse on it*/
    .menuDiv > ul > li > a:hover {
        color: #f0f;
        background-color: #bcf;
    }

    /* Secondary menu */
    .menuDiv > ul > li > ul > li > a {
        width: 120px;
        line-height: 36px;
        color: #456;
        background-color: #eff;
        text-align: center;
        border: 1px solid #ccc;
        border-top: none;
        display: block;
    }
    
    /* In the secondary menu, the first one sets the top border*/
    .menuDiv > ul > li > ul > li:first-child > a {
        border-top: 1px solid #ccc;
    }
    
    /* In the secondary menu, the style of the mouse on it*/
    .menuDiv > ul > li > ul > li > a:hover {
        color: #a4f;
        background-color: #cdf;
    }
    /* -------------Menu css code----------end---------- */
    
    </style>
</head>
<body>

    <!-- -------Menu html code---------begin------- -->
    <div class="menuDiv">
        <ul>
            <li>
                <a href="#">Menu 1</a>
                <ul>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                </ul>
            </li> 
            <li>
                <a href="#">Menu 2</a>
                <ul>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                </ul>
            </li> 
            <li>
                <a href="#">Menu Three</a>
                <ul>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                </ul>
            </li> 
            <li>
                <a href="#">Menu 4</a>
            </li> 
            <li>
                <a href="#">Menu Five</a>
                <ul>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                    <li><a href="#">Secondary Menu</a></li>
                </ul>
            </li> 
        </ul>
    </div>
    <!-- -------Menu html code---------end------- -->
    
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to use nginx as a proxy cache

>>:  CSS imitates Apple's smooth switch button effect

Recommend

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

js tag syntax usage details

Table of contents 1. Introduction to label statem...

JavaScript to make the picture move with the mouse

This article shares the specific code of JavaScri...

Detailed explanation of fuser command usage in Linux

describe: fuser can show which program is current...

How to communicate between WIN10 system and Docker internal container IP

1. After installing the Windows version of Docker...

How to add configuration options to Discuz! Forum

Discuz! Forum has many configuration options in th...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

How to use JS to check if an element is within the viewport

Preface Share two methods to monitor whether an e...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

How to implement a single file component in JS

Table of contents Overview Single file components...

Summary of how to modify the root password in MySQL 5.7 and MySQL 8.0

MySQL 5.7 version: Method 1: Use the SET PASSWORD...

Form submission page refresh does not jump

1. Design source code Copy code The code is as fol...

WeChat applet implements the Record function

This article shares the specific code for the WeC...