<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
style
>
.dropdown {
left: 40%;
text-align: center;
position: relative;
display: inline-block;
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
</
style
>
<
title
>Dropdown Example</
title
>
</
head
>
<
body
>
<
div
class
=
"dropdown"
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
h2
>
Click the below button to see the
<
br
/>dropdown menu.
</
h2
>
<
button
onclick
=
"toggleDropdown()"
class
=
"dropbtn"
>
Click me
</
button
>
<
div
id
=
"myDropdown"
class
=
"dropdown-content"
>
<
a
href
=
"#"
>Option 1</
a
>
<
a
href
=
"#"
>Option 2</
a
>
<
a
href
=
"#"
>Option 3</
a
>
</
div
>
</
div
>
<
script
>
function toggleDropdown() {
const dropdownContent =
document.getElementById("myDropdown");
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
}
window.onclick = function (event) {
if (!event.target.matches('.dropbtn')) {
const dropdowns =
document.getElementsByClassName
("dropdown-content");
for (let i = 0; i <
dropdowns.length
; i++) {
const
openDropdown
=
dropdowns
[i];
if (openDropdown.style.display === "block") {
openDropdown.style.display
=
"none"
;
}
}
}
}
</script>
</
body
>
</
html
>