MaterializeCSS Select Background Color - html

I tried the background color of the MaterializeCSS select. I attempted to use this question, but it did not work Change select box option background color as it was for regular HTML (no Materialize).
How could I do this?

It has to do with Materialize's selectors. The below code worked for me:
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<div class="input-field col s12">
<select class="select1">
<option value="1" selected>Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Select</label>
</div>
<style>
input.select-dropdown {
background: #1A1B1C !important;
color: #26a69a;
}
ul.dropdown-content.select-dropdown li span {
background: #1A1B1C;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
});
</script>

Related

mouseleave is triggered on <select> when the cursor moves to the options inside in Firefox

I have 2 <select> inside a hidden <div> which is revealed by a hover function in jQuery. The problem is that once the <div> is visible, if I click on any of the <select>s and move the cursor to the options inside, they collapse this <div> making it impossible to interact with them. This is an issue that I've been experiencing only in Firefox.
If anyone can give me a clue of what's causing the problem that'd be greatly appreciated.
<div id="tester-container">
<div class="options-container">
<select id="tester-fs">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3" selected>Option 3</option>
</select>
<select id="tester-align">
<option value="left" selected>Left</option>
<option value="center">Center</option>
<option value="right">Right</option>
</select>
</div>
<div class="font-tester">
<div style="font-size: 80px;">hover here</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".options-container").hide();
});
$('#tester-container').hover(mouseEnter, mouseLeave);
function mouseEnter() {
$(".options-container").show(200);
};
function mouseLeave() {
$(".options-container").hide(200);
};
</script>
This is the reported issue in firefox. However I have made few changes on mouseleave event and also have updated the functions and now it will work fine on any browser also in firefox.
$(document).ready(function() {
$(".options-container").hide();
});
$('#tester-container').mouseenter(function() {
$('.options-container').show(200);
});
$('#tester-container').on('mouseleave', function(event) {
if ($('.options-container').has(event.target).length > 0) {
event.stopPropagation();
return;
} else {
$('.options-container').hide(200);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="tester-container">
<div class="options-container">
<select id="tester-fs">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3" selected>Option 3</option>
</select>
<select id="tester-align">
<option value="left" selected>Left</option>
<option value="center">Center</option>
<option value="right">Right</option>
</select>
</div>
<div class="font-tester">
<div style="font-size: 80px;">hover here</div>
</div>
</div>

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.

I want to change the background color of the select box

I want to change the background color of a select box. The color is #F1C26A
for the background of the selector. i tried changing the color and the background for this.
But don't change.
Any idea?
Thanks for the help
select.form-control:not([size]):not([multiple]) {
height: calc(2.25rem + 2px); }
select.form-control:focus::-ms-value {
color: #F1C26A;
background-color: #fff; }
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="col-6">
<select id="Pizzas" name="Pizzas" class="form-control">
<option value="1" >Option 1</option>
<option value="2" >Option 2</option>
</select>
</div>
</body>
</html>
Just remove -ms-value as it is not supported by most browsers only edge and IE.
select.form-control:not([size]):not([multiple]) {
height: calc(2.25rem + 2px); }
select.form-control:focus {
color: #F1C26A;
background-color: #fff; }
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="col-6">
<select id="Pizzas" name="Pizzas" class="form-control" >
<option value="1" >Option 1</option>
<option value="2" >Option 2</option>
</select>
</div>
</body>
</html>
Set the background-color on the select itself, like so select { background-color: #F1C26A; }
If you want to change the background option not only select, I recommend using, for example, select2 select2.org.
Background definitely browser, using select2, you can simply change styles.
Of course, there are more variations of change select

Materialize CSS select statement not showing

Although this question has already been asked in this thread:
Materialize CSS - Select Doesn't Seem to Render
I'm having problems with where to place this code:
$(document).ready(function() {
$('select').material_select();
});
At the moment, the most logical thing I can think of is to place it like so:
<body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js">
$(document).ready(function() {
$('select').material_select();
});
</script>
<div class="input-field col s12">
<select>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Select</label>
</div>
</body>
I've tried changing the "document" to the name of my document, in this case "index", but it still doesn't work.
Am I just being slow?
Thanks in advance.
Try this:
<body>
<div class="input-field col s12">
<select>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Select</label>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
<script>
$(document).ready(function() {
$('select').material_select();
});
</script>
</body>
You need use a class "browser-default" to show the select combo. Like this:
<select name="name" class="browser-default"><option>- options -</option></select>
i had same problem in 2019, these answers helped, but a little update cause of the Materialize version upgraded
M's select and form are better than browser-default one:)
now the initialization code is changed to
$(document).ready(function(){
$('select').formSelect();
});
https://materializecss.com/select.html
You need to initialise the select element as shown here :
https://materializecss.com/select.html#initialization
$(document).ready(function(){
$('select').formSelect();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<div class="input-field col s12">
<select multiple>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Multiple Select</label>
</div>
this work for me. The main change is the version, not use 1.0.0. then use 0.97.2
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.2/js/materialize.min.js"></script>
<script>
$(document).ready(function() {
$('select').material_select();
});
</script>
<?!= include("Planificacion-js");?>
</body>

How to center an element vertically in a div?

I'm practising making a form and I would like to position the form elements on the left in the centre of their row sort of how it would automatically be done with a table. Here is some jsFiddle for what i've been attempting so far.
How do I vertically centre the label element for a form?
Give label same height as input filed have & give line-height to your label. Like this:
.element label {
float: left;
font-weight: 700;
height: 30px;
line-height: 30px;
width: 156px;
}
You could use jQuery and setup the position after the elements are loaded. Minor adjustments to HTML (assign IDs):
<div class="element">
<label id="feedback">Feedback: </label>
<select multiple="multiple" name="feedback" size="4" id="selectbox">
<option value="">Option1</option>
<option value="">Option1</option>
<option value="">Option2</option>
<option value="">Option3</option>
<option value="">Option4</option>
<option value="">Option5</option>
</select>
</div>
Javascript:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){
var ht = $("#selectbox").height();
var ht2 = $("#feedback").height();
ht = $("#selectbox").offset().top + (ht-ht2) / 2;
$("#feedback").offset({ top: ht });
});
</script>
Strictly CSS (ID assignment on the label)
<div class="element">
<label id="feedback">Feedback: </label>
<select multiple="multiple" name="feedback" size="4">
<option value="">Option1</option>
<option value="">Option1</option>
<option value="">Option2</option>
<option value="">Option3</option>
<option value="">Option4</option>
<option value="">Option5</option>
</select>
</div>
And the CSS:
.element select {
height: 200px;
}
.element label#feedback {
margin-top: 85px;
}