简体   繁体   中英

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.

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.

I want to achieve this using CSS only.

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:

<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.

And some javascript to handle showing and hiding "li" elements

Its not possible as option tag. you can use custom dropdown list. http://tympanus.net/Tutorials/CustomDropDownListStyling/index2.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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