skip to Main Content

How can we customize dropdown in html and JavaScript and make similar as I attached screenshot?

enter image description here

In this requirement, we have to change height, width, back ground color and hover color of dropdown option.

Please give suggestion, how we can achieve this.

2

Answers


  1. To customize a dropdown menu in HTML and JavaScript, you can modify its appearance using CSS styles and enhance its functionality using JavaScript event handling. Here’s an example of how you can customize a dropdown:

    HTML structure:

    <div class="dropdown">
      <button class="dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Select an option
      </button>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Option 1</a>
        <a class="dropdown-item" href="#">Option 2</a>
        <a class="dropdown-item" href="#">Option 3</a>
      </div>
    </div>
    

    CSS styles:

    .dropdown {
      position: relative;
      display: inline-block;
    }
    
    .dropdown-toggle {
      background-color: #ffffff;
      padding: 8px 16px;
      border: 1px solid #cccccc;
      border-radius: 4px;
      font-weight: bold;
      cursor: pointer;
    }
    
    .dropdown-toggle:focus {
      outline: none;
    }
    
    .dropdown-menu {
      position: absolute;
      top: 100%;
      left: 0;
      min-width: 120px;
      background-color: #ffffff;
      border: 1px solid #cccccc;
      border-radius: 4px;
      padding: 8px 0;
    }
    
    .dropdown-item {
      display: block;
      padding: 4px 16px;
      text-decoration: none;
      color: #333333;
      cursor: pointer;
      transition: background-color 0.2s;
    }
    
    .dropdown-item:hover {
      background-color: #f6f6f6;
    }
    

    JavaScript event handling (using plain JavaScript):

    var dropdownToggle = document.querySelector('.dropdown-toggle');
    var dropdownMenu = document.querySelector('.dropdown-menu');
    
    dropdownToggle.addEventListener('click', function() {
      dropdownMenu.classList.toggle('show');
    });
    
    document.addEventListener('click', function(e) {
      var target = e.target;
      if (!dropdownToggle.contains(target) && !dropdownMenu.contains(target)) {
        dropdownMenu.classList.remove('show');
      }
    });
    

    In this example, the CSS styles define the appearance of the dropdown and its items. The JavaScript code adds event listeners for opening and closing the dropdown menu upon clicking the dropdown toggle button. Clicking outside the dropdown will also close the menu.

    You can customize the CSS styles to match your desired design and enhance the JavaScript code further to meet your specific requirements. Additionally, you can leverage CSS frameworks like Bootstrap or Tailwind CSS to get more pre-defined dropdown styles and behaviours with minimal customization.

    Login or Signup to reply.
  2. You Can Use CSS Properties in that case:
    like on focus and on hover to design The Elements

    const button = document.querySelector('#btn');
    const ul = document.querySelector("ul");
    
    function myfunc() {
        if (ul.style.display === "none" || window.getComputedStyle(ul).display === "none") {
            ul.style.display = 'flex';
        } else {
            ul.style.display = 'none';
        }
        button.style.backgroundColor = "green"
    }
    
    button.addEventListener('click', myfunc);
    *{
    font-family:monospace;}
    
    .list{
     background-color:#fff;
    }
    ul{
     list-style:none;
     display:none;
     flex-direction:column;
     gap:20px;
     text-align:center;
     height:50px;
     font-size:30px;
    }
    .list li:hover{
     background-color:skyblue;
    }
    button{
    background-color:blue;}
    <div class="list">
     <button id="btn">Click me To open List Item</button>
      <ul>
          <li>Home</li>
          <li>About</li> 
          <li>Contact</li>       
          <li>Community</li> 
      </ul>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search