Home > Web Front-end > CSS Tutorial > How Can I Create a Dropdown Menu Using Only CSS?

How Can I Create a Dropdown Menu Using Only CSS?

DDD
Release: 2024-12-26 19:59:11
Original
823 people have browsed it

How Can I Create a Dropdown Menu Using Only CSS?

Pure CSS-Based Dropdown Menu Creation

In web development, dropdown menus are a common UI element used to organize and present choices logically. If you desire a dropdown menu without relying on external frameworks or JavaScript, pure CSS can deliver a suitable solution.

Pure CSS Dropdown Menu Implementation

To construct a purely CSS-based dropdown menu, follow these steps:

  1. HTML Structure: Define an unordered list (<ul>), with each list item (
  2. ) representing a menu item. Inside the list items, nest other unordered lists for submenus.
  3. Styling: Use CSS to style the menu. Set the ul to have no bullets, no margins, and a horizontal alignment. Position the li elements inline-block for horizontal alignment.
  4. Dropdown Visibility: To toggle the visibility of submenus, use the :hover pseudo-class on list items. When a list item is hovered over, its submenu (ul) becomes visible.
  5. Menu Appearance: Customize the appearance of menu items, submenus, and links using CSS properties like background-color, color, and padding.

Example Code

The following code snippet provides an example of a pure CSS-based dropdown menu:

HTML:

<ul>
Copy after login

CSS:

ul {
  font-family: Arial, Verdana;
  font-size: 14px;
  margin: 0;
  padding: 0;
  list-style: none;
}

ul li {
  display: inline-block;
  position: relative;
}

li ul {
  display: none;
}

ul li a {
  display: block;
  text-decoration: none;
  color: #ffffff;
  border-top: 1px solid #ffffff;
  padding: 5px 15px 5px 15px;
  background: #2C5463;
  margin-left: 1px;
  white-space: nowrap;
}

ul li a:hover {
  background: #617F8A;
}

li:hover ul {
  display: block;
  position: absolute;
}

li:hover li {
  float: none;
  font-size: 11px;
}

li:hover a {
  background: #617F8A;
}

li:hover li a:hover {
  background: #95A9B1;
}
Copy after login

Live Demo: [jsfiddle.net/XPE3w/7/](http://jsfiddle.net/XPE3w/7/)

The above is the detailed content of How Can I Create a Dropdown Menu Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template