I have a <select>:
select:invalid {
color: red;
}
<select required>
<option disabled selected>- please choose -</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
Unfortunately the disabled select is not marked red.
Any idea?
It's not the option I like to style, I like to style the select!
You need to specify value="" for the first option. Without value="", the value of an option is implicitly equal to its text content, meaning it fulfils the required constraint.
Demo:
select:invalid { color: red; }
<select required>
<option disabled value="" selected>- please choose -</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
The above will make the select + all options red until a value has been chosen.
If you want the options to always be black, just extend the CSS a little:
select:invalid { color: red; }
select option { color: black; }
<select required>
<option disabled value="" selected>- please choose -</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
Try to use option:disabled.
select option:disabled {
color: red;
}
<select required>
<option disabled selected>- please choose -</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
.choose_elements{
background-color : red;
}
.elements{
background-color : white;
}
<select required
onchange="this.className=this.options[this.selectedIndex].className"
class="choose_elements">
<option value="0" class="choose_elements">- please choose -</option>
<option value="1" class = "elements">A</option>
<option value="2" class = "elements">B</option>
</select>
Related
I am using select 2 https://select2.org/ and I am wondering how do I change the colour of selected option with the value="4". So when Mark as destroyed is selected it shows in a different colour. I tried setting class and inline css directly but both did not work.
Not working: <option value="4" class="tx-orange">Mark as destroyed</option>
Not working: <option value="4" style="color:orange;">Mark as destroyed</option>
This is how the select block looks like
<select id="scanActionOptions" class="form-control select2">
<option value="0">Verify</option>
<option value="1">Mark as active</option>
<option value="2">Mark as dispense</option>
<option value="3">Mark as stolen</option>
<option value="4">Mark as destroyed</option>
<option value="5">Mark as sample</option>
<option value="6">Mark as free sample</option>
<option value="7">Mark as locked</option>
<option value="8">Mark as exported</option>
<option value="9">Mark as checkout</option>
<option value="10">Mark as expired</option>
<option value="11">Mark as recalled</option>
<option value="12">Mark as withdrawn</option>
</select>
$(document).ready(function() {
$('.select2').select2();
});
/*selected css*/
.select2-container--default .select2-results__option--selected {
color: orange;
background:rgb(221 221 221 / 32%) !important;
}
/*hover css*/
.select2-container--default .select2-results__option--highlighted.select2-results__option--selectable {
background-color: orange !important;
color: white;
}
/*particular option color*/
.select2-container--default .select2-results>.select2-results__options li:nth-child(2) {
color: #fff;
background: red !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-beta.1/dist/js/select2.min.js"></script>
<select id="scanActionOptions" class="form-control select2">
<option value="0">Verify</option>
<option value="1">Mark as active</option>
<option value="2">Mark as dispense</option>
<option value="3">Mark as stolen</option>
<option value="4">Mark as destroyed</option>
<option value="5">Mark as sample</option>
<option value="6">Mark as free sample</option>
<option value="7">Mark as locked</option>
<option value="8">Mark as exported</option>
<option value="9">Mark as checkout</option>
<option value="10">Mark as expired</option>
<option value="11">Mark as recalled</option>
<option value="12">Mark as withdrawn</option>
</select>
If you need to change selected colour apply css on ".select2-container--default .select2-results__option--selected" and change hover css then apply on ".select2-container--default .select2-results__option--highlighted.select2-results__option--selectable".
If you need colour particular option like 2nd option then you can use ".select2-container--default .select2-results>.select2-results__options li:nth-child(2)" .
Select2 CSS seems getting the style from this rule-set
.select2-container--default .select2-results__option--highlighted.select2-results__option--selectable {
background-color: #5897fb;
color: white;
}
You might use li:nth-child(x) applied to the rule-set shown before. CSS aplied to the original select won't be visible.
I am trying to change the color of the first option (Business Type) in a select tag to gray so it looks like placeholder text for a text field. I tried several things that did not work. Is there any way this could be done simply with HTML and CSS?
<select id="businesstype">
<option value="" disabled selected hidden>Business Type</option>
<option value="">Restaurant</option>
<option value="">Hotel</option>
<option value="">Café</option>
<option value="">Shop</option>
<option value="">Services Agency</option>
<option value="">NGO</option>
<option value="">Institution</option>
</select>
This should work:
#businesstype div[disabled] {
opacity: 0.5;
}
Also check out: https://www.w3schools.com/css/css_attribute_selectors.asp
Add required in select tag and do like
<select class="form-control form-item__element--select" required>
<option disabled value="">Business Type*</option>
<option value="">Restaurant</option>
<option value="">Hotel</option>
<option value="">Café</option>
In CSS
.form-item__element--select [disabled] {
color: #DEDEDE;
font-weight: 600;
font-size: 16px;
}
.form-item__element--select:invalid {
color: #DEDEDE;
font-weight: 600;
font-size: 16px;
}
Target select to change the selected color style.
Then, target all options from this select to reset their inherited color.
#businesstype {
color: gray;
}
#businesstype option {
color: black;
}
<select id="businesstype">
<option value="" disabled selected hidden>Business Type</option>
<option value="">Restaurant</option>
<option value="">Hotel</option>
<option value="">Café</option>
<option value="">Shop</option>
<option value="">Services Agency</option>
<option value="">NGO</option>
<option value="">Institution</option>
</select>
You can do it by just adding following css,
#businesstype option[disabled]:first-child {
color: gray;
}
I would like to hide a select form option using CSS but I can't figure out how to do this.
I have a form like this and I need to remove the "before" option.
I expect that option[value=before]{display:none!important} will hide it but it appears it does not work.
option[value=before] {
display: none!important
}
<select class="_select">
<option value="">Any</option>
<option value="1d">Today</option>
<option value="7d">This Week</option>
<option value="31d">This month</option>
<option selected="" value="before">Before date</option>
</select>
Use "" :[value="before"]
If you want to hide it from select remove selected="" from <option selected="" value="before">Before date</option>
option[value="before"]{
display:none;
}
<select class="_select">
<option value="">Indifférente</option>
<option value="1d">Today</option>
<option value="7d">This Week</option>
<option value="31d">This month</option>
<option value="before">Before date</option>
</select>
The problem is I can't change the height of the drop down box in html.for (select->option) I try to apply the style in CSS. It does not works
You can not control the style of option. (see here:https://css-tricks.com/dropdown-default-styling/)
however you can change the font-size and the option will grow
select option{
font-size: 20px;
}
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It is not possible to change height of 'option'
Only you can change the background-color and color of the 'option' values
You can change the 'select' element height.
You just need to use the height CSS attribute for the select tag.
label select {
height: 100px;
}
<label>
<select>
<option selected>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
</label>
<select style="min-height:40px;min-width:100px;">
<option value="">Select1</option>
<option value="">Select2</option>
<option value="">Select3</option>
</select>
You can style the select and option elements usign CSS like this:
select {
height: 200px;
color: red;
}
option {
color: green;
}
Notice that you have to remove the "." before select and option.
This will overwrite any select and option element.
EDIT:
You can also refer to a certain select element by giving to that element a class name, like this:
select.s1{
height: 100px;
}
select.s2{
height: 150px;
}
<select class="s1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<select class="s2">
<option value="fiat">Fiat</option>
<option value="ferrari">Ferrari</option>
<option value="alfaromeo>Alfa Romeo</option>
<option value="lancia">Lancia</option>
</select>
I have the following code and I want to hide the last option of the select box ie option - 3
select[name="test"]:not(optgroup) option:last-child {
display: none;
}
<select name="test">
<option value="one">1</option>
<optgroup label="emails">
<option value="two">2</option>
</optgroup>
<option value="three">3</option>
</select>
But the above CSS code doesn't work.
Any help is highly appreciated. Thanks in advance.
see my attempt below
select option[value=three] {
display: none;
}
<select name="test">
<option value="one">1</option>
<optgroup label="emails">
<option value="two">2</option>
</optgroup>
<option value="three">3</option>
</select>
Basically what you want is to hide the last direct option child of the select element:
select > option:last-child { display: none; }
<select name="test">
<option value="one">1</option>
<optgroup label="emails">
<option value="two">2</option>
</optgroup>
<option value="three">3</option>
</select>
Just a small edit on your CSS. :)
select[name="test"] > option:last-child { display: none;}
<select name="test">
<option value="one">1</option>
<optgroup label="emails">
<option value="two">2</option>
</optgroup>
<option value="three">3</option>
</select>
You can also use the value selector like this:
option[value="three"] {
display: none;
}
<select name="test">
<option value="one">1</option>
<optgroup label="emails">
<option value="two">2</option>
</optgroup>
<option value="three">3</option>
</select>