Select list required attribute styling - html

I have a select list which has the attribute 'required' with the first item set as disabled and selected:
<select tabindex='4' id='storeys' name='storeys' required >
<option selected disabled>Select an option</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
My css is:
select{
border: 1px solid rgb(171, 171, 171);
height: 24px;
font-size: 16px;
height:24px;
width:250px;
color:grey;
}
option{color: #000;}
option:first-child{color: #ccc;}
select:required{border-right:3px solid red;}
select:valid{border-right: 3px solid green;}
Question is how do I make the select list border red when the selected option has no value(ie the disabled option) and green when a value has been selected
I have added fiddle here http://jsfiddle.net/czs1w6br/ which also has a text input demonstrating the effect

Currently, the value property of your first option would be "Select an option", which is a perfectly valid value.
Give the first option an empty value attribute:
<select tabindex='4' id='storeys' name='storeys' required >
<option value='' selected disabled>Select an option</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
JSFiddle

Related

HTML <select> <option> How to set up click/selected color?

Hi there wonderful people. I have a question, it may be only related to firefox but, how do I change the color of the selected <option> when the focus is in and out of the <select>? I don't mean on :hover that works fine I'm talking when you click on a <option> and after that when you click out of the <select> object.
The intention is to change the background-color/color of the selected <option> when the <select> is in focus or not in focus. In Firefox it shows in windows system blue/white with dotted outline and in Chrome is showing in a lightb blue/white on focus and white/grey on out of focus... I don't want that.
CSS
.select_style{
background-color: #555;
}
.select_style option{
background-color: #ccc;
}
.select_style option:hover{
background-color: #0f0;
}
HTML
<select class="select_style" size='3' multiple='multiple'>
<option value="">Please choose</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
This made the trick, remember the code is using a a "selectbox" <select> with the attribute multiple it does't act as the normal "droplist" <select>.
CSS
option:checked{
background: #00ff00 !important;
box-shadow: 0 0 10px 100px #00ff00 inset;
}

Change color of a particular option under select tag

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.

How to change the color of a placeholder option in a Select tag?

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;
}

Style invalid <select> using :invalid

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>

border around drop down list

Can i somehow remove this dashed border in drop down list. Every time i click on drop down list i get this dashed border. Example is in image.
edit:
css:
option {
height: 20px;
padding: 7px 0 5px 3px;
outline: none;
border: none;
}
html:
<select onchange="window.open(this.options[this.selectedIndex].value,'_top')">
<option value="">Razvrsti restavracije po</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
</select>
I think the border is used by some people who prefer to navigate using their keyboard. So it might not be a good idea to remove it.
I don't think it is possible with CSS. What is known is that dashed border has the same color as text, so if you set the text-color same as your background it will "disappear" but your text will too:
Maybe you can play with some javascript:
onmouseover="this.style.color='#DFF1FA'" onmouseout="this.style.color='#000000'"
Or add this at your onchange att: this.blur()
<select onchange="window.open(this.options[this.selectedIndex].value,'_top'); this.blur()">
<option value="" style="color:black">Razvrsti restavracije po</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
<option value="#">Odrto test</option>
</select>
With it the dashed outline will disappear after choosing an option.
You can do even more...
JavaScript rulez!
You can disable such borders using the css outline property:
outline: none;