Style ≪Select≫ Element Based on Selected ≪Option≫

Style select element based on selected option

Unfortunately, yes - this is something not currently possible with only CSS. As mentioned in the answers and comments to this question, there is currently no way to make the parent element receive styling based on its children.

In order to do what you're wanting, you would essentially have to detect which of the children (<option>) is selected, and then style the parent accordingly.

You could, however, accomplish this with a very simple jQuery call, as follows:

HTML

<select>
<option value="foo">Foo!</option>
<option value="bar">Bar!</option>
</select>

jQuery

var $select = $('select');
$select.each(function() {
$(this).addClass($(this).children(':selected').val());
}).on('change', function(ev) {
$(this).attr('class', '').addClass($(this).children(':selected').val());
});

CSS

select, option { background: #fff; }
select.foo, option[value="foo"] { background: red; }
select.bar, option[value="bar"] { background: green; }

Here is a working jsFiddle.

Back to the question about the future of selectors. Yes - the "Subject" selectors are intended to do exactly what you mention. If/when they ever actually go live in modern browsers, you could adapt the above code to:

select { background: #fff; }
!select > option[value="foo"]:checked { background: red; }
!select > option[value="bar"]:checked { background: green; }

As a side-note, there is still debate about whether the ! should go before or after the subject. This is based on the programming standard of !something meaning "not something". As a result, the subject-based CSS might actually wind up looking like this instead:

select { background: #fff; }
select! > option[value="foo"]:checked { background: red; }
select! > option[value="bar"]:checked { background: green; }

Style Select Element with same style as the selected option

There's no way you can accomplish this without JavaScript:

$('select.theme').change(function () {
$(this).css('color', $(this).find('option:selected').css('color'));
}).change();

Here's your fiddle: http://jsfiddle.net/uCmSR/2/

How to style the option of an html select element?

There are only a few style attributes that can be applied to an <option> element.

This is because this type of element is an example of a "replaced element". They are OS-dependent and are not part of the HTML/browser. It cannot be styled via CSS.

There are replacement plug-ins/libraries that look like a <select> but are actually composed of regular HTML elements that CAN be styled.

CSS :selected pseudo class similar to :checked, but for option elements

the :checked pseudo-class initially applies to such elements that have the HTML4 selected and checked attributes

Source: w3.org


So, this CSS works, although styling the color is not possible in every browser:

option:checked { color: red; }

An example of this in action, hiding the currently selected item from the drop down list.

option:checked { display:none; }
<select>    <option>A</option>    <option>B</option>    <option>C</option></select>

How to change the style of a select after an option has been selected?

Try

select[data-chosen]

select[data-chosen] {outline-offset:2px;outline:12px solid;}
select[data-chosen='opt1'] {outline:none;}
select[data-chosen='opt2'] {outline-color:green;}
select[data-chosen='opt3'] {outline-color:red;}
select[data-chosen='opt4'] {outline-color:steelblue;}
select[data-chosen='opt5'] {outline-color:orange;}
<select onchange=" this.dataset.chosen = this.value; ">
<option value="opt1"> I have no outline. </option>
<option value="opt2"> Make it green! </option>
<option value="opt3"> Why not red instead! </option>
<option value="opt4"> No, steelblue! It's so cool! </option>
<option value="opt5"> Okay, orange. It's final. </option>
</select>

Detecting if an option has been selected in select using CSS

Yes, the :checked pseudo-class also targets <option> tags.

Example:

option:checked { display:none; }

Source:

http://www.w3.org/TR/selectors/#checked

EDIT:

If you want to target an element outside of your <select>, it can be done in a hacky way by manipulating the :valid css pseudo-class. Note that the required attribute must be enabled for the <select> tag to register as valid/invalid.

Example:

body {  background-color: #fff;}
select:valid ~ label { background-color: red;}
<select required>  <option value="" selected>Please select an option</option>  <option>1</option>  <option>2</option></select><label>Please select an option</label>

Changing the color of a select depending on its value - CSS only(?)

You can use required and assign empty value to the first option:

<select class="mySelect" required>
<option value="">0</option>
<option value="1">1</option>
</select>

css:

.mySelect { font-size: 2em; }
.mySelect option { color: green; }
.mySelect option[value=""] { color: red; }
.mySelect:invalid { color: red; }

Check fiddle



Related Topics



Leave a reply



Submit