Pure CSS solution to styling specific <select> options in webkit based browsers?

后端 未结 3 1984
小鲜肉
小鲜肉 2021-01-20 03:34

Quite simply, is there any way to style specific select options in Chrome/Safari?

For example, if I had:


                        
    
提交评论

  • 2021-01-20 04:00

    I recently came across this technique to custom style a select tag with only CSS.

    HTML:

    <div class="styled-select">
      <select class="the-select">
          <optgroup label="TECHNICIANS">
            <option>Joe Smith</option>
            <option>Joe White</option>
          </optgroup>
          <optgroup label="PRODUCERS">
            <option>Jane Black</option>
            <option>Cindy Gray</option>
          </optgroup>
      </select>
    </div>
    

    CSS:

    .styled-select {
    width: 342px;
        height: 30px;
        overflow: hidden;
        background: url("/img/selectarrow.png") no-repeat right;
        border: none;
        opacity: 0.8;
        background-color: #999999;
     }
    
    .styled-select select {
      background: transparent;
      width: 342px;
      padding-bottom: 10px;
      padding-left: 10px;
      padding-top: 5px;
      font-size: 16px;
      border: 0;
      border-radius: 0;
      height: 34px;
      -webkit-appearance: none;
      font-weight: 200;
     font-family: "lato", sans-serif;
     font-size: 18px;
     }
    
    
     .styled-select select:focus {
    outline: none;
     }
    

    Here's a fiddle: http://jsfiddle.net/eshellborn/AyDms/

    And then just make sure you get a picture called 'selectarrow' for the drop-down image.

    0 讨论(0)
  • 2021-01-20 04:17

    If you just want them to clearly be headers, use a tag intended for this: <optgroup>. This might also help you with applying CSS.

    <select class="the-select">
      <optgroup label="TECHNICIANS">
        <option>Joe Smith</option>
        <option>Joe White</option>
      </optgroup>
      <optgroup label="PRODUCERS">
        <option>Jane Black</option>
        <option>Cindy Gray</option>
      </optgroup>
    </select>
    
    0 讨论(0)
  • 提交回复
    热议问题