articles

Home / DeveloperSection / Articles / Creating responsive drop-down in HTML

Creating responsive drop-down in HTML

Creating responsive drop-down in HTML

Ashutosh Kumar Verma 145 10-Jun-2024

Responsive Drop-Down in HTML

When you create a responsive dropdown menu in HTML, it often uses a combination of HTML, CSS, and sometimes JavaScript

HTML

Basics nav-bar syntax with drop-down

<div class="navbar">
 <a href="#">Home</a>
 <a href="#">About</a>
 <div class="dropdown">
   <button class="dropbtn">Services</button>
   <div class="dropdown-content">
     <a href="#">Web Design</a>
     <a href="#">Graphic Design</a>
     <a href="#">SEO</a>
   </div>
 </div>
 <a href="#">Contact</a>
</div>

Example- 

Here is the complete example of how to apply the CSS properties for creating the responsive drop-down using HTML 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" />
<title>Responsive Dropdown Menu</title>
<style>
  /* Basic styling */
  body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
  }

  /* Navbar styling */
  .navbar {
    background-color: #333;
    overflow: hidden;
    display: flex;
    justify-content: end;
    flex-wrap: wrap;
  }

  /* Navbar links */
  .navbar a {
    float: left;
    display: block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
  }

  /* Dropdown container */
  .dropdown {
    float: left;
    overflow: hidden;
  }

  /* Dropdown button */
  .dropdown .dropbtn {
    font-size: 16px;
    border: none;
    outline: none;
    color: white;
    padding: 14px 20px;
    background-color: inherit;
    margin: 0;
  }

  /* Dropdown content */
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #333;
    min-width: 160px;
    z-index: 1;
  }

  /* Dropdown links */
  .dropdown-content a {
    float: none;
    color: white;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
  }

  /* Show dropdown content on hover */
  .dropdown:hover .dropdown-content {
    display: block;
  }
</style>
</head>
<body>

<div class="navbar">
  <a href="#">Home</a>
  <a href="#">About</a>
  <div class="dropdown">
    <button class="dropbtn">Services <i class="fa-solid fa-angle-down"></i></button>
    <div class="dropdown-content">
      <a href="#">Web Design</a>
      <a href="#">Graphic Design</a> <a href="#">SEO</a> <a href="#">Digital Marketing</a> </div> </div> <a href="#">Contact</a> </div> </body> </html>

In the above Example- 
The navbar class is created using the <div> element of a class navbar.
Each menu item is a hyperlink (<a> tag) in the navbar.

The dropdown menu is created using the class dropdown content using a combination of button (<button> tag) and nested <div>.

CSS is used to style the navbar, links, dropdown button, and dropdown content.

The footer is hidden by default (display: none) and is displayed by hovering over the dropdown button (:hover).

Output- 

Creating responsive drop-down in HTML

Also, Read: How to create a scroll bar using CSS


Updated 10-Jun-2024
Hi! This is Ashutosh Kumar Verma. I am a software developer at MindStick Software Pvt Ltd since 2021. I have added some new and interesting features to the MindStick website like a story section, audio section, and merge profile feature on MindStick subdomains, etc. I love coding and I have good knowledge of SQL Database.

Leave Comment

Comments

Liked By