JS implements a hover drop-down menu. This is a scenario question encountered in front-end interviews. The principle is to modify the attribute value of the display attribute of the menu style from none=>block. The specific implementation is shown below. Focus on the following parts.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hover drop-down menu</title> <style> /* Remove existing styles if necessary for wildcards, a, and li*/ * { margin: 0; padding: 0; } a { text-decoration: none; color: black; } /* li here only removes the existing style without specifying the width*/ li { list-style: none; } /* Flex layout of each section as an item */ .container { margin: 50px auto; width: 40%; height: 40px; display: flex; /* space-evenly first seen*/ justify-content: space-evenly; background-color: skyblue; } /* Floating only needs to be done in each section*/ /* Set "font size, text alignment and line height" for section => inheritable properties*/ .section { float: left; font-size: 16px; line-height: 40px; text-align: center; } /*Here specifies the style of the head when hovering*/ /* Can also be written as .head:hover */ .section:hover .head { color: white; background-color: orange; } /* The entire menu is invisible at first and the style is set*/ .menu { display: none; background-color: transparent; } /* You can see the menu after hovering. You can only hover the parent box*/ .section:hover .menu { display: block; } /* Specifies the style of li when hovering*/ .menu li:hover { background-color: orange; } </style> </head> <body> <div class="container"> <div class="section"> <a href="#" class="head">Write a paper</a> <ul class="menu"> <li>Search for information</li> <li>Take notes</li> <li>Reproduce</li> </ul> </div> <div class="section"> <a href="#" class="head">Learn front-end</a> <ul class="menu"> <li>HTML</li> <li>CSS</li> <li>JavaScript</li> <li>LeetCode</li> </ul> </div> <div class="section"> <a href="#" class="head">Little Days</a> <ul class="menu"> <li>Eat</li> <li>Sleep</li> <li>Play Beans</li> </ul> </div> </div> </body> </html> The final effect is as follows. Tips: There is another similar question which is to implement a drop-down menu by clicking. The difference here is that you need to add a click event, write it in JS, and then supplement it later. You can also choose to write the three sections in the form of ul li, so that nested two layers of ul can also be achieved, with better semantics, left for readers' reference. 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. You may also be interested in:
|
<<: Problems and solutions for installing Docker on Alibaba Cloud
>>: Detailed steps to install the NERDTree plugin in Vim on Ubuntu
Regarding the connection method between Java and ...
“How to make a website look high-end? Or more des...
MySQL5.6 How to create SSL files Official documen...
Preface Note: The test database version is MySQL ...
Table of contents 1. Scope 1. Global scope 2. Loc...
A long time ago, I summarized a blog post titled ...
<br />English address: http://developer.yaho...
This article shares the specific code of JavaScri...
Table of contents Intro Nginx Dockerfile New conf...
1. Global Object All modules can be called 1) glo...
One of our web projects has seen an increase in t...
Install Docker You have to install Docker, no fur...
Table of contents 1. Introduction 1. Basic layout...
TeamCenter12 enters the account password and clic...
Original article, please indicate the author and ...