How to change the color of a select when selecting an option

Asked

Viewed 9,132 times

5

I have a select and I’m trying to change the color of it by selecting an option (for example I’m changing the color to red). Using some attributes like hover, focus, active and checked i can even do what I want. The problem is that when select loses Focus it returns its original color.

I want the select to remain gray until it is selected and after the selected color red be applied. Even if he loses focus after.

select {
  color: gray;
}

select:hover,
select:focus,
select:active,
select:checked {
  color: red;
}

select option {
  color: #333;
}

option[value=""][disabled] {
  display: none;
}
<select id="estado" name="estado">
			<option value="" disabled selected>Selecione</option>
            <option value="AC">AC</option>
            <option value="AL">AL</option>
            <option value="AM">AM</option>
            <option value="AP">AP</option>
            <option value="BA">BA</option>
            <option value="CE">CE</option>
            <option value="DF">DF</option>
            <option value="ES">ES</option>
            <option value="GO">GO</option>
            <option value="MA">MA</option>
            <option value="MG">MG</option>
            <option value="MS">MS</option>
            <option value="MT">MT</option>
            <option value="PA">PA</option>
            <option value="PB">PB</option>
            <option value="PE">PE</option>
            <option value="PI">PI</option>
            <option value="PR">PR</option>
            <option value="RJ">RJ</option>
            <option value="RN">RN</option>
            <option value="RO">RO</option>
            <option value="RR">RR</option>
            <option value="RS">RS</option>
            <option value="SP">SP</option>
            <option value="SC">SC</option>
            <option value="SE">SE</option>
            <option value="TO">TO</option>
</select>

  • I’ve never seen a method for css not dude. Ever thought of using js?

  • Hi @Leonardorodrigues! com js would be easy! but here we only use to make the page dynamic or for more complex animations! all the rest of the styling we do using only css and Sass.

4 answers

5

A very simple jQuery script solves!

What you need to do is change the css when the element is selected.

$('#estado').change(function(){
    $(this).css('color', 'red');
});

See working on jsfiddle

  • Thanks for the help @Amanda Lima! But here we only work with css and Sass. We use javascript and jQuery only for more complex animations. Anyway worth! ;)

5


When the :has supported in browsers could use, I believe it would work:

select:has(option[value=""]) {
  color: red;
}

However, so far no browser supports and maybe it won’t even come off the paper.


One option would be to create a long transaction/animation, thus preventing the color to return to normal state.

/!\ This is a gambit!

Thus the select it will take 2147483647 seconds to return to normal, which is gray, this works using the transition: color 0s 2147483647s;.

Meanwhile when there’s the hover, for example, the color would be changed immediately, thanks to the transition:0s;.

So this answers the question of "I want the select to remain red after selecting an option. Even if it loses focus afterwards.", but using Jquery, as already answered, would be ideal.

select {
  color: gray;
  transition: color 0s 2147483647s;
}

select:hover,
select:focus,
select:active,
select:focus {
  color: red;
  transition:0s;
}

select option {
  color: #333;
}

option[value=""][disabled] {
  display: none;
}
<select id="estado" name="estado">
   <option value="" disabled selected>Selecione</option>
   <option value="A">A</option>
   <option value="B">B</option>
</select>

  • +1 just for the warning that is a gambiarra

  • Poxa @Inkeliz! Sometimes and only sometimes we need to resort to ganbiarras to solve one problem or another! kkk! But your solution served me very well! the important thing is q will work as expected! thanks! :)

2

        <select id="estado" class="color_gray" name="estado">
        <option value="" disabled selected>Selecione</option>
        <option value="AC">AC</option>
        <option value="AL">AL</option>
        <option value="AM">AM</option>
        <option value="AP">AP</option>
        <option value="BA">BA</option>
        <option value="CE">CE</option>
        <option value="DF">DF</option>
        <option value="ES">ES</option>
        <option value="GO">GO</option>
        <option value="MA">MA</option>
        <option value="MG">MG</option>
        <option value="MS">MS</option>
        <option value="MT">MT</option>
        <option value="PA">PA</option>
        <option value="PB">PB</option>
        <option value="PE">PE</option>
        <option value="PI">PI</option>
        <option value="PR">PR</option>
        <option value="RJ">RJ</option>
        <option value="RN">RN</option>
        <option value="RO">RO</option>
        <option value="RR">RR</option>
        <option value="RS">RS</option>
        <option value="SP">SP</option>
        <option value="SC">SC</option>
        <option value="SE">SE</option>
        <option value="TO">TO</option>
        </select>

css . color_red { color: red; } . color_gray { color: Gray; }

Js

       $( "select" )
        .change(function () {

            var $SL = $('#estado');
            $SL.addClass('color_red');
            $SL.removeClass('color_gray');

           });

1

Just change the color of select in:

select {
    color: gray;
}

FOR

select {
    color: red;
}
  • I think he wants the color to stay gray until the user modifies, taking away the default option.

  • @Inkeliz "I want select to remain red after selecting an option. Even if it loses focus afterwards."

  • This way it presented it will have red color even before losing focus, not only during and after.

  • that’s right @Inkeliz! select starts with the color Gray and only changes to red after a selection! the solution @Inkeliz served me very well! :

Browser other questions tagged

You are not signed in. Login or sign up in order to post.