How to Create a Drop-Down Menus With HTML and CSS
How to Create a Drop-Down Menus With HTML and CSS
1
Open your HTML text editor. you'll use an easy text editor,
like Notepad or TextEdit, otherwise you can use a more advanced text editor
like Notepad++.
If you're doing decide to use Notepad++, confirm you opt on
HTML from the "H" section of the Language menu at the very best of
the window before you proceed.
2.Enter the document header. this is often the code that
determines the code type used for the remainder of the document:
<!DOCTYPE html>
<html>
<head>
<style>
3
Create the menu itself. Type within the following code to
work out the dimensions and color of the menu , ensuring to exchange
"#" with the amount you would like to use (the larger the amount ,
the larger your menu will be). you'll also replace the "background-color"
and "color" values with any color (or HTML color code) of your
choice:[1]
dropbtn {
background-color:
black;
color: white;
padding: #px;
font-size: #px;
border: none;
}
4.Indicate that you simply want to put your links within the
menu . Since you will be adding links to the menu later, you'll place them
inside the menu by entering the subsequent code:
dropdown {
position: relative;
display:
inline-block;
}
5
Create the drop-down menu's appearance. the subsequent code
will determine the drop-down menu's size, position when other webpage elements
are involved, and color. make certain to exchange the "min-width"
section's "#" together with your preferred number (e.g., 250) and
alter the "background-color" heading to your preferred color or HTML
code:
dropdown-content {
display: none;
position: absolute;
background-color:
lightgrey;
min-width: #px;
z-index: 1;
}
6
Add detail to the drop-down menu's contents. the subsequent
code addresses the drop-down menu's text color and therefore the size of the drop-down
menu's button. make certain to exchange "#" together with your
preferred number of pixels to dictate the dimensions of the button:
dropdown-content a {
color: black;
padding: #px #px;
text-decoration:
none;
display: block;
}
7
Edit the drop-down menu's hover behavior. once you hover
your mouse over the drop-down menu's button, you will need a couple of colors
to vary . the primary "background-color" line refers to the colour
change which will appear once you select an item within the menu , while the
second "background-color" line refers to the colour change of the
drop-down menu's button. Ideally, both of those colors are going to be lighter
than their appearance when not selected:
.dropdown-content a:hover {background-color: white;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: grey;}
8
Close the CSS section. Enter the subsequent code to point
that you're through with the CSS portion of the document:
</style>
</head>
9
Create the drop-down button's name. Enter the subsequent
code, ensuring to exchange "Name" with whatever you would like the
drop-down button's name to be (e.g., Menu):
<button
class="dropbtn">Name</button>
10
Add your drop-down menu's links. Each of the things within
the menu should link to something, be that a page on your website or an
external website. you'll add items to the menu by entering the subsequent code,
ensuring to exchange https://www.website.com with the link's address (keep the
quotation marks) and "Name" with the link's name.
Name
Name
Name
11
Close out your document. Enter the subsequent tags to shut
the document and indicate the top of the drop-down menu's code:
</body>
</html>
12
Review your drop-down box's code. Your code should look
almost like the following:[2]
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color:
black;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color:
lightgrey;
min-width: 200px;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration:
none;
display: block;
}
.dropdown-content a:hover {background-color: white;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: grey;}
</style>
</head>
<button class="dropbtn">Social
Media</button>
Google
Facebook
YouTube
</body>
</html>
No comments: