How To Create Dropdown Menu In Html
When building out web pages, you have limited real estate to display all your content. To maximize space, you can use dropdown menus. The ecommerce website Designer Junkie Apparel uses a dropdown menu to display all its product categories, for example. That way, visitors can either shop the whole collection or hover over the menu and click one of the options to narrow down to the products they're most interested in. In this post, we'll walk through how to make a dropdown menu using HTML so you can incorporate it into your website designs. A drop-down menu is a list of options that is only revealed when a user interacts with the menu — either by clicking or hovering over it. The options then descend vertically, or "drop down," and disappear again once the user disengages with the menu. Since a dropdown menu allows you to place more content on a page without cluttering it, it's commonly used in website navigation and forms. Let's walk through how to create a basic and hoverable dropdown in HTML below. It's easy to create a basic dropdown menu in HTML with the <select> element. Let's break the process down step by step below. To start, add a <label> element. In the opening tag, add a for attribute with a shorthand name for the dropdown list. For example, if the dropdown contains a list of dog names, then you could set the attribute equal to "dog-nams." Here's what your HTML might look like so far: Next, add a <select> element. In the opening tag, add a name and ID attribute and set both to the same value as the for attribute in the previous step. So, for this example, you'd set the name and ID attributes to "dogs." Here's the HTML: The name attribute is required for any data from the dropdown menu to be submitted. The id attribute, on the other hand, is required to associate the dropdown list with a label. Finally, you'll add option elements between the opening and closing tags of the select element. Add as many options as you want to provide in the dropdown list. Then, add a value attribute within each opening <option> tag and set it to the option name. Here are four examples: Here's the HTML all together and the result: See the Pen HTML-only Dropdown by Christina Perricone (@hubspot) on CodePen. The first option will be listed as the default value of the dropdown menu, as you can see in the example above. However, to change the default value, you can use the "selected" boolean attribute. Simply add it to the opening tag of the option element you want to display as the default, after its value attribute. In the example below, you'll see a dropdown menu for membership plans. While the options include a free and bronze plan, the boolean attribute is used to set the default value to silver. See the Pen HTML-only Dropdown with boolean attribute by Christina Perricone (@hubspot) on CodePen. If you'd like a dropdown menu to only appear when a user hovers over an element, then you'll need to use HTML and CSS. Let's look at the process below. First, you'll need to create a div and add a class attribute. Set this class attribute to "dropdown." Then, in CSS, set this div's display to "inline-block" and position to "relative." This will allow the dropdown content to appear right below the dropdown button. Here's the HTML: Here's the CSS: Next, create an element that will reveal the dropdown list when a user hovers over it. For this example, we'll create a button. Then, place it inside the div. Here's the HTML so far: Now it's time to create the actual dropdown content, which will be hidden until the user hovers over the button. For the example below, we'll include three links in the drop down menu. Each of the links will be wrapped in a div with the classname "dropdown-content." Here's the HTML for the dropdown content: In CSS, set this div's display to "none," its position to "absolute," and its width to 100%. This will ensure the dropdown content appears right below the dropdown button and is the same width as the button. Also, set the overflow property to "auto" to enable scroll on small screens. Finally, the box-shadow property is defined to make the dropdown content stand out against the background. Here's the CSS: To ensure the dropdown content actually shows on hover, you need to specify the div's display property with the pseudo-class ":hover." This defines a special state of the element — in this case, how it appears when a user is hovering over it. Here's the CSS: Without any styling, the links inside the dropdown menu would be bright blue and underlined, with no spacing in between. To make them look better, let's set the font color to black, the padding property to 5px, and the text decoration property to "none." Here's the CSS: You can also style the links to appear differently on hover using the pseudo-class ":hover." Say, you want the text and background color of a link to change when a user hovers over it. Here's the CSS: Here's the code all together and the result: See the Pen Hoverable Dropdown Menu with Links by Christina Perricone (@hubspot) on CodePen. In the examples above, users could only select one option from the dropdown menu. You can create a menu that allows users to select multiple options. This is called a multiselect dropdown. To create a multiselect dropdown, you will need HTML, CSS, and Javascript. Here's an example created by game and app developer Charlie Walter. Notice that he uses a form element. See the Pen Multiselect (dropdown) by Charlie Walter (@cjonasw) on CodePen. Now that you know how to create some different types of dropdown menus, you're ready to move onto the next stage: web accessibility. Dropdown menus must be accessible so that all visitors — including those with disabilities — can browse your site, submit forms, and so on. To learn about the fundamentals of drop-down menu accessibility, check out How to Create Drop-Down and Fly-Out Menus That Are Web-Accessible. HTML Dropdown Menu
How to Make a Dropdown Menu in HTML
Step 1: Create a label element.
<label for="dog-names">Choose a dog name:</label>
Step 2: Create a select element.
<select name="dog-names" id="dog-names"></select>
Step 3: Create option elements and place them inside the select element.
<option value="rigatoni">Rigatoni</option>
<option value="dave">Dave</option>
<option value="pumpernickel">Pumpernickel</option>
<option value="reeses">Reeses</option>
HTML Dropdown Default Value
How to Make a Hoverable Dropdown Menu
Step 1: Create and style a div with a class name "dropdown."
<div class="dropdown">
</div>
.dropdown {
display: inline-block;
position: relative;
}
Step 2: Create the hoverable element.
<div class="dropdown">
<button>HubSpot Resources</button>
</div>
Step 3: Create and style the dropdown content.
<div class="dropdown-content">
<a href="https://blog.hubspot.com/">Blog</a>
<a href="https://academy.hubspot.com/">Academy</a>
<a href="https://www.youtube.com/user/hubspot">YouTube</a>
</div>
.dropdown-content {
display: none;
position: absolute;
width: 100%;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
Step 4: Set the dropdown menu's hover state.
.dropdown:hover .dropdown-content {
display: block;
}
Step 5: Style the links inside the dropdown menu.
.dropdown-content a {
display: block;
color: #000000;
padding: 5px;
text-decoration: none;
}
Step 6: Change the color of dropdown links on hover.
.dropdown-content a:hover {
color: #FFFFFF;
background-color: #00A4BD;
}
Multiselect Dropdown
What's Next in Coding Dropdown Menus
Originally published Jun 8, 2021 7:00:00 AM, updated October 08 2021
How To Create Dropdown Menu In Html
Source: https://blog.hubspot.com/website/html-dropdown
Posted by: kingstonobleas.blogspot.com
0 Response to "How To Create Dropdown Menu In Html"
Post a Comment