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
Recently Microsoft released Windows Server 2016, ...
I'm very happy. When encountering this proble...
This article example shares the specific code of ...
First, we need a server with Docker installed. (I...
Today I looked at some things related to data bac...
Problem description: I bought a Mac and installed...
1. The ul tag has a padding value by default in Mo...
Table of contents 1. Pull the image 2. Create a R...
Table of contents Declare fonts with font-face co...
Overview I have been using Docker for more than a...
This article uses examples to illustrate the prin...
Table of contents 1. Aggregate Query 1. COUNT fun...
Preface To be honest, I've been feeling very ...
This article example shares the specific code of ...
Table of contents initialization initState() init...