Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to select an open select box in css?

Tags:

I set a background image (arrow down) to a select box after setting the webkit-appearance attribute to none. When the option list is opened I want to display another background image (arrow up). Is there a pseudo class or something for it? I couldn't find anything during my research...

like image 534
user2718671 Avatar asked Sep 21 '15 11:09

user2718671


People also ask

How do I style a dropdown selection in CSS?

There are many ways to design a <select> dropdown menu using CSS. The Dropdown Menu is mainly used to select an element from the list of elements. Each menu option can be defined by an <option> element that can nested inside the <select> element.

How do I change the Select option in CSS?

To change the selected option background-color CSS style, we can set the style attribute of the select and option elements. to set the whole select element to background color 009966 and color FFF . Then we override the the styles in the option elements with style="background: white; color: black;" .

Can you style a select tag?

<select> tags can be styled through CSS just like any other HTML element on an HTML page rendered in a browser. Below is an (overly simple) example that will position a select element on the page and render the text of the options in blue.

How do I create a custom dropdown in CSS?

Example Explained HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.


2 Answers

you can use the :focus pseudo class.

But this will indicate the "open" state, also when the <select> is selected via tab or an item was just selected. To circumvent this, you maybe use something like select:hover:focus but this is rather ugly and is not a solid solution.

select:focus {
  background-color: blue;
  color: white;
}
<select>
  <option>Click me</option>
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>
like image 124
Nico O Avatar answered Oct 09 '22 06:10

Nico O


It's not possible to detect if a HTML select element is opened using CSS, or even javascript.

If you wish to customise the arrow symbol of a custom dropdown, your best option is to use a custom dropdown component which maps to a hidden select element.

like image 43
Curtis Avatar answered Oct 09 '22 07:10

Curtis