How to style select dropdown only when value is empty - html

Lets say I have markup like this:
<select>
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
Now, I want the dropdown to display in gray ONLY when the value is empty i.e when it is showing "Select". When the user has selected one of the options, it should be black / default color.
(This is to be visually consistent with textboxes on my page that have gray placeholders).
I want to accomplish something like this:
select[value=""] {
color: gray;
}
But it turns out that the select tag doesn't really have a value attribute.
Any way to accomplish this other than using JavaScript?

Styling options, will only style them as shown in the drop down list, not when the select is not in focus. I wanted the select to be italic when the blank option is shown, it can be achived like this (note the blank option must have value="" set):
/* If the select does not have a valid value, ie the blank option is selected, we make it italic */
select:invalid {
font-style: italic;
}
/* Now the child options inherit the italics style, so we reset that*/
select > option {
font-style: normal;
}
/* Apply italics to the invalid options too when shown in the dropdown */
select option[value=""], select option:not([value]) {
font-style: italic;
}
<select required>
<option value="">-- Please Select --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Or using onchange attribute (works without required attribute):
/* If the select does not have the attribute [data-empty="false"] */
select:not([data-empty="false"]) {
font-style: italic;
}
/* Now the child options inherit the italics style, so we reset that*/
select > option {
font-style: normal;
}
/* Apply italics to the invalid options too when shown in the dropdown */
select option[value=""], select option:not([value]) {
font-style: italic;
}
<select onchange="this.dataset.empty = this.value == ''">
<option value="">-- Please Select --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

You can use pseudo classes :first-child, :nth-child(1), :nth-of-type(1), if your empty option always will be on the first position. As I understand, option with empty value and text "Select" should be first, as default. In this case, we will be aware that.

Sweet and Simple, Just add below CSS will resolve your issue. I hope it'll help you out.
select option[value=""] {
color: red;
}
Verified on Google Chrome: Version 92.0.4515.107 (Official Build)
(64-bit)
Verified on Firefow: Version 90.0.2 (64-bit)
select option[value=""] {
color: red;
}
<select>
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<select>
<option value="">Select</option>
<option value="1">One</option>
<option value="">Two</option>
<option value="3">Three</option>
<option value="">Four</option>
</select>

<select name="backGround" size="1" onChange="updateTextColour(this.value);"><!--changeColor(this.selected);"-->
<option style="color:Gray">Select</option>
<option style="color:Black">Value 1</option>
<option style="color:Black">Value 2</option>
</option>
</select></span></span></td>

Related

How do i get rid of the dropdown effect of the select tag

I'm trying to use css to style a select/option html element so that they display as a small list of button with the selected option being highlighted instead of the traditional dropdown menu.
I would've simply rewritten the entire thing, but i am editing a shopify theme, and there are too many implications of such change, some of them are above my "pay-grade" let's say, so i am trying to use the easy way out of just restyling it using css to get the desired result
I have done this before like this, but it doesn't work on mobile devices such as iOS.
#foo {
overflow: auto;
height: 2.4em;
border: 0;
}
#foo option {
display: inline-block;
margin: 0 .3em;
}
#foo option:checked {
font-weight: bold;
}
<select multiple="multiple" name="foo[]" id="foo" size="5">
<option value="1" selected="selected">Foo 1</option>
<option value="2">Foo 2</option>
<option value="3">Foo 3</option>
<option value="4">Foo 4</option>
<option value="5">Foo 5</option>
<option value="6">Foo 6</option>
<option value="7" selected="selected">Foo 7</option>
</select>
Sounds like <select multiple> might be closer to what you're looking for. Learn more here: https://www.w3schools.com/tags/att_select_multiple.asp

Change select fake placeholder color when option is selected - Pure CSS

I've got a simple select element with a fake/placeholder. I want to have the placeholder in a grey color. Once a option is selected I want to change the color to black.
I do know that I can archive this by adding the required state to my select element and in my CSS I add:
But my question is can I archive this without having to require the field?
If possible I would prefer a pure CSS solution.
select {
color: black;
}
select:required:invalid {
color: grey;
}
<select id="select" required>
<option disabled selected hidden value="">Please select value</option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>

select option text color displayed on dropdown list but not colored in box, this option works in ie 9 but not works in chrome

https://jsfiddle.net/pbvijayk/7bLdg7cg/5/
option.red
{
color: #cc0000;
}
<select name="color">
<option class="red">Red</option>
<option >White</option>
<option >Blue</option>
<option >Green</option>
</select>
please run the code in ie9 and chrome.
color of the text displayed in ie9 correctly color will appear in both dropdown and dropdown box.
but in chrome text color displayed in dropdown list but not displayed in box.
Please try this one :
https://jsfiddle.net/7bLdg7cg/7/
.greenText{ color:green; }
.blueText{ color:blue; }
.redText{ color:red; }
<select
onchange="this.className=this.options[this.selectedIndex].className"
class="greenText">
<option class="greenText" value="apple" >Apple</option>
<option class="redText" value="banana" >Banana</option>
<option class="blueText" value="grape" >Grape</option>
</select>
Try giving the style to select not the option like this:
<select name="color">
<option class="red">Red</option>
<option>White</option>
<option >Blue</option>
<option >Green</option>
</select>
<style>
select .red
{
color: #cc0000;
}
</style>

Select size attribute size not working in Chrome/Safari?

How to solve problem that select attribute size, like in code:
<select size="2">
<option value="0" selected="selected">Default</option>
<option value="1">select 1</option>
<option value="2">select 2</option>
<option value="3">select 3</option>
</select>
How can I get it working in Chrome and Safari?
This is a known bug in webkit browsers.
The simplest way to get it to work just how you like it in Chrome and Safari would be to manually specify the styling the select itself.
Here is a working jsFiddle.
select {
height: 36px;
font-size: 14px;
}
<select size="2">
<option value="0" selected="selected">Default</option>
<option value="1">select 1</option>
<option value="2">select 2</option>
<option value="3">select 3</option>
</select>
Include height: auto and it should work.
<select size="2" style="height: auto">
<option value="0" selected="selected">Default</option>
<option value="1">select 1</option>
<option value="2">select 2</option>
<option value="3">select 3</option>
</select>
I use the size attribute in the HTML for "other" browsers and then specify webkit appearance in CSS. Other types of menu list options are available, textfield is not the only one. And you may need to mess with height based on font size. This makes it look about the same on all browsers.
CSS
select{
-webkit-appearance: menulist-textfield;
height: 45px;
}
I was able to fix this problem by adding the "multiple" option to the select tag.
Try using the css property width of the select tag :
select {
width:value px;
}

Styling disabled <select> (dropdown boxes) in HTML

One of our customers has a hard time reading the grey text in disabled controls in our web-based application:
We would like to change the style to a light grey background and a black text. Unfortunately, most browsers (including IE, which is what the customer is using) ignore the color: ... CSS attribute on disabled controls, so we cannot change the foreground color.
For text boxes (input type="text"), this can easily be workarounded by using the readonly instead of the disabled attribute. Unfortunately, this is not an option for dropdowns (select) or checkboxes (input type="checkbox").
Is there an easy workaround for that? Preferebly one where the control does not need to be replaced by another type of control? (...since our controls are rendered by ASP.NET)
PS: Using the [disabled] selector in CSS does not make a difference.
In Internet Explorer 9, support will be added for the :disabled pseudo-selector (ref). I don't know whether that will honor the "color" property, but it seems likely.
In older versions of IE, you can adjust the background color (but not the color). Thus:
<style type="text/css">
select[disabled] { background-color: blue; }
</style>
That works in IE 7 and IE 8. You still can't alter the foreground color, but you can change the background color to contrast more strongly with the gray that IE assigns it when it's disabled.
This worked for me in webkit and Firefox
select:disabled{
opacity: 0.6;
}
For those still finding this.
Not working:
select[disabled] { background-color: blue; }
Working:
select option [disabled] { background-color: blue; } will do
This worked for me
select[disabled='disabled']::-ms-value {
color: red;
}
Sorry for my english...
That's not possible using css just, IE doesn't allow change properties of a disabled select tag
You can try the following:
<style>
/*css style for IE*/
select[disabled='disabled']::-ms-value {
color: #555;
}
/*Specific to chrome and firefox*/
select[disabled='disabled'] {
color: #555;
}
</style>
I know this is question is old but this code worked well for me.
It allowed for full control of text and background color. I used this code with a disabled select control whose value is set based on a value from another select. I didn't want to see the grayed background, especially when the value had not yet been set.
CSS
<style>
.whatever-control:disabled, .whatever-control[readonly] {
background-color: white;
opacity: 1;
color: blue;
font-weight: bold;
}
</style>
Form
<select id = "SelectState" name="SelectState" class="whatever-control" disabled="disabled">
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DC">District of Columbia</option>
<option value="DE">Delaware</option>
<option value="FL" selected="selected">Florida</option>
<option value="GA">Georgia</option>
</select>
You can try,
option:disabled{
opacity: 0.6;background-color: #ff888f;
}
<select id="HouseCleaningEmp" onChange="myCalculater()">
<option value="1">option 1 </option>
<option value="2">option 2 </option>
<option value="3">option 3 </option>
<option value="4">option 4 </option>
<option value="5" disabled>option 5 </option>
<option value="6" disabled>option 6 </option>
<option value="7">option 7 </option>
<option value="8">option 8 </option>
</select>