How to style the option of an html “select” element?

后端 未结 18 2481
迷失自我
迷失自我 2020-11-21 05:09

Here\'s my HTML:


                        
    
提交评论

  • 2020-11-21 05:37

    You can style the option elements to some extent.

    Using the * CSS tag you can style the options inside the box that is drawn by the system.

    Example:

    #ddlProducts *
    {
     border-radius:15px;
     background-color:red;
    }
    

    That will look like this:

    enter image description here

    0 讨论(0)
  • 2020-11-21 05:37

    Some properties can be styled for<option> tag:

    • font-family
    • color
    • font-*
    • background-color

    Also you can use custom font for individual <option> tag, for example any google font, Material Icons or other icon fonts from icomoon or alike. (That may come handy for font selectors etc.)

    Considering that, you can create font-family stack and insert icons in <option> tags, eg.

        <select>
            <option style="font-family: 'Icons', 'Roboto', sans-serif;">a    ★★★</option>
            <option style="font-family: 'Icons', 'Roboto', sans-serif;">b    ★★★★</option>
        </select>
    

    where is taken from Icons and the rest is from Roboto.

    Note though that custom fonts do not work for mobile select.

    0 讨论(0)
  • 2020-11-21 05:37

    Leaving here a quick alternative, using class toggle on a table. The behavior is very similar than a select, but can be styled with transition, filters and colors, each children individually.

    function toggleSelect(){ 
     if (store.classList[0] === "hidden"){
        store.classList = "viewfull"
      }
      else {
        store.classList = "hidden"
      }
    }
    #store {
      overflow-y: scroll;
      max-height: 110px;
      max-width: 50%
     }
     
    .hidden {
      display: none
     }
     
    .viewfull {
      display: block
    }
    
    #store :nth-child(4) {
      background-color: lime;
    }
    
    span {font-size:2rem;cursor:pointer}
    <span onclick="toggleSelect()">⮋</span>
     <div id="store" class="hidden">
     
    <ul><li><a href="#keylogger">keylogger</a></li><li><a href="#1526269343113">1526269343113</a></li><li><a href="#slow">slow</a></li><li><a href="#slow2">slow2</a></li><li><a href="#Benchmark">Benchmark</a></li><li><a href="#modal">modal</a></li><li><a href="#buma">buma</a></li><li><a href="#1526099371108">1526099371108</a></li><a href="#1526099371108o">1526099371108o</a></li><li><a href="#pwnClrB">pwnClrB</a></li><li><a href="#stars%20u">stars%20u</a></li><li><a href="#pwnClrC">pwnClrC</a></li><li><a href="#stars ">stars </a></li><li><a href="#wello">wello</a></li><li><a href="#equalizer">equalizer</a></li><li><a href="#pwnClrA">pwnClrA</a></li></ul>
     
     </div>

    0 讨论(0)
  • 2020-11-21 05:37

    This element is rendered by the OS, not HTML. It cannot be styled via CSS.

    	$(function() {
        var clicky;
        var t=0;
        $(document).mousedown(function(e) {
            clicky = $(e.target);
        });
        $(document).mouseup(function(e) {
            clicky = null;
        });
    
        $("select").focusout(function(e) {
            if (typeof clicky.attr('id') !== typeof undefined && clicky.attr('id') !== false) {
            	$(this).parents().children("span.selected").html(clicky.html());
            	$(this).children('option[value="'+clicky.attr('id')+'"]').prop('selected', true);
    		}
            
            $(this).parents().children("span.lists").html('');
            
    
        });
        $('select > option').text(function(i, text) {
    				var attr = $(this).attr('selected');
    				if (typeof attr !== typeof undefined && attr !== false) {
            				$(this).parents().parents().children("span.selected").html(text);
    				}
    	});
    
    		$("select").focusin(function(){
    			$(this).children('option').text(function(i, text) {
        			$(this).parents().children("span.lists").append("<span class='item' id='"+$(this).attr('value')+"'>"+text+"</span>");
    				});
    		});
    
    	});
    select {
      width: 0px;
      height: 0px;
      overflow:hidden;
      outline: none;
      border: none;
      appearance:none;
      -moz-appearance: none;
    
    }
    label{
    	display: inline-block;
    	padding: 5px 10px;
    	position: relative;
    	width: 100px;
    	height: 20px;
    	background-color:#ccc;
    
    }
    label .selected{
    	display: inline-block;
    	overflow: hidden;
    	width: 100%;
    	height: 100%;
    }
    label span.lists{
    	width: 100%;
    	display: inline-block;
    	position: absolute;
    	top: 100%;
    	left: 0px;
    	box-shadow: 0px 0px 2px 0px #ccc;
    	background-color:#fff;
    	z-index: 9;
    }
    label span.item{
    	display: inline-block;
    	width: 100%;
    	border-bottom: 1px solid #ccc;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    	<form action="?" method="GET">
    	<label><span class="selected">select..</span> <span class="lists"></span>
    	<select name="test">
    		<option value="1">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</option>
    		<option value="2" selected>item 2</option>
    		<option value="3">item 3</option>
    		<option value="4">item 4</option>
    	</select>
    	</label><br>
    	<label><span class="selected">select..</span> <span class="lists"></span>
    	<select name="test2">
    		<option value="1">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</option>
    		<option value="2">item 2</option>
    		<option value="3" selected>item 3</option>
    		<option value="4">item 4</option>
    	</select>
    	</label><br>
    	<button>Submit</button>
    </form>
    
    </body>
    </html>

    try this it may help you

    [ https://codepen.io/venu9l/pen/jeNXzY][1]
    
    0 讨论(0)
  • 2020-11-21 05:39

    You can add a class and gives font-weight:700; in option. But by using this all the text will become bold.

    0 讨论(0)
  • 提交回复
    热议问题