简体   繁体   中英

How can I style the dropdown of input type select?

I want to style only the dropdown menu, is there any option? Also, apply a custom arrow.

<select onChange={displayProjectBy}>
    <option value="MOST_POPULAR">Most Popular</option>
    <option value="NEWEST">Newest</option>
    <option value="OLDEST">Oldest</option>
    <option value="NAME_ASC">Name A-Z</option>
    <option value="NAME_DESC">Name Z-A</option>
</select>

select {    
    outline: 0;
     border-color: $color-gainsboro;
     border-radius: 10px;
     color: $color-black;
     font-weight: 700;
     padding-left: 20px;
     float: right;
     width: 100%;                         
}

Unfortunately you have to make your own dropdown to accomplish that. Here is an example how you can do that ( all credits to https://www.w3schools.com/ ):

<div class="custom-select" style="width:200px;">
 <select>
  <option value="0">Select car:</option>
  <option value="1">Audi</option>
  <option value="2">BMW</option>
  <option value="3">Citroen</option>
  <option value="4">Ford</option>
  <option value="5">Honda</option>
  <option value="6">Jaguar</option>
  <option value="7">Land Rover</option>
  <option value="8">Mercedes</option>
  <option value="9">Mini</option>
  <option value="10">Nissan</option>
  <option value="11">Toyota</option>
  <option value="12">Volvo</option>
 </select>
</div>

CSS:

/* The container must be positioned relative: */
.custom-select {
   position: relative;
   font-family: Arial;
}

.custom-select select {
   display: none; /*hide original SELECT element: */
}

.select-selected {
   background-color: DodgerBlue;
}

/* Style the arrow inside the select element: */
.select-selected:after {
  position: absolute;
  content: "";
  top: 14px;
  right: 10px;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-color: #fff transparent transparent transparent;
}

/* Point the arrow upwards when the select box is open (active): */
.select-selected.select-arrow-active:after {
  border-color: transparent transparent #fff transparent;
  top: 7px;
}

/* style the items (options), including the selected item: */
.select-items div,.select-selected {
  color: #ffffff;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
  cursor: pointer;
}

/* Style items (options): */
.select-items {
  position: absolute;
  background-color: DodgerBlue;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 99;
}

/* Hide the items when the select box is closed: */
.select-hide {
  display: none;
}

.select-items div:hover, .same-as-selected {
  background-color: rgba(0, 0, 0, 0.1);
}

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