gpt4 book ai didi

html - 包含图像或图标的下拉列表

转载 作者:行者123 更新时间:2023-12-05 05:51:55 26 4
gpt4 key购买 nike

我想将特定图标或 png 添加到文本旁边的下拉列表中。 Firefox 不再支持内联背景图像样式来执行此操作。我环顾四周,但所有方法都已过时。

这是带有示例的 codepen 链接 Link

如果需要的话就是这个代码

HTML

<div>

<select>
<option value="Cat">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="cat icon">
Cat</option>
<option value="Dog">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="dog icon">
Dog</option>
<option value="Bird">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="bird icon">
Bird</option>
<option value="Mouse">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="mouse icon">
Mouse</option>
</select>

</div>

CSS

select {
padding: 5px 25px;
font-size: 20px;
border: none;
background:grey;
border-radius: 5px;
color: azure;
}

div{
position: absolute;
top:20%;
width:100%;
left: 50%;
margin: auto 0;
}

最佳答案

我建议您不要使用 <select>作为一个菜单,而是一个 <div><a>标签或 <ul><li>标签,这是一个工作示例:

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">image<br><img src="https://www.w3schools.com/images/lamp.jpg"></a>
<a href="#">font awesome<br><i class="fas fa-phone"></i></a>
</div>
</div>

完整代码如下:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.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;
}

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

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

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">image<br><img src="https://www.w3schools.com/images/lamp.jpg"></a>
<a href="#">font awesome<br><i class="fas fa-phone"></i></a>
</div>
</div>

<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>

</body>
</html>

关于html - 包含图像或图标的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70294838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com