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 use TypeScript in Vue

introduction In recent years, the call for TypeSc...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

How to use the Marquee tag in XHTML code

In the forum, I saw netizen jeanjean20 mentioned h...

Summary of various methods for JavaScript to determine whether it is an array

Table of contents Preface Array.isArray construct...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

MySQL database operation and maintenance data recovery method

The previous three articles introduced common bac...

Use of TypeScript Generics

Table of contents 1. Easy to use 2. Using generic...

Detailed tutorial on installing SonarQube using Docker

Table of contents 1. Pull the image 1.1 Pull the ...

Detailed analysis of the principles and usage of MySQL views

Preface: In MySQL, views are probably one of the ...

How to modify Ubuntu's source list (source list) detailed explanation

Introduction The default source of Ubuntu is not ...

jQuery implements accordion effects

This article shares the specific code of jQuery t...

A brief discussion on React native APP updates

Table of contents App Update Process Rough flow c...

...