Home  >  Article  >  Web Front-end  >  css menu show and hide

css menu show and hide

WBOY
WBOYOriginal
2023-05-09 10:29:37859browse

CSS menu display and hiding

CSS is one of the essential skills for front-end developers. Today we will learn how to use CSS to display and hide menus. In web design, menu is a very important element. It can improve user experience, optimize page layout, improve website navigation and search functions, etc.

CSS can realize the display and hiding of menus. The specific implementation method is as follows:

1. Use the CSS selector to select the elements that need to be hidden:

.menu {
     display: none;
}

In the above code. menu means selecting an element with class menu, and display:none means hiding this element.

2. Add CSS style to the element that needs to display the menu:

#show-menu:hover + .menu {
     display: block;
}

#show-menu in the above code means selecting an element with the id of show-menu, and : means selecting the following element A sibling element, .menu means selecting the element with class menu. The .menu element is displayed when the mouse is hovered over #show-menu.

The complete code is as follows:

<style>
.menu {
     display: none;
}
#show-menu:hover + .menu {
     display: block;
}
</style>

<div id="show-menu">显示菜单</div>
<ul class="menu">
     <li><a href="#">菜单项1</a></li>
     <li><a href="#">菜单项2</a></li>
     <li><a href="#">菜单项3</a></li>
     <li><a href="#">菜单项4</a></li>
</ul>

In the above code, when the mouse hovers over "Show Menu", the menu will be displayed, and when the mouse leaves, the menu will disappear.

In addition, CSS also provides a variety of other methods to display and hide menus, including: using transition in CSS3, using JavaScript, etc. Developers can choose which method to use based on actual needs.

Through the study of this article, I believe that everyone has mastered the method of displaying and hiding menus in CSS. This is a very basic skill for front-end developers and an important way to improve website user experience.

The above is the detailed content of css menu show and hide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn