Option within select mouse hover [duplicate] - html
Is it possible to change the default background color of a select list option on hover?
HTML:
<select id="select">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I have tried option:hover { background-color: red; }, but it is of no use. Does anybody know how to do this?
This can be done by implementing an inset box shadow.
eg:
select.decorated option:hover {
box-shadow: 0 0 10px 100px #1882A8 inset;
}
Here, .decorated is a class assigned to the select box.
Hope you got the point.
Select / Option elements are rendered by the OS, not HTML. You cannot change the style for these elements.
This way we can do this with minimal changes :)
option:hover {
background-color: yellow;
}
<select onfocus='this.size=10;' onblur='this.size=0;' onchange='this.size=1; this.blur();'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Implementing an inset box shadow CSS works on Firefox:
select option:checked,
select option:hover {
box-shadow: 0 0 10px 100px #000 inset;
}
Checked option item works in Chrome:
select:focus > option:checked {
background: #000 !important;
}
There is test on https://codepen.io/egle/pen/zzOKLe
For me this is working on
Google Chrome
Version 76.0.3809.100 (Official Build) (64-bit)
Newest article I have found about this issue by Chris Coyier (Oct 28, 2019) https://css-tricks.com/the-current-state-of-styling-selects-in-2019/
The problem is that even JavaScript does not see the option element being hovered. This is just to put emphasis on how it's not going to be solved (any time soon at least) by using just CSS:
window.onmouseover = function(event)
{
console.log(event.target.nodeName);
}
The only way to resolve this issue (besides waiting a millennia for browser vendors to fix bugs, let alone one that afflicts what you're trying to do) is to replace the drop-down menu with your own HTML/XML using JavaScript. This would likely involve the use of replacing the select element with a ul element and the use of a radio input element per li element.
Select / Option elements are rendered by the OS/Client, not HTML.
You cannot change the style for these elements in modern Browser.
On older clients
select option:checked,
select option:hover {
box-shadow: 0 0 10px 100px #000 inset;
}
Checked option item works in older Chrome:
select:focus > option:checked {
background: #000 !important;
}
Adding/toggling size attributes on focus event as suggestest by #Krishnaraj works pretty well on desktop using mouse controls.
However, the previous answers don't work well with keyboard controls.
The following example wraps the aforementioned state toggling into a javaScript helper function and adds additional event listeners for better accessibility
setSelectHover();
function setSelectHover(selector = "select") {
let selects = document.querySelectorAll(selector);
selects.forEach((select) => {
let selectWrap = select.parentNode.closest(".select-wrap");
// wrap select element if not previously wrapped
if (!selectWrap) {
selectWrap = document.createElement("div");
selectWrap.classList.add("select-wrap");
select.parentNode.insertBefore(selectWrap, select);
selectWrap.appendChild(select);
}
// set expanded height according to options
let size = select.querySelectorAll("option").length;
// adjust height on resize
const getSelectHeight = () => {
selectWrap.style.height = "auto";
let selectHeight = select.getBoundingClientRect();
selectWrap.style.height = selectHeight.height + "px";
};
getSelectHeight(select);
window.addEventListener("resize", (e) => {
getSelectHeight(select);
});
/**
* focus and click events will coincide
* adding a delay via setTimeout() enables the handling of
* clicks events after the select is focused
*/
let hasFocus = false;
select.addEventListener("focus", (e) => {
select.setAttribute("size", size);
setTimeout(() => {
hasFocus = true;
}, 150);
});
// close select if already expanded via focus event
select.addEventListener("click", (e) => {
if (hasFocus) {
select.blur();
hasFocus = false;
}
});
// close select if selection was set via keyboard controls
select.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
select.removeAttribute("size");
select.blur();
}
});
// collapse select
select.addEventListener("blur", (e) => {
select.removeAttribute("size");
hasFocus = false;
});
});
}
body {
font-size: 10vmin;
}
select {
--selectHoverCol: #999;
--selectedCol: red;
width: 100%;
font-size: 1em;
padding: 0.3em;
background-color: #fff;
}
select:focus {
border-radius: 0px;
border-color: red;
background: #fff;
outline: none;
}
.select-wrap {
position: relative;
}
.select-wrap:focus-within select {
position: absolute;
top: 0;
left: 0;
z-index: 10
}
option:hover {
background-color: var(--selectHoverCol);
color: #fff;
}
option:checked {
box-shadow: 0 0 1em 100px var(--selectedCol) inset;
}
<select class="selectHovercolor">
<option value="volvo" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<p>paragraph</p>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="Lexus">Lexus</option>
<option value="Mercedes">Mercedes</option>
</select>
<p>paragraph</p>
The select field is wrapped in a <div> with relative position.
:focus-within pseudo state toggles the select positioning between absolute and initial (static) – this way we can avoid layout shifts.
the wraping div height is recalculated on resize
since the focus event coincides with with the click event, we add a delay for click events, triggering the select field to collapse after selection
If an option was selected via keyboard controls and selection was confirmed by pressing "enter" - the size attribute is removed.
size attribute is set according to the actual number of <option> elements
You can do this, just know that it will change all of the select inputs throughout the html, it doesn't change the blue hover, but it does style everything else.
option {
background: #1b1a1a !important;
color: #357b1d !important;
}
select {
background: #1b1a1a !important;
color: #357b1d !important;
}
// If you also want to theme your text inputs:
input {
background: #1b1a1a !important;
color: #357b1d !important;
}
<html>
<head>
<style>
option:hover {
background-color: yellow;
}
</style>
</head>
<body>
<select onfocus='this.size=10;' onblur='this.size=0;' onchange='this.size=1; this.blur();'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
This worked for me in chrome!
<select onfocus='this.size=10;'>
<option>Crossing</option>
<option>Crossing Up</option>
<option>Crossing Down</option>
</select>
<style>
select option:hover {
box-shadow: 0 0 10px 100px green inset;
color:white;
}
select option:checked{
box-shadow: 0 0 10px 100px green inset;
}
</style>
However, the checked option's background will remain same even if i hover on another option
By the way, you can do that one as well.
Here is the link for that: https://www.w3schools.com/howto/howto_custom_select.asp
I would consider switching from a <select> element to a <div> list, like below:
https://jsfiddle.net/getbutterfly/gquh02dz/
This will make it cross-browser compatible. Every other method using CSS appearance tricks and <select> dropdowns is hacky.
HTML
<div class="sel">
<div class="label">Select option...</div>
<div class="options">
<div>Option 1</div>
<div>Option 2</div>
<div>Option 3</div>
<div>Lot of text to display, so it can expand multiple lines and expand the select main text also</div>
</div>
</div>
JavaScript
const sel = document.querySelector('.sel');
const label = document.querySelector('.label');
const options = document.querySelector('.options');
options.setAttribute('hidden', true);
sel.addEventListener('click', (e) => {
e.stopPropagation();
options.removeAttribute('hidden');
});
document.body.addEventListener('click', (e) => {
options.setAttribute('hidden', true);
});
options.addEventListener('click', (e) => {
if (e.target.tagName === 'DIV') {
e.stopPropagation();
label.textContent = e.target.textContent;
e.target.classList.add('selected');
Array.from(e.target.parentNode.children).forEach((child) => {
if (child !== e.target) {
child.classList.remove('selected');
}
});
options.setAttribute('hidden', true);
}
});
CSS
* {
box-sizing: border-box;
}
.sel {
color: #000000;
width: 250px;
box-sizing: border-box;
background-color: #ffffff;
border: 1px solid #000000;
overflow: hidden;
background: url("data:image/svg+xml,<svg height='10px' width='10px' viewBox='0 0 16 16' fill='%23000000' xmlns='http://www.w3.org/2000/svg'><path d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/></svg>") no-repeat calc(100% - 10px) 14px;
}
.label,
.sel .options div {
padding: 10px;
}
.selected {
background-color: #ff0000;
}
.sel .options {
width: 250px;
background-color: #ffffff;
}
.sel .options div:hover {
background-color: #00ff00;
}
With a bit of extra CSS, the dropdown can be animated and the selected text can be truncated to fit inside a fixed height, behaving exactly like a <select> element.
I realise this is an older question, but I recently came across this need and came up with the following solution using jQuery and CSS:
jQuery('select[name*="lstDestinations"] option').hover(
function() {
jQuery(this).addClass('highlight');
}, function() {
jQuery(this).removeClass('highlight');
}
);
and the css:
.highlight {
background-color:#333;
cursor:pointer;
}
Perhaps this helps someone else.
this is what you need, the child combinator:
select>option:hover
{
color: #1B517E;
cursor: pointer;
}
Try it, works perfect.
Here's the reference: http://www.w3schools.com/css/css_combinators.asp
Related
div color depends on two output
I want to create a div which color depends on two select input. The soil_colour1 and soil_colour2 will determined the colour of the div(colorbox). below are my current code: <select name="soil_colour" id="soil_colour1"> <option value="dark">Dark</option> <option value="medium">Medium</option> <option value="light">Light</option> <option value="pale">Pale</option> </select> <select name="soil_colour" id="soil_colour2"> <option value="grey">Grey</option> <option value="purple">Purple</option> <option value="blue">Blue</option> <option value="green">Green</option> <option value="brown">Brown</option> <option value="orange">Orange</option> <option value="yellow">Yellow</option> <option value="red">Red</option> </select> <div id="colorbox" style="background-color: #FFFFFF7F ; padding: 10px; border: 1px; width: 23%"> </div> the example script that i had in mind <script type="text/javascript"> function findColor() { if (document.getElementById('soil_colour1').value = 'dark') { if (document.getElementById('soil_colour2').value = 'grey') { document.getElementById('colorbox').style.background-color = '#4F4E50' } } } </script>
In your colorbox definition you put the background color in hey with transparency. It's working well now in browser in easier to combine. If you make a console log with your background color, browser will give you a rgb (or rgba) value. You can have a function to convert rgb to hex, if you search here you'll find this question. But here, I made the snippet with hex value, faster, shorter. Because browser will give you rgb, I'm using data attribute to store the full hex current color background in colorbox. For HTML color name in hex, look here: https://www.w3schools.com/colors/colors_names.asp For transparency in hex, look here: Hex transparency in colors document.querySelector('#soil_colour1').addEventListener('change', evt => { const elcolbox = document.querySelector('#colorbox'); const color = elcolbox.dataset.bkgcol.slice(0, -2); elcolbox.dataset.bkgcol = color + evt.target.value; elcolbox.style.backgroundColor = color + evt.target.value; }) document.querySelector('#soil_colour2').addEventListener('change', evt => { const elcolbox = document.querySelector('#colorbox'); const transparency = document.querySelector('#colorbox').dataset.bkgcol.slice(-2); elcolbox.dataset.bkgcol = evt.target.value + transparency; elcolbox.style.backgroundColor = evt.target.value + transparency; }) <select name="soil_colour" id="soil_colour1"> <option value="E6">Dark</option> <option value="99">Medium</option> <option value="66">Light</option> <option value="33">Pale</option> </select> <select name="soil_colour" id="soil_colour2"> <option value="#808080">Grey</option> <option value="#800080">Purple</option> <option value="#0000FF">Blue</option> <option value="#008000">Green</option> <option value="#A52A2A">Brown</option> <option value="#FFA500">Orange</option> <option value="#FFFF00">Yellow</option> <option value="#FF0000">Red</option> </select> <div id="colorbox" style="background-color: #FF0000FF ; padding: 10px; border: 1px; width: 23%" data-bkgcol="#FF0000FF"> </div>
One strategy to achieve that result could be to control the color value using the HSL model and definining the map of colors (weight+colorname = color to set the css background property) as css classes addressing the color weight by setting the lightness and the color name setting the hue and saturation. https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl In this demo I have the weight category of classes defined starting with lightness-* and the color name category of classes defined as color-*. In this way you can define the color map using a css asset instead of dealing with js configuration. Each of those classes just control the css custom variables: --hue, --saturation, --lightness Those variable are used by the master css rule assigned to the target element that you wish to style with the given color as background: .custom-background-color { background: hsl(var(--hue) var(--saturation) var(--lightness)); } Notes: The demo expects a target object to be the subject of the styling but since we had that custom-background-color class it could be a criteria to deduce which objects are supposed to be styled like that without having to pass a specific element fetched in advance Then the javascript code just adds a change event listener on both the dropdowns controlling respectively the color weight and name by adding the respective classes to the target element after any were first unset. The name of the weight and colorname classes to add to the element are a function of the values coming from the dropdown. const colorbox = document.getElementById('colorbox'); const colorName = document.getElementById('color_name'); const colorWeight = document.getElementById('color_weight'); colorName.addEventListener('change', onColorPicked); colorWeight.addEventListener('change', onColorPicked); onColorPicked(); function onColorPicked(){ clearColor(colorbox); setColor(colorbox, colorName.value, colorWeight.value); } //sets the background color for target as name, weight function setColor(target, name, weight) { target.classList.add(`color-${name}`, `lightness-${weight}`); } //removes the class color-* and lightness-* if any function clearColor(target){ [...target.classList].forEach( className => { if ( className.startsWith('color-') || className.startsWith('lightness-')) { target.classList.remove(className); } }); } *{ box-sizing: border-box; } body{ display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 15% 70%; gap: 1em; padding: .5em; height: 95vh; } select{ cursor: pointer; } #colorbox { padding: 10px; width: 100%; margin-top: .5em; grid-column: span 2; height: 100%; } .custom-background-color { --hue: 100; --saturation: 100%; --lightness: 100%; background: hsl(var(--hue) var(--saturation) var(--lightness)); } /* lightness */ .lightness-dark { --lightness: 10% !important; } .lightness-medium { --lightness: 30% !important; } .lightness-light { --lightness: 50% !important; } .lightness-pale { --lightness: 70% !important; } /* colors */ .color-grey { --hue: 0; --saturation: 0%; } .color-purple { --hue: 270; --saturation: 100%; } .color-blue { --hue: 240; --saturation: 100%; } .color-green { --hue: 120; --saturation: 100%; } .color-brown { --hue: 30; --saturation: 50%; } .color-orange { --hue: 30; --saturation: 100%; } .color-yellow { --hue: 60; --saturation: 100%; } .color-red { --hue: 0; --saturation: 100%; } <select id="color_weight"> <option value="dark">Dark</option> <option value="medium">Medium</option> <option value="light">Light</option> <option value="pale" selected>Pale</option> </select> <select id="color_name"> <option value="grey">Grey</option> <option value="purple">Purple</option> <option value="blue">Blue</option> <option value="green">Green</option> <option value="brown">Brown</option> <option value="orange">Orange</option> <option value="yellow">Yellow</option> <option value="red" selected>Red</option> </select> <div id="colorbox" class="custom-background-color"> </div>
How to resize the select menu according to the height of selected option?
I have a select menu, each options has different length. How can I change the height of the select menu according to the height of selected option? The width of the select menu should be the same. body { background-color: black; } select { padding: 30px, 30px, 0, 0; max-width: 280px; min-width: 140px; background-color: white; height: 32px; border: 0; border-radius: 0; margin: 0 auto; cursor: pointer; box-sizing: border-box; white-space: normal; word-wrap: break-word; #media only screen and (max-width: 600px) { max-width: 320px; } #media only screen and (max-width: 400px) { max-width: 180px; } } <body> <select> <option value="first">First Value First Value First Value First Value First Value First Value</option> <option value="second" selected>Second Value Second Value Second Value Second Value</option> <option value="third">Third Value Third Value Third Value Third Value</option> </select> </body> Link to the CodePen Thanks!
Well this can easily be implemented if you use some jquery :P function function1() { var val = document.getElementById("ID1").value; console.log(val); if (val == "first") { $("#ID1").css( {"height": "20px"} ); } else if (val == "second") { $("#ID1").css( {"height": "50px"} ); } else if (val == "third") { $("#ID1").css( {"height": "100px"} ); } } select{ height: 50px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select Id="ID1" onchange="function1()"> <option value="first">First Value First Value First Value First Value First Value First Value</option> <option value="second" selected>Second Value Second Value Second Value Second Value</option> <option value="third">Third Value Third Value Third Value Third Value</option> </select> don't forget to add an id to your select as well as call the function1() onchange. <select Id="ID1" onchange="function1()">
How make select2 readonly?
I know that "readonly" feature does not exist for select2. Please check here. How do I achieve that? Any help would be appreciated. Thanks. Note: I cant use disabled. If I use disabled, I will not get the list of selected value.
I guess that issue is resolved or may be available for higher version as this worked for me. Please go through the example to make a select2 readonly here: Select 2 In js code: $("select").select2("readonly", true); You can put your own CSS like: .select2-container.select2-container-disabled .select2-choice { background-color: #ddd; border-color: #a8a8a8; }
$('.js-example-basic-single').select2({ disabled: true }); Render a disabled attribute after the page loads resolved for me. A reminder, a field with the disabled attribute is not sent on forms
as the question says: How to make select2 readonly?, and as the user pointed out: the function "readonly" does not exist for select2. According to the select2 team developers: read this. I just want to add a couple of things: disabled is not the same as readonly! be careful. $(element).select2({ disabled: true }); only works on versions prior to V4. In fact the answer marked as correct refers to V3. Having said that, I want to share a simple suggestion: destroy the element and make it readonly. $(element).select2('destroy').attr("readonly", true) if you need it available again you can always call him again. $(element).select2() tip: if you want it to look like the previous element, just remove the default css style: $(element).select2('destroy').attr("readonly", true).css({'-moz-appearance': 'none','-webkit-appearance': 'none'});
Try this: $('select option:selected').attr('disabled','disabled'); EDIT: For those using Select 2 version 4+, the new way to do it should be: $("select").prop("disabled", true); // instead of $("select").enable(false); After clarifying the question, this is the right way to do it: $('select').select2().enable(false);
After few tests trying to block expanding/opening of the Select2 items, I've found the way to apply a listener on each natives select tags having Select2 attribute id... and detect on opening event if the native tag is readonly. $('select[data-select2-id]').on('select2:opening', function (e) { if( $(this).attr('readonly') == 'readonly') { // Check if select tag is readonly. console.log( 'readonly, can't be open !' ); e.preventDefault(); $(this).select2('close'); return false; }else{ console.log( 'expandable/selectable' ); } }); For more custom on the Select2, we can add some CSS ... select[readonly] ~ .select2.select2-container .selection [role="combobox"] { background: repeating-linear-gradient( 45deg , #b4d2e4, #b4d2e4 10px, rgba(255, 255, 255, 0.15) 10px, rgba(255, 255, 255, 0.15) 20px) !important; box-shadow: inset 0 0 0px 1px rgba jQuery(document).ready(function($) { // Implement Select2 $( 'select' ).select2({ placeholder: "Saisissez un mot pour aide à saisie", tags: true, // allow create new width: '100%' }); // Just extra button to swap readonly $('#button_demo_2').off().on( 'click',function(e){ console.log( $('#select_demo_2').attr('readonly') ); if( typeof( $('#select_demo_2').attr('readonly') ) == 'undefined' ){ $('#select_demo_2').attr('readonly','readonly'); }else{ $('#select_demo_2').removeAttr('readonly'); } } ); // Demo code... $('select[data-select2-id]').on('select2:opening', function (e) { if( $(this).attr('readonly') == 'readonly') { console.log( 'can not open : readonly' ); e.preventDefault(); $(this).select2('close'); return false; }else{ console.log( 'can be open : free' ); } }); }); *{ margin : 0; padding : 0; } body { height: 100vh; background-color: #215a82; font-family: 'Roboto',sans-serif; background: linear-gradient(180deg,#215a82 0%,#152135 100%) no-repeat; display: -webkit-box !important; display: -ms-flexbox !important; display: flex !important; -webkit-box-align: center !important; -ms-flex-align: center !important; align-items: center !important; -ms-flex-pack: distribute !important; justify-content: space-around !important; } .container { display: -webkit-box !important; display: -ms-flexbox !important; display: flex !important; } div[role="box"]{ padding:1rem; margin:2rem display: block; } pre { line-height: 1rem; height: 1.5rem; color: white; } select[readonly] ~ .select2.select2-container .selection [role="combobox"] { background: repeating-linear-gradient(45deg, #dadada, #dadada 10px, rgba(255, 255, 255, 0.66) 10px, rgba(255, 255, 255, 0.66) 20px) !important; box-shadow: inset 0 0 0px 1px #77859133; } input{ display: block; padding: 0.5rem; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script> <main class="container"> <div role="box"> <pre></pre> <select class="form-control inputFocusable" id="select_base" name="select_base" tabindex="-1" aria-hidden="true"> <option value="A">Item A</option> <option value="B">Item B</option> <option value="C">Item C</option> </select> </div> <div role="box"> <pre>readonly</pre> <select class="form-control inputFocusable" id="select_demo_1" name="select_demo_1" tabindex="-1" aria-hidden="true" readonly> <option value="A">Item A</option> <option value="B">Item B</option> <option value="C">Item C</option> </select> </div> <div role="box"> <pre>readonly="readonly"</pre> <select class="form-control inputFocusable" id="select_demo_2" name="select_demo_2" tabindex="-1" aria-hidden="true" readonly="readonly"> <option value="A">Item A</option> <option value="B">Item B</option> <option value="C">Item C</option> </select> </div> </main> <div role="box"> <pre>readonly ?</pre> <input id="button_demo_2" type="button" value="Fix / Remove"> </div>
You can use $("#id_example").select2({readonly:true}); Disabled not send data on POST action.
you cannot make the select2 as readonly as per new version of select2, follow the link for the css hack, it works like a charm https://stackoverflow.com/a/55001516/9945426
$('#element_id').select2().readonly(true); This can be define your element readonly and still send your POST data
Try this : $(".example_element").css("pointer-events","none");
How can color the first item in a select?
I have this sample: link CODE HTML: <select name="card_type" id="card_type" class="select-full"> <option value="">Select</option> <option value="visa">Visa</option> <option value="mastercard">Mastercard</option> <option value="discovery">Discovery</option> <option value="maestro">Maestro</option> </select> CODE CSS: select { color: #ccc; } option { color: #000; } option:first-child { color: red; } I have a wish to spot and select only the first item in the list. The first element to be red and the other to be gray for example. Is there any possibility to do this? I tried the above code but does not work. Can you please tell me what is wrong? Thanks in advance!
I have made changes in snippet. Please check it. is this the way you want? $(document).ready(function(){ if($(this).val() == 0){ $('select').css("color","red"); } else{ $('select').css("color","#000") } $('select').change(function(){ if($(this).val() == 0){ $('select').css("color","red"); } else{ $('select').css("color","#000") } }) }) select { color: #ccc; } option { color: #000; } option:first-child { color: red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="card_type" id="card_type" class="select-full"> <option value="0">Select</option> <option value="visa">Visa</option> <option value="mastercard">Mastercard</option> <option value="discovery">Discovery</option> <option value="maestro">Maestro</option> </select>
Try this: select:first-child{color: red;}
Use nth-of-type(1) option:nth-of-type(1) { color: red; }
Well assuming something. I think you want your <option value="visa">Visa</option> to color red but note that this is second option so use this css select.select-full option:nth-child(2){ color:red; } fiddle link :https://jsfiddle.net/yudi/L439rrsd/
How to place a drop-down and a div inside a div, so that it ll act as a dropdown?
I am really confuded in this. I have a label and select drop-down inside my container which is right aligned. GOAL Container should act like a drop-down. Only sort-by label should be displayed initially.When user clicks on it, it should shoe the option to the user. Problem I don't know how to trigger drop down when i click on the sort by label. <div class="container"> <label class="Sortlabel">Sort by</label> <select> <option>2</option> <option>22</option> <option>222</option> </select> </div> If i must use jquery or JS, i ll add these tags also. Any suggestions??
And what is the difference with this one : http://jsfiddle.net/csdtesting/vuc81u87/ Result is the same. <div class="container"> <label class="Sortlabel"></label> <select> <option>Sort by</option> <option>22</option> <option>222</option> </select> </div> select { width:100%; -webkit-appearance: none; } .container { float: right; width:190px; } But if you insists.I took this idea and here it is (Pure Javascript): http://jsfiddle.net/csdtesting/ybjdsqrx/ var state = false; // <select> element displays its options on mousedown, not click. showDropdown = function(element) { var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; // This isn't magic. window.runThis = function() { var dropdown = document.getElementById('sel'); showDropdown(dropdown); }; select { -webkit-appearance: none; border: none; width: 70%; } .container { width: 100%; float: left; width: 190px; border: 1px solid; } .Sortlabel { width: 20%; } } <div class="container"> <label class="Sortlabel" onclick="runThis()">Sort by</label> <select id="sel"> <option></option> <option>2</option> <option>22</option> <option>222</option> </select> </div>