Hoy crearemos un menú de navegación desplegable que se desplegará cuando el usuario haga clic en un botón.

Paso 1: Configuración básica del HTML Primero, crea un archivo HTML y agrega el siguiente código:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Menú Desplegable</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button id="toggleMenu">Mostrar Menú</button>
<nav id="menu">
<ul>
<li><a href="#">Inicio</a></li>
<li><a href="#">Acerca de</a></li>
<li><a href="#">Servicios</a></li>
<li><a href="#">Contacto</a></li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
Paso 2: Estilo del menú con CSS Crea un archivo CSS llamado styles.css y agrega el siguiente código:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
button {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
}
nav {
display: none;
background-color: #f1f1f1;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav ul li {
padding: 10px;
text-align: center;
border-bottom: 1px solid #ccc;
}
nav ul li:last-child {
border-bottom: none;
}
nav a {
text-decoration: none;
color: #333;
display: block;
}
nav a:hover {
background-color: #ddd;
}
Paso 3: Funcionalidad del menú con JavaScript Crea un archivo JavaScript llamado script.js y agrega el siguiente código:
// Obtener el botón y el menú
const toggleButton = document.getElementById('toggleMenu');
const menu = document.getElementById('menu');
// Agregar un evento clic al botón
toggleButton.addEventListener('click', () => {
// Si el menú está oculto, mostrarlo; si está visible, ocultarlo
if (menu.style.display === 'none') {
menu.style.display = 'block';
} else {
menu.style.display = 'none';
}
});
Paso 4: Visualizar el menú Abre el archivo HTML en tu navegador y deberías ver un botón que dice «Mostrar Menú». Al hacer clic en el botón, el menú desplegable se mostrará y se ocultará alternativamente.
Con esto, has creado un sencillo menú desplegable utilizando HTML, CSS y JavaScript. Puedes personalizar el estilo y la funcionalidad según tus necesidades.

Ingeniero en Informática, Investigador, me encanta crear cosas o arreglarlas y darles una nueva vida. Escritor y poeta. Más de 20 APPs publicadas y un libro en Amazon.