guys i need your litle help i want to make select that show all option without to click select, i try add "multiple" but when i try in mobile display go wrong
this is what i want in front-end to mobile device
but on mobile
here is my code
<style>
select {
background-color: transparent;
border: none;
margin: 0;
width: 100%;
font-family: inherit;
font-size: 14;
cursor: inherit;
line-height: inherit;
outline: none;
text-align: center;
height: 90;
}
select option {
margin-top: 10;
}
</style>
<div class="select m-auto">
<select id="standard-select" name="bahasa" multiple>
<option value="id">Bahasa Indonesia</option>
<option value="en">Inggris</option>
<option value="viet">Vietnamese</option>
</select>
</div>
do be aware that size or multiple is not respected by mobile devices, but it works fine on desktops
you can try - overflow:auto;
select {
background-color: transparent;
border: none;
margin: 0;
width: 100%;
font-family: inherit;
font-size: 14;
cursor: inherit;
line-height: inherit;
outline: none;
text-align: center;
overflow:auto;
}
<div class="select m-auto">
<select id="standard-select" name="bahasa" multiple>
<option value="id">Bahasa Indonesia</option>
<option value="en">Inggris</option>
<option value="viet">Vietnamese</option>
</select>
</div>
Dynamically setting the label attribute would be enough for all iOS versions to display the control label. Turns out this depends on the iOS version.
you not setting innerText on iOS 7-9, but iOS 10 & up requires innerText to be set for the label to display.
required iOS 10 & up
let option = document.createElement('option');
option.label = option.innerText = 'option label';
option.value = '123';
not required on iOS 7-9
var option = document.createElement('option');
option.label = 'option label';
option.value = '123';
Related
I'm new to CSS, and the following example is confusing me. So I would like to get a better understanding.
Here's what I did:
HTML:
<select id="dropdown" required>
<option disabled selected value>Choose current role</option>
<option class="option">Student</option>
<option class="option">Full Time Job</option>
<option class="option">Prefer Not to Say</option>
<option class="option">Others</option>
</select>
CSS:
body: {color: white;}
Either before and after any option is clicked on the webpage, the text color on the Select bar will be white.
I tried to change the text color with below syntax but to no avail:
#dropdown {
padding-right: 100%;
}
#dropdown:focus:after {
color: black;
}
It only works when I take out the #dropdown declaration:
#dropdown:focus:after {
color: black;
}
But I want to keep the #dropdown declaration for the creating padding. Is there other way to make this work?
And why doesn't it work with both #dropdown and #dropdown:focus:after declarations?
Try this:
var select = document.getElementById('mySelect');
select.onchange = function () {
select.className = 'redText';
}
.redText {
background-color:#F00;
}
<select id="mySelect">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
This is with javascript, i did it as easy as possible.
<select> tags are difficult to style, you'll need to strip it down first by using appearance: none on the select.
body {
font: 2ch/1 Consolas;
}
fieldset {
position: relative;
display: inline-block;
border: 0;
}
select {
appearance: none;
}
#dropdown {
display: inline-block;
color: tomato;
height: 28px;
padding: 3px 30px 3px 6px;
font: inherit;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
background: #000;
}
#dropdown:focus {
color: lime;
background: #333;
}
.icon {
position: absolute;
top: 6px;
right: 16px;
display: inline-block;
padding: 0;
width: 22px;
height: 27px;
background: url(https://i.ibb.co/Kx33pSY/01.jpg) right / 90% no-repeat #000;
pointer-events: none;
}
<fieldset>
<legend>Whith Style</legend>
<output class='icon'></output>
<select id="dropdown" required>
<option disabled selected>Choose current role</option>
<option class="option">Student</option>
<option class="option">Full Time Job</option>
<option class="option">Prefer Not to Say</option>
<option class="option">Others</option>
</select>
</fieldset>
<fieldset>
<legend>Without Style</legend>
<select required>
<option disabled selected>Choose current role</option>
<option class="option">Student</option>
<option class="option">Full Time Job</option>
<option class="option">Prefer Not to Say</option>
<option class="option">Others</option>
</select>
</fieldset>
You can try this too:
select option {
background - color: white;
font - weight: bold;
color: red;
}
An HTML select element can be styled, however very minimally. Otherwise, if you want every different option colour, give a separate class or id to all options.
I have this html for an select control
<select class="form-control">
<option value="0"></option>
<option value="1: 1" >Jr.</option>
<option value="2: 2">Sr.</option>
<option value="3: 3">I</option>
<option value="4: 4">II</option>
<option value="5: 5">III</option>
</select>
It is getting rendered as expected in chrome
chrome image 1
chrome image 2
but in IE, the select option is hiding the control when it is clicked or in other words the the select option is not getting opened from the bottom of the select control as seen in this following screen shot
IE image 1
IE image 2
is this a default behaviour or can I change it? I tried giving using this css but did not work
select.form-control {
width: 100%;
max-width: 325px;
border-width: 0 0 1px 0;
box-shadow: none;
border-radius: 0;
-moz-appearance: none;
text-indent: .01px;
text-overflow: '';
position: relative;
}
option, select.form-control option {
color: blue !important;
top: 0px !important;
position: absolute !important;
}
any suggestion?
This is standard behavior. Every browser renders elements slightly different and has their own styles for it. Some styles can be changed, others are hidden in the shadow root of the elements and cannot be changed. option sadly has only a few styles like color that can be set...
One solution for this would be to hide the select element and control it via another element that can be styled (e.g. span) and JavaScript. That is not really pretty but many css frameworks already do so and if you absolutely have to make it look good (most of the times that is the case) that is your only option.
Here's a quick example of a custom built select box. As you can see, even putting images in the options is possible now. Hope this helps you.
Fontawesome is used for the caret. Documentation in the JS source code.
// Create a reference to the select box
const selectBox = document.getElementById("selected");
// Add an event listener to detect the click and make the options (in)visible
selectBox.addEventListener("click", function() {
// Add or remove class 'open'
document.getElementById("options").classList.toggle("open");
});
// Put all options in an array
const options = [...document.getElementsByClassName("option")];
// Add event listener for each option
options.map( option => option.addEventListener("click", function() {
// Create a reference to the input field
const myInput = document.getElementById("sel");
// Retrieve the text from the clicked option
const optionText = this.getElementsByTagName("span")[0].innerHTML;
// Put the text in the input field value
myInput.value = optionText;
// Put the text in the select box
selectBox.innerHTML = optionText;
// Close the select box
document.getElementById("options").classList.toggle("open")
}));
.container {
display: flex;
flex-wrap: wrap;
width: 25%;
}
#selected {
border: thin solid darkgray;
border-radius: 5px;
background: lightgray;
display: flex;
align-items: center;
cursor: pointer;
height: 1.5em;
margin-bottom: .2em;
padding-left: .5em;
min-width: 150px;
position: relative;
}
#selected:after {
font-family: FontAwesome;
content: "\f0d7";
margin-left: 1em;
position: absolute;
right: .5em;
}
#options {
display: none;
margin: 0;
padding: 0;
}
#options.open {
display: inline-block;
}
li {
display: flex;
justify-content: flex-start;
align-items: center;
cursor: pointer;
}
li>img {
margin-right: 1em;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form>
<input type="hidden" id="sel">
<div class="container">
<div id="selected">Select an option</div>
<ul id="options">
<li class="option"><img src="http://placehold.it/50/00ff00"><span>Option 1</span></li>
<li class="option"><img src="http://placehold.it/50/ff0000"><span>Option 2</span></li>
<li class="option"><img src="http://placehold.it/50/0000ff"><span>Option 3</span></li>
</ul>
</div>
</form>
You can correct the behavior with CSS
select {
float: left;
display: inline-block;
}
<select class="form-control">
<option value="0"></option>
<option value="1: 1" >Jr.</option>
<option value="2: 2">Sr.</option>
<option value="3: 3">I</option>
<option value="4: 4">II</option>
<option value="5: 5">III</option>
</select>
I am using select tag for mobile and tablet device.
On mobile device, it is working fine but on tablet, the select tag is not functioning properly.
My problem is : The option is getting disabled but the disabled options are not getting transparent(Faint in color).
Below is the code sample :
<select id="nativeDropDown" name='nativeDropDown' class="trend-filter native-dropdown-position" ng-click="openNativeDropDown()">
<option value='selectOption' selected data-ng-show="isIphone"></option>
<option value='lastDay' data-ng-disabled="chartType === 'boxplot'" data-bas-translate="LastDayFilter"></option>
<option value='last7Days' data-ng-disabled="chartType === 'boxplot'" data-bas-translate="Last7DaysFilter"></option>
<option value='last30Days' data-bas-translate="Last30DaysFilter"></option>
<option value='last12Months' data-bas-translate="Last12MonthsFilter"></option>
<option value='allTime' data-ng-disabled="!enableBoxplotForAllTime()" data-bas-translate="AllTime"></option>
<option value='customRange' data-bas-translate="CustomRangeFilter"></option></select>
.trend-filter
{
-webkit-appearance: none;
-moz-appearance: none;
text-indent: 1px;
text-overflow: '';
background-color: #EEEEEE;
overflow: hidden;
height: 35px;
margin-left: 5px;
/*opacity: 0.5;*/
width: 40px;
margin-left: -40px;
}
.native-dropdown-position {
position: absolute;
z-index: 99999;
}
Try using javascript validation instead.
And use the following code:
document.getElementById(" ").disabled = true; //Fill the white spaces with whatever you name it
I have created this styled-select, due to my requirements, and it is shown in the example below. Now if the options are longer than the frame, they won't be shown (which is what I require). I want some functionality in order to leave the cursor over any option and the whole text of that option is shown line an info-box.
I know that I should use hover but I could not remember the name of that functionality.
Working jsFiddle
$(function() {
$('.styled-select select').hide();
$("select#elem").val('0');
$('.styled-select div').each(function() {
var $container = $(this).closest('.styled-select');
$(this).html($container.find('select option:selected').text());
});
$('.styled-select div').click(function() {
var $container = $(this).closest('.styled-select');
var opLen = $container.find('select').children('option').length;
if (opLen < 5) {
$container.find('select').show().attr('size', opLen).focus();
} else {
$container.find('select').show().attr('size', 5).focus();
}
});
$('.styled-select select').click(function() {
var $container = $(this).closest('.styled-select');
var text = $container.find('select option:selected').text();
$container.find('div').html(text);
$container.find('select').hide();
});
$('.styled-select select').focusout(function() {
var $container = $(this).closest('.styled-select');
$container.find('select').hide();
});
});
.styled-select select {
position: absolute;
background: transparent;
padding-top: 5px;
font-size: 18px;
font-family: 'PT Sans', sans-serif;
color: black;
border: 0;
border-radius: 4;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance: none;
z-index: 1;
outline: none;
top: 42px;
box-shadow: 0 2px 2px 0 #C2C2C2;
}
.styled-select {
background: url('../img/campaignSelector.png') no-repeat right;
background-color: white;
height: 42px;
position: relative;
margin: 0 auto;
box-shadow: 0 2px 2px 0 #C2C2C2;
background-position: 97% 50%;
}
.styled-select option {
font-size: 18px;
background-color: white;
margin-left: 3px;
}
.styled-select option:hover {
//here
}
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<div class="styled-select" style="width: 301px; margin:5px 0 0 10px;">
<div id="userChannelDivId" style="font-size:18px; position: absolute; top: 8px; left: 5px; width: 300px; height: 42px; text-overflow: ellipsis; padding: 0 30px 0 0; white-space: nowrap; overflow: hidden;"></div>
<select id="userChannelId" name="userChannelId" style="width:100%">
<option value="">--- Select ---</option>
<option value="6040224291703">This is a text long enough to go outside frame 01</option>
<option value="6036780606903">This is a text long enough to go outside frame 02</option>
<option value="6038009946703">This is a text long enough to go outside frame 03</option>
<option value="6037196648903">This is a text long enough to go outside frame 04</option>
<option value="6040601588703">This is a text long enough to go outside frame 05</option>
<option value="6040080586303">This is a text long enough to go outside frame 06</option>
<option value="6040602117503">This is a text long enough to go outside frame 07</option>
<option value="6038006580703">This is a text long enough to go outside frame 08</option>
</select>
</div>
UPDATE:
I can not use title because these options are generated by getting data from database as follow:
<form:select id="campaignGoalId" path="campaignStructureList[${status1.index}].dataSourceModelList[${status2.index}].goalId" style="position: absolute;">
<form:option value="" label="--- Select ---" />
<form:options items="${campaignConfigurationModel.gaGoalList}" itemValue="goalId" itemLabel="goalIdName" />
</form:select>
Why don't you use the title attribute ?
<option title="This is a text long enough to go outside frame 08">
This is probably the easiest way.
I have a select element and am using the first option as the title of the select field. I am wondering if there is a way to gray out the text inside the select field when the first option is selected. Can this only be done in JS, or is there a CSS solution?
I have tried changing the style of the first option but that only changes the colour of the text when I activate the dropdown menu.
<select>
<option>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
Here is a more modern solution so it's not specific to the first option, but rather an invalid option and requires no JS to show only the title/placeholder option as grey whereas the rest appear normal.
select,
select option {
color: #000000;
}
select:invalid,
select option[value=""] {
color: #999999;
}
label {
display: block;
margin: 16px 0;
}
/*Added for browser compatibility*/
[hidden] {
display: none;
}
<label>
Invalid option cannot be selected and is hidden from the user in the dropdown.
<select required>
<option value="" selected disabled hidden>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
</label>
<label>
Invalid option cannot be selected, but is not hidden from the user in the dropdown.
<select required>
<option value="" selected disabled>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
</label>
<label>
Invalid option can be selected and is not hidden from the user in the dropdown.
<select required>
<option value="" selected>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
</label>
The :invalid selector on the select only works on an option
if the select box is required and the selected option's value is empty,
so you can style it as you would a text box's placeholder text.
Setting it to disabled prevents the user from selecting it in the select's options,
and setting it to hidden hides it from the select's options.
Here is my CodePen demo that explores additional select box styles and shows this one in action on a light background.
September 2017 edit
You should take a look at Tessa's answer below, since it's CSS only and much better now! This answer is almost 5 years old now, so things have changed a bit. I'm keeping the original answer just for reference.
Original answer
I am closer to what you need:
You need to gray the entire SELECT (so that when it's closed, it is gray), then "un-gray" all the OPTION's (put them black) and gray the first-child. Something like this:
CSS
select
{
color: #ccc;
}
option
{
color: #000;
}
option:first-child
{
color: #ccc;
}
EDIT
So the edited code is:
HTML
<select onchange="changeMe(this)">
<option selected disabled>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
Javascript
<script type="text/javascript">
function changeMe(sel)
{
sel.style.color = "#000";
}
</script>
I've update jsFiddle. You can check it here: http://jsfiddle.net/s5Xy2/5/
Notice that I've also changed the HTML part, because I think you want to use the "disabled" attribute (and because of that, you'll have to add the "selected" attribute also).
If you still want the pure CSS code, it's here: http://jsfiddle.net/s5Xy2/4/
Inspired from Fábio Silva's solution, a very cool solution using AngularJS:
select {
color: #ccc;
}
option {
color: #aaa;
}
option:first-child {
color: #ccc;
}
select.ng-dirty {
color: #aaa;
}
You can edit your code to my code :
<select id="drop">
<option>Please select your favourite fruit</option>
<option>Apple</option>
<option>Banana</option>
</select>
<style type="text/css">
#drop :first-child
{
color:gray;
}
</style>
This code set first item color gray .
i hope help you...
Here's my 2018 version that combines some of the other answers and a bit of my own js. There does not seem to be a solution that works w/o javascript if you want the first element gray when it is closed.
var grayout = document.getElementsByClassName('grayout');
var grayOutSelect = function() {
if ( this.value === "" ) {
this.classList.add('gray');
} else {
this.classList.remove('gray');
}
};
for (var i = 0; i < grayout.length; i++) {
grayout[i].addEventListener('change', grayOutSelect);
if ( grayout[i].value === "" ) {
grayout[i].classList.add('gray');
} else {
grayout[i].classList.remove('gray');
}
}
select {
color: #333;
}
select.gray {
color: #aaa;
}
/* Optional styles for when the select is open. Doesn't work on all browsers */
option {
color: black;
}
.grayout option:first-child {
color: gray;
}
/* Extra / just to make the demo look nice */
body {
background: #ddd;
padding: 30px;
font-size: 20px;
}
select {
margin: 0;
vertical-align: top;
padding: 5px 60px 5px 8px;
background-color: #fff;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/4/4b/Feather-arrows-chevron-down.svg');
background-position: 97% center;
background-position: right 8px center;
background-repeat: no-repeat;
background-size: 18px;
border: 2px solid #999;
border-radius: 3px;
-webkit-appearance: button;
-webkit-border-radius: 3px;
-webkit-padding-end: 30px;
-webkit-padding-start: 8px;
-webkit-user-select: none;
-moz-appearance: none;
font-size: inherit;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
transition: border 300ms;
}
<p>main example</p>
<p>
<select class="grayout">
<option value="">Please select your favourite fruit</option>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
</select>
</p>
<p>one of the real options is selected</p>
<p>
<select class="grayout">
<option value="">Please select your favourite computer</option>
<option value="apple" selected>Apple</option>
<option value="c64">Commodore 64</option>
<option value="gateway2000">Gateway 2000</option>
</select>
</p>
<p>the grayout style is not applied here</p>
<p>
<select>
<option value="">Please select your favourite insult</option>
<option value="jerk">Jerk</option>
<option value="ahole">A**hole</option>
<option value="shakespeare">Thou damned and luxurious mountain goat</option>
</select>
</p>