简体   繁体   English

如何使用 css 在选择选项标签中放置背景图像

[英]How to put background image in select option tag using css

I have a country dropdown list receiving from json response, and want to add respective country flag icons (getting the images as well from json reponse) next to the country names.我有一个从 json 响应接收的国家/地区下拉列表,并希望在国家/地区名称旁边添加相应的国家/地区标志图标(也从 json 响应中获取图像)。

I have tried different jQuery and bootstrap plugins for that but what they are doing is ruining my previous select box styling and are adding their own ones dynamically.我为此尝试了不同的 jQuery 和 bootstrap 插件,但他们正在做的是破坏我以前的选择框样式并动态添加自己的样式。

I want to achieve this using CSS only.我只想使用 CSS 来实现这一点。

Code tried so far :到目前为止尝试过的代码:

<select class="form-control icon-menu" name="country" id="country_id" onchange="fetchCountryCode(this.value);">
   <?php
       foreach ($countries as $countriesList) {
         echo '<option value=' . $countriesList['id'] .' style=" background-image : url('. $countriesList['flag_url'] .')" data-imagecss="flag ad">' . $countriesList['name'] . '</option>';
        }
    ?>
</select>

CSS: CSS:

<style>
select.icon-menu option {
    background-repeat:no-repeat;
    background-position:bottom left;
    padding-left:30px;
}
</style>

But not getting any results, i am stucked pls help但没有得到任何结果,我被困住了请帮忙

You can't give an option tag a background-image.您不能为选项标签提供背景图像。

One solution will be to create your own "select" component.一种解决方案是创建您自己的“选择”组件。

<ul>
     <li>option1</li>
     <li>option2></li>
     ..
</ul>

then you give some css to look like a select input.然后你给一些 css 看起来像一个选择输入。

And some javascript to handle showing and hiding "li" elements还有一些 javascript 来处理显示和隐藏“li”元素

Its not possible as option tag.它不可能作为选项标签。 you can use custom dropdown list.您可以使用自定义下拉列表。 http://tympanus.net/Tutorials/CustomDropDownListStyling/index2.html http://tympanus.net/Tutorials/CustomDropDownListStyling/index2.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM