-2

How do i add images in dropdown menu HTML using PHP ? I have tried creating background url style in option tag but that isn't working, what other ways i can use to add images inside a dropdown menu.

1 Answers1

2

You can easilly do that using HTML5, CSS and Javascript. Here is an example - you can also find in in JSFiddle.

const button = document.querySelector('button');
const list   = document.querySelector('#myDropdown');
let classes = list.classList;

button.addEventListener('click', function() {
  classes.toggle('show');
});
ul, li {
  padding: 0;
  margin: 0;
  list-style: none;
  display:flex;
}

.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.list-items{  
  flex-direction: column;
}

.list-items li {
  align-items: center;
  justify-content: space-between;
  flex: 1 1 auto;
}

.list-items li .img-container {
  height: 64px;
  width: 64px;  
  background-color: #edfaff;
  overflow: hidden;
}

.list-items li .img-container img {
  width: 100%;
  height: 100%
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
<h3>Clickable Dropdown</h3>
<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
  <ul class="list-items">
    <li>
      <a href="#home">Home</a> <span class="img-container"><img src="https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" /></span>
    </li>
    <li>
      <a href="#about">About</a> <span class="img-container"><img src="https://images.unsplash.com/photo-1506929562872-bb421503ef21?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" /></span>
    </li>
    <li>  
      <a href="#contact">Contact</a> <span class="img-container"><img src="https://images.unsplash.com/photo-1495954484750-af469f2f9be5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" /></span>
    </li>
  </ul>  
  </div>
</div>
Steven
  • 19,224
  • 47
  • 152
  • 257