How to create a dropdown menu with pure CSS
Sep 20, 2025 am 02:19 AMUse HTML and CSS to create drop-down menus without JavaScript. 2. Trigger the submenu display through the :hover pseudo-class. 3. Use nested lists to build a structure, and set the hidden and suspended display effects in CSS. 4. Transition animation can be added to improve the visual experience.
A dropdown menu can be created using only HTML and CSS—no JavaScript required. The key is using the :hover pseudo-class to show a hidden submenu when a user hovers over a parent menu item. Below is a step-by-step guide to build a simple, functional dropdown menu.
1. HTML Structure
Start with a basic unordered list for your navigation. Use nested lists for the dropdown items.
2. Basic CSS Styling
Style the main menu as a horizontal bar. Hide the submenu by default, then display it on hover.
.menu {list-style: none;
padding: 0;
margin: 0;
background: #333;
}
.menu > li {
display: inline-block;
position: relative;
}
.menu a {
display: block;
padding: 15px 20px;
color: white;
text-decoration: none;
}
.menu a:hover {
background: #555;
}
3. Show Dropdown on Hover
Use CSS to hide the submenu initially and reveal it when the parent is hovered.
.submenu {list-style: none;
padding: 0;
margin: 0;
position: absolute;
top: 100%;
left: 0;
background: #444;
display: none;
min-width: 180px;
}
.dropdown:hover .submenu {
display: block;
}
.submenu li {
display: block;
}
.submenu a {
padding: 10px 15px;
}
.submenu a:hover {
background: #666;
}
4. Optional: Add Smooth Appearance
Improve the look with transitions and subtle effects.
.submenu {opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease-in-out;
}
.dropdown:hover .submenu {
display: block;
opacity: 1;
visibility: visible;
}
With this approach, the dropdown appears smoothly on hover and disappears when the mouse moves away. It works across modern browsers and is accessible via keyboard navigation when structured properly.
Basically just combine clean HTML structure with smart use of :hover and positioning in CSS.
The above is the detailed content of How to create a dropdown menu with pure CSS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Useobject-fitormax-widthwithheight:autotopreventimagedistortion;object-fitcontrolshowimagesfillcontainerswhilepreservingaspectratios,andmax-width:100%;height:autoensuresresponsivescalingwithoutstretching.

clamp() function realizes responsive font scaling through the minimum, preferred and maximum values; 2. The syntax is clamp (minimum value, preferred value, maximum value), and commonly used rem and vw units; 3. The font takes the minimum value on the small screen, and scales according to vw as the screen increases, and does not exceed the maximum value; 4. Reasonably select the numerical value to ensure readability and avoid being too large or too small; 5. Combining the rem type proportion to improve design consistency.

1. Open the web page printing interface, click "More Settings" and uncheck "Header and Footer" to remove automatically added URLs, dates and other information. 2. By adding the CSS style of @mediaprint{@page{margin:0}} to the web page code, the default margins and headers and footers can be cleared. 3. Install third-party printing extensions such as PrintEdit, which can edit print content more flexibly and disable the default header and footer.

Use aspect-ratio:1/1 to create a responsive square, and set the aspect ratio in modern browsers; if you need to be compatible with old browsers, you can use padding-top:100% technique to maintain the consistency of width and height by relative units; you can also use vw units to make the square change with the viewport.

Use backdrop-filter:blur() to achieve the frosted glass effect, combining rgba transparent background, thin borders and rounded corners, such as .frosted-card{backdrop-filter:blur(10px);background-color:rgba(255,255,255,0.1);border:1pxsolidrgba(255,255,255,0.2);border-radius:12px;padding:20px;}, be sure to ensure that there is content behind the elements and pay attention to browser compatibility.

Responsive design is implemented through media query, first defining the minimum width condition for mobile priority, then gradually adapting to the tablet and desktop, combining range, direction and resolution to optimize the layout.

Use transform:rotate() to achieve element rotation, support units such as deg, adjust the rotation center through transform-origin, and combine transition to achieve smooth animation effect.

Use the attribute selector to select elements with a specific data attribute, such as [data-status] select all elements containing the attribute, and [data-status="active"] select elements with the value active.
