how style list of select menu html and css [duplicate] - html

This question already has answers here:
How to style the option of an html "select" element?
(21 answers)
Closed 3 years ago.
ok i have code like this html
<div class="select_countries">
<select name="region" id="countries">
<option value="none" selected disabled hidden>Filter by Region</option>
<option value="all">All</option>
<option value="africa">Africa</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
<option value="oceania">Oceania</option>
</select>
</div>
i try on different ways to style list items and make gap between select menu and list items but i don't figure right way
[url=https://imge.to/i/5rw8i][img]https://b.imge.to/2019/07/20/5rw8i.jpg[/img][/url]
https://screenshot.net/dgp8of5

You are very limited in how browsers will apply padding within a select but one workaround is to set the appearance: none; along with its vendor prefixes.
This then allows you to use padding to your heart's content as below:
select
{
padding-bottom: 20px;
border: 1px solid #979997;
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
}
<div class="select_countries">
<select name="region" id="countries">
<option value="none" selected disabled hidden>Filter by Region</option>
<option value="all">All</option>
<option value="africa">Africa</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
<option value="oceania">Oceania</option>
</select>
</div>

There isn't much you can do to style the actual dropdown menu (besides bolding the options), you can only style the form field (how it looks before you click it and the dropdown shows)
if you want a custom dropdown menu, you can do a custom solution like the one here:
https://components.fleeksite.com/select

W3Schools has example with HTML,JS,CSS
here
DEMO FROM W3schools
(EDITED With edit make gap too)
var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < selElmnt.length; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
/*the container must be positioned relative:*/
.custom-select {
position: relative;
font-family: Arial;
}
.custom-select select {
display: none;
/*hide original SELECT element:*/
}
.select-selected {
background-color: #2d3945;
border-radius: 2px;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
/*style the items (options), including the selected item:*/
.select-items div,
.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
user-select: none;
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: #2d3945;
top: 150%; /*Made Gap here*/
left: 0;
right: 0;
z-index: 99;
border: 1px solid rgba(61, 143, 253, 1);
}
/*hide the items when the select box is closed:*/
.select-hide {
display: none;
}
.select-items div:hover,
.same-as-selected {
background-color: rgba(61, 143, 253, 1);
}
<div class="custom-select" style="width:200px;">
<select name="region" id="countries">
<option value="none" selected disabled hidden>Filter by Region</option>
<option value="all">All</option>
<option value="africa">Africa</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
<option value="oceania">Oceania</option>
</select>
</div>

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 get autoComplete search instuition name input field from api

When user write inside the input filed then show the suggestion of instuition name as per wrote inside input field .
Now , how can i solve this problem.
Thanks in advance.
I have done this type of thing before and it is really easy, all you need is an array of names in your JavaScript file, and then have a function that shows and displays those suggestions based on user input.
Here is an example:
// The names you want displayed as the user types
var instuitionNames = ["Techzilla", "Paragon Tech", "Mountain Hill Tech", "Example District Tech"];
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// Start autocompletion
autocomplete(document.getElementById("myInput"), instuitionNames);
/* Just some example CSS */
* { box-sizing: border-box; }
body {
font: 16px Arial;
}
.autocomplete {
/*the container must be positioned relative:*/
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*when hovering an item:*/
background-color: #e9e9e9;
}
.autocomplete-active {
/*when navigating through the items using the arrow keys:*/
background-color: DodgerBlue !important;
color: #ffffff;
}
<!--Make sure the form has the autocomplete function switched off:-->
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Instuition Name">
</div>
<input type="submit">
</form>
Reference: https://www.w3schools.com/howto/howto_js_autocomplete.asp

select list option go on top of control

I don't know why but for some reason even when I use the most plained example, either regular HTML or bootstrap, the option list goes on top of the select control itself.
No idea why.
Help someone?
My code looks like this
<form>
<div class="form-group">
<select class="custom-select" id="sel1">
#foreach( $state in $stateList )
<option value="$state">$state</option>
#end
</select>
</div>
</form>
<select class="custom-select">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
Both example gives back the same visual problem
enter image description here
This is going to look like a lot. But, here is an example with your code (only one select though). It isn't really a bootstrap solution its from W3c like your example.
You can try this:
var x, i, j, selElmnt, a, b, c;
/* Look for any elements with the class "custom-select": */
x = document.getElementsByClassName("test-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/* For each element, create a new DIV that will act as the selected item: */
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/* For each element, create a new DIV that will contain the option list: */
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < selElmnt.length; j++) {
/* For each option in the original select element,
create a new DIV that will act as an option item: */
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/* When an item is clicked, update the original select box,
and the selected item: */
var y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/* When the select box is clicked, close any other select boxes,
and open/close the current select box: */
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/* A function that will close all select boxes in the document,
except the current select box: */
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/* If the user clicks anywhere outside the select box,
then close all select boxes: */
document.addEventListener("click", closeAllSelect);
/* The container must be positioned relative: */
.test-select {
position: relative;
font-family: Arial;
}
.test-select select {
display: none; /*hide original SELECT element: */
}
.select-selected {
background-color: DodgerBlue;
}
/* Style the arrow inside the select element: */
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
/* Point the arrow upwards when the select box is open (active): */
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
/* style the items (options), including the selected item: */
.select-items div,.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
}
/* Style items (options): */
.select-items {
position: absolute;
background-color: DodgerBlue;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
/* Hide the items when the select box is closed: */
.select-hide {
display: none;
}
.select-items div:hover, .same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
<form>
<div class="form-group">
<div class="test-select">
<select class="custom-select">
<option value="0">Select car:</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
</div>
</div>
</form>

How do I make a select option wrap when it exceeds the max width?

For example, I have something like this:
With the following HTML code, I set the max width of the entire dropdown to be 80%, which I expect to affect each of the options.
I want to have it so that long options wrap around at the max point:
Where the red lines indicate what is considered "one option", and thus when I hover over it, everything in between the red lines should be selected
<select name=countries style="width:100%;max-width:30%;">
<option value=gs selected="selected">All</option>
<option value=gs>This is fine</option>
<option value=gs>This message should wrap because it is very long</option>
<optgroup label="Title">
<option value="optcheck">Option groups are long, so they should wrap too</option>
</optgroup>
</select>
https://jsfiddle.net/pxl70474/x7w85fnm/
If you are using bootstrap you can use the bootstrap selectpicker class with the data-content attribute.
<select class="selectpicker form-control" data-live-search="true"
id="subject_teacher_drop_down">
<option data-content="English" title="English">English</option>
<option data-content="Methodology of social..." title="Methodology of social science
with special reference to economics">
Methodology of social science with special reference to economics
</option>
</select>
You could try rewriting it like this. I pulled this out of W3Schools. This simply writes the select menu for a responsive view. You could apply it for your site as it does wrap the text. Or figure out some other way to apply it to your site.
var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 0; j < selElmnt.length; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
/*the container must be positioned relative:*/
.custom-select {
position: relative;
font-family: Arial;
}
.custom-select select {
display: none; /*hide original SELECT element:*/
}
.select-selected {
background-color: DodgerBlue;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
/*style the items (options), including the selected item:*/
.select-items div,.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
user-select: none;
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: DodgerBlue;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
/*hide the items when the select box is closed:*/
.select-hide {
display: none;
}
.select-items div:hover, .same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
<div class="custom-select" style="width:500px;">
<select>
<option value="0">HTML5</option>
<option value="1">Option groups are long, so they should wrap too</option>
<optgroup label="Title">
<option value="optcheck">Hypertext Markup Language, a standardized system for tagging text files to achieve font, color, graphic, and hyperlink effects on World Wide Web pages.</option>
</optgroup>
</select>
</div>
To make word wrap, try this css to extend height of container to make enough space to wrap text.
option {
max-width: 100%;
overflow: hidden;
word-wrap: normal !important;
white-space: normal;
}
This simple change worked for me.
option {
white-space: break-spaces;
}
Expanding on Pulin's answer
To get this to work in Firefox, I needed to target the :before pseudoelement:
option:before {
white-space: break-spaces;
}
This let my multi-select options take up more than one line of text!

HTML select option text monospace

I am trying to get select options to use monospace fonts so that they are lined up vertically when you click the drop down. I am trying to put a code left justified followed by a dash and then a description. I added the options using coded spaces so that each option has the same number of characters before the dash, but they still are not lined up. I tried courier new and monospace. I can tell it is using the fonts because they change, but they are still not lined up. Here is the code:
<!DOCTYPE html>
<html>
<head>
<title>font test</title>
<style>
select, option{
font-family:monospace, monospace;
}
</style>
</head>
<body>
<form>
<select name=SOURCECODE>
<option value="" selected>Select a Option</option>
<option value="A">A - TEST A</option>
<option value="AB">AB - TEST AB</option>
<option value="ABC">ABC - TEST ABC</option>
<option value="ABCD">ABCD - TEST ABCD</option>
<option value="A">A - TEST A</option>
<option value="AB">AB - TEST AB</option>
<option value="ABC">ABC - TEST ABC</option>
<option value="ABCD">ABCD - TEST ABCD</option>
</select>
</form>
</body>
</html>
Is there a way to make this work?
****Note this appears to only be a problem with firefox
Ok, reading throught several articles, it seems that this is a Mozilla Firefox's bug, and the font style cannot actually be set at the moment (as of 10.5.2018). I have personally tested on 59.0.2 and 60.0 on Windows 10 (x64), both have the same bug.
5 years old bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=910022
11 months old bug report (for BG color): https://bugzilla.mozilla.org/show_bug.cgi?id=1376443
SO question on the same topic: CSS Font-Family Support Dropped for <SELECT> in Firefox?
I have also noticed this writting in regards to option tag:
Firefox refuses to apply background-color to option tag on a select menu.
Problem encountered since Firefox 48 and +, on Windows 7, 8.1 and 10 (all x64)
Problem not encontered on Firefox 48 and +, on Windows XP 32 bits
Solutions that would usually work (but not working in this case):
changing font-family to monospace
adding tabs ( ) and <pre></pre> tags
as Kelvin Samuel noted, creating own custom Select might work (e.g. following this tutorial, see below working solution)
Working custom select from tutorial:
var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < selElmnt.length; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
/*the container must be positioned relative:*/
.custom-select {
position: relative;
font-family: monospace;
}
.custom-select select {
display: none; /*hide original SELECT element:*/
}
.select-selected {
background-color: DodgerBlue;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
/*style the items (options), including the selected item:*/
.select-items div,.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: DodgerBlue;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
/*hide the items when the select box is closed:*/
.select-hide {
display: none;
}
.select-items div:hover, .same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
<!--surround the select box within a "custom-select" DIV element.
Remember to set the width:-->
<div class="custom-select" style="width:200px;">
<select>
<option value="" selected>Select a Option</option>
<option value="A">A - TEST A</option>
<option value="AB">AB - TEST AB</option>
<option value="ABC">ABC - TEST ABC</option>
<option value="ABCD">ABCD - TEST ABCD</option>
<option value="A">A - TEST A</option>
<option value="AB">AB - TEST AB</option>
<option value="ABC">ABC - TEST ABC</option>
<option value="ABCD">ABCD - TEST ABCD</option>
</select>
</div>