Images in Dropdown List

Putting images with options in a dropdown list

This code will work only in Firefox:

<select>
<option value="volvo" style="background-image:url(images/volvo.png);">Volvo</option>
<option value="saab" style="background-image:url(images/saab.png);">Saab</option>
<option value="honda" style="background-image:url(images/honda.png);">Honda</option>
<option value="audi" style="background-image:url(images/audi.png);">Audi</option>
</select>

Edit (April 2018):

Firefox does not support this anymore.

want to show image/icons in dropdown list

html:

<select name="event_icon" id="event_icon">
<option>Select An Icon</option>
<option value="vaisakhi.jpg" data-style="background-image: url('icons/vaisakhi.jpg');"></option>
<option value="cake.png" data-style="background-image: url('icons/cake.png');"></option>
<option value="game.png" data-style="background-image: url('icons/game.png');"></option>
</select>

script:

<script>
$(function () {
$.widget("custom.iconselectmenu", $.ui.selectmenu, {
_renderItem: function (ul, item) {
var li = $("<li>"),
wrapper = $("<div>", {text: item.label});

if (item.disabled) {
li.addClass("ui-state-disabled");
}

$("<span>", {
style: item.element.attr("data-style"),
"class": "ui-icon " + item.element.attr("data-class")
})
.appendTo(wrapper);

return li.append(wrapper).appendTo(ul);
}
});

$("#event_icon")
.iconselectmenu()
.iconselectmenu("menuWidget")
.addClass("ui-menu-icons avatar");
});
</script>

How to add images in select list?

In Firefox you can just add background image to option:

<select>
<option style="background-image:url(male.png);">male</option>
<option style="background-image:url(female.png);">female</option>
<option style="background-image:url(others.png);">others</option>
</select>

Better yet, you can separate HTML and CSS like that

HTML

<select id="gender">
<option>male</option>
<option>female</option>
<option>others</option>
</select>

CSS

select#gender option[value="male"]   { background-image:url(male.png);   }
select#gender option[value="female"] { background-image:url(female.png); }
select#gender option[value="others"] { background-image:url(others.png); }

In other browsers the only way of doing that would be using some JS widget library, like for example jQuery UI, e.g. using Selectable.

From jQuery UI 1.11, Selectmenu widget is available, which is very close to what you want.

Add background images in dropdown list in option

You may use select2.js.

Do check out the GitHub link

Making an image clickable for a dropdown menu

You mean this?

You really should resize the image to the exact size of your button

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
document.getElementById("drop").addEventListener("click", function(e) {
document.getElementById("myDropdown").classList.toggle("show");
});

// Close the dropdown if the user clicks outside of it
window.addEventListener("click", function(event) {
if (!event.target.matches('.dropbtn')) {
document.querySelectorAll(".dropdown-content.show")
.forEach(openDropdown => openDropdown.classList.remove('show'))
}
});
.dropbtn {
color: white;
padding: 16px;
font-size: 16px;
border: 1px solid black;
cursor: pointer;
background: url(https://boys-cry.com/wp-content/uploads/2022/04/bc_logo_black.png);
background-repeat: no-repeat;
background-size: 100px 50px;
}

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

.dropdown-content {
display: none;
position: absolute;
background-color: #00000000;
min-width: 200px;
z-index: 1;
margin-top: 20px;
}

.dropdown-content a {
color: black;
font-family: helvetica;
padding: 5px 5px;
text-decoration: none;
display: block;
}

hr.solid {
border-left: none;
border-right: none;
border-top: 1px solid #000000;
border-bottom: none;
}

.show {
display: block;
}
<div class="dropdown">
<button id="drop" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<hr class="solid">
<a href="#">WE</a>
<hr class="solid">
<a href="#">CLIENTS</a>
<hr class="solid">
<a href="mailto:info@boys-cry.com">GET IN TOUCH</a>
<hr class="solid">
</div>
</div>

How to add Dropdown list of different images with text

You should use the model in dropdown.

For Example

class Demo extends StatefulWidget {
@override
_DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
List<CountryData> _countries = CountryData.getCounties();
List<DropdownMenuItem<CountryData>> _dropdownMenuItems;
CountryData _selectedCompany;

@override
void initState() {
_dropdownMenuItems = buildDropdownMenuItems(_countries);
_selectedCompany = _dropdownMenuItems[0].value;
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Country"),
SizedBox(
height: 20.0,
),
DropdownButton(
value: _selectedCompany,
items: _dropdownMenuItems,
onChanged: onChangeDropdownItem,
),
SizedBox(
height: 20.0,
),
Text('Selected: ${_selectedCompany.name}'),
],
),
),
),
);
}

onChangeDropdownItem(CountryData selectedCompany) {
setState(() {
_selectedCompany = selectedCompany;
});
}

List<DropdownMenuItem<CountryData>> buildDropdownMenuItems(List counties) {
List<DropdownMenuItem<CountryData>> items = List();
for (CountryData country in counties) {
items.add(
DropdownMenuItem(
value: country,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Image.asset(
country.images,
width: 10,
height: 10,
),
SizedBox(
width: 10,
),
Text(country.name)
],
),
),
),
);
}
return items;
}

@override
void dispose() {
super.dispose();
}
}

class CountryData {
int id;
String name;
String images;

CountryData(this.id, this.name, this.images);

static List<CountryData> getCounties() {
return <CountryData>[
CountryData(1, 'india', 'assets/images/india.png'),
CountryData(2, 'australia', 'assets/images/australia.png'),
CountryData(3, 'chine', 'assets/images/china.png'),
CountryData(4, 'SU', 'assets/images/south-africa.png'),
CountryData(5, 'UK', 'assets/images/united-kingdom.png'),
];
}
}

Output

output



Related Topics



Leave a reply



Submit