I'm fixing the width of one of my dropdown boxes (yes I know there are cross-browser issues with doing this).
Is there a non-js way to cut off overflowing text and append ellipses? text-overflow:ellipsis doesn't work for <select> elements (at least in Chrome).
select, div {
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
<!--works for a div-->
<div>
A long option that gets cut off
</div>
<!--but not for a select-->
<select>
<option>One - A long option that gets cut off</option>
<option>Two - A long option that gets cut off</option>
</select>
Example here:
http://jsfiddle.net/t5eUe/
NOTE: As of July 2020, text-overflow: ellipsis works for <select> on Chrome
HTML is limited in what it specifies for form controls. That leaves room for operating system and browser makers to do what they think is appropriate on that platform (like the iPhone’s modal select which, when open, looks totally different from the traditional pop-up menu).
If it bugs you, you can use a customizable replacement, like Chosen, which looks distinct from the native select.
Or, file a bug against a major operating system or browser. For all we know, the way text is cut off in selects might be the result of a years-old oversight that everyone copied, and it might be time for a change.
If you are finding this question because you have a custom arrow on your select box and the text is going over your arrow, I found a solution that works in some browsers. Just add some padding, to the select, on the right side.
Before:
After:
CSS:
select {
padding:0 30px 0 10px !important;
-webkit-padding-end: 30px !important;
-webkit-padding-start: 10px !important;
}
iOS ignores the padding properties but uses the -webkit- properties instead.
http://jsfiddle.net/T7ST2/4/
The simplest solution might be to limit the number of characters in the HTML itself. Rails has a truncate(string, length) helper, and I'm certain that whichever backend you're using provides something similar.
Due to the cross-browser issues you're already familiar with regarding the width of select boxes, this seems to me to be the most straightforward and least error-prone option.
<select>
<option value="1">One</option>
<option value="100">One hund...</option>
<select>
You can use this:
select {
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
select option {
width:100px;
text-overflow:ellipsis;
overflow:hidden;
}
div {
border-style:solid;
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
Found this absolute hack that actually works quite well:
https://codepen.io/nikitahl/pen/vyZbwR
Not CSS only though.
The basic gist is to have a container on the dropdown, .select-container in this case. That container has it's ::before set up to display content based on its data-content attribute/dataset, along with all of the overflow:hidden; text-overflow: ellipsis; and sizing necessary to make the ellipsis work.
When the select changes, javascript assigns the value (or you could retrieve the text of the option out of the select.options list) to the dataset.content of the container, and voila!
Copying content of the codepen here:
var selectContainer = document.querySelector(".select-container");
var select = selectContainer.querySelector(".select");
select.value = "lingua latina non penis canina";
selectContainer.dataset.content = select.value;
function handleChange(e) {
selectContainer.dataset.content = e.currentTarget.value;
console.log(select.value);
}
select.addEventListener("change", handleChange);
span {
margin: 0 10px 0 0;
}
.select-container {
position: relative;
display: inline-block;
}
.select-container::before {
content: attr(data-content);
position: absolute;
top: 0;
right: 10px;
bottom: 0;
left: 0;
padding: 7px;
font: 11px Arial, sans-serif;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
text-transform: capitalize;
pointer-events: none;
}
.select {
width: 80px;
padding: 5px;
appearance: none;
background: transparent url("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png") no-repeat calc(~"100% - 5px") 7px;
background-size: 10px 10px;
color: transparent;
}
.regular {
display: inline-block;
margin: 10px 0 0;
.select {
color: #000;
}
}
<span>Hack:</span><div class="select-container" data-content="">
<select class="select" id="words">
<option value="lingua latina non penis canina">Lingua latina non penis canina</option>
<option value="lorem">Lorem</option>
<option value="ipsum">Ipsum</option>
<option value="dolor">Dolor</option>
<option value="sit">Sit</option>
<option value="amet">Amet</option>
<option value="lingua">Lingua</option>
<option value="latina">Latina</option>
<option value="non">Non</option>
<option value="penis">Penis</option>
<option value="canina">Canina</option>
</select>
</div>
<br />
<span>Regular:</span>
<div class="regular">
<select style="width: 80px;">
<option value="lingua latina non penis canina">Lingua latina non penis canina</option>
<option value="lorem">Lorem</option>
<option value="ipsum">Ipsum</option>
<option value="dolor">Dolor</option>
<option value="sit">Sit</option>
<option value="amet">Amet</option>
<option value="lingua">Lingua</option>
<option value="latina">Latina</option>
<option value="non">Non</option>
<option value="penis">Penis</option>
<option value="canina">Canina</option>
</select>
</div>
** HTML ex. **
<select id="selectDropdownID">
<option>One - A long option that gets cut off</option>
<option>Two - A long option that gets cut off</option>
</select>
CSS file
.selectDD {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
JS file
$(document).ready(function () {
$("#selectDropdownID").next().children().eq(0).addClass("selectDD");
});
quirksmode has a good description of the 'text-overflow' property, but you may need to apply some additional properties like 'white-space: nowrap'
Whilst I'm not 100% how this will behave in a select object, it could be worth trying this first:
http://www.quirksmode.org/css/textoverflow.html
I used this approach in a recent project and I was pretty happy with the result:
.select-wrapper {
position: relative;
&::after {
position: absolute;
top: 0;
right: 0;
width: 100px;
height: 100%;
content: "";
background: linear-gradient(to right, transparent, white);
pointer-events: none;
}
}
Basically, wrap the select in a div and insert a pseudo element to overlay the end of the text to create the appearance that the text fades out.
You can use this jQuery function instead of plus Bootstrap tooltip
function DDLSToolTipping(ddlsArray) {
$(ddlsArray).each(function (index, ddl) {
DDLToolTipping(ddl)
});
}
function DDLToolTipping(ddlID, maxLength, allowDots) {
if (maxLength == null) { maxLength = 12 }
if (allowDots == null) { allowDots = true }
var selectedOption = $(ddlID).find('option:selected').text();
if (selectedOption.length > maxLength) {
$(ddlID).attr('data-toggle', "tooltip")
.attr('title', selectedOption);
if (allowDots) {
$(ddlID).prev('sup').remove();
$(ddlID).before(
"<sup style='font-size: 9.5pt;position: relative;top: -1px;left: -17px;z-index: 1000;background-color: #f7f7f7;border-radius: 229px;font-weight: bold;color: #666;'>...</sup>"
)
}
}
else if ($(ddlID).attr('title') != null) {
$(ddlID).removeAttr('data-toggle')
.removeAttr('title');
}
}
Related
I'm fixing the width of one of my dropdown boxes (yes I know there are cross-browser issues with doing this).
Is there a non-js way to cut off overflowing text and append ellipses? text-overflow:ellipsis doesn't work for <select> elements (at least in Chrome).
select, div {
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
<!--works for a div-->
<div>
A long option that gets cut off
</div>
<!--but not for a select-->
<select>
<option>One - A long option that gets cut off</option>
<option>Two - A long option that gets cut off</option>
</select>
Example here:
http://jsfiddle.net/t5eUe/
NOTE: As of July 2020, text-overflow: ellipsis works for <select> on Chrome
HTML is limited in what it specifies for form controls. That leaves room for operating system and browser makers to do what they think is appropriate on that platform (like the iPhone’s modal select which, when open, looks totally different from the traditional pop-up menu).
If it bugs you, you can use a customizable replacement, like Chosen, which looks distinct from the native select.
Or, file a bug against a major operating system or browser. For all we know, the way text is cut off in selects might be the result of a years-old oversight that everyone copied, and it might be time for a change.
If you are finding this question because you have a custom arrow on your select box and the text is going over your arrow, I found a solution that works in some browsers. Just add some padding, to the select, on the right side.
Before:
After:
CSS:
select {
padding:0 30px 0 10px !important;
-webkit-padding-end: 30px !important;
-webkit-padding-start: 10px !important;
}
iOS ignores the padding properties but uses the -webkit- properties instead.
http://jsfiddle.net/T7ST2/4/
The simplest solution might be to limit the number of characters in the HTML itself. Rails has a truncate(string, length) helper, and I'm certain that whichever backend you're using provides something similar.
Due to the cross-browser issues you're already familiar with regarding the width of select boxes, this seems to me to be the most straightforward and least error-prone option.
<select>
<option value="1">One</option>
<option value="100">One hund...</option>
<select>
You can use this:
select {
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
select option {
width:100px;
text-overflow:ellipsis;
overflow:hidden;
}
div {
border-style:solid;
width:100px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
Found this absolute hack that actually works quite well:
https://codepen.io/nikitahl/pen/vyZbwR
Not CSS only though.
The basic gist is to have a container on the dropdown, .select-container in this case. That container has it's ::before set up to display content based on its data-content attribute/dataset, along with all of the overflow:hidden; text-overflow: ellipsis; and sizing necessary to make the ellipsis work.
When the select changes, javascript assigns the value (or you could retrieve the text of the option out of the select.options list) to the dataset.content of the container, and voila!
Copying content of the codepen here:
var selectContainer = document.querySelector(".select-container");
var select = selectContainer.querySelector(".select");
select.value = "lingua latina non penis canina";
selectContainer.dataset.content = select.value;
function handleChange(e) {
selectContainer.dataset.content = e.currentTarget.value;
console.log(select.value);
}
select.addEventListener("change", handleChange);
span {
margin: 0 10px 0 0;
}
.select-container {
position: relative;
display: inline-block;
}
.select-container::before {
content: attr(data-content);
position: absolute;
top: 0;
right: 10px;
bottom: 0;
left: 0;
padding: 7px;
font: 11px Arial, sans-serif;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
text-transform: capitalize;
pointer-events: none;
}
.select {
width: 80px;
padding: 5px;
appearance: none;
background: transparent url("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png") no-repeat calc(~"100% - 5px") 7px;
background-size: 10px 10px;
color: transparent;
}
.regular {
display: inline-block;
margin: 10px 0 0;
.select {
color: #000;
}
}
<span>Hack:</span><div class="select-container" data-content="">
<select class="select" id="words">
<option value="lingua latina non penis canina">Lingua latina non penis canina</option>
<option value="lorem">Lorem</option>
<option value="ipsum">Ipsum</option>
<option value="dolor">Dolor</option>
<option value="sit">Sit</option>
<option value="amet">Amet</option>
<option value="lingua">Lingua</option>
<option value="latina">Latina</option>
<option value="non">Non</option>
<option value="penis">Penis</option>
<option value="canina">Canina</option>
</select>
</div>
<br />
<span>Regular:</span>
<div class="regular">
<select style="width: 80px;">
<option value="lingua latina non penis canina">Lingua latina non penis canina</option>
<option value="lorem">Lorem</option>
<option value="ipsum">Ipsum</option>
<option value="dolor">Dolor</option>
<option value="sit">Sit</option>
<option value="amet">Amet</option>
<option value="lingua">Lingua</option>
<option value="latina">Latina</option>
<option value="non">Non</option>
<option value="penis">Penis</option>
<option value="canina">Canina</option>
</select>
</div>
** HTML ex. **
<select id="selectDropdownID">
<option>One - A long option that gets cut off</option>
<option>Two - A long option that gets cut off</option>
</select>
CSS file
.selectDD {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
JS file
$(document).ready(function () {
$("#selectDropdownID").next().children().eq(0).addClass("selectDD");
});
quirksmode has a good description of the 'text-overflow' property, but you may need to apply some additional properties like 'white-space: nowrap'
Whilst I'm not 100% how this will behave in a select object, it could be worth trying this first:
http://www.quirksmode.org/css/textoverflow.html
I used this approach in a recent project and I was pretty happy with the result:
.select-wrapper {
position: relative;
&::after {
position: absolute;
top: 0;
right: 0;
width: 100px;
height: 100%;
content: "";
background: linear-gradient(to right, transparent, white);
pointer-events: none;
}
}
Basically, wrap the select in a div and insert a pseudo element to overlay the end of the text to create the appearance that the text fades out.
You can use this jQuery function instead of plus Bootstrap tooltip
function DDLSToolTipping(ddlsArray) {
$(ddlsArray).each(function (index, ddl) {
DDLToolTipping(ddl)
});
}
function DDLToolTipping(ddlID, maxLength, allowDots) {
if (maxLength == null) { maxLength = 12 }
if (allowDots == null) { allowDots = true }
var selectedOption = $(ddlID).find('option:selected').text();
if (selectedOption.length > maxLength) {
$(ddlID).attr('data-toggle', "tooltip")
.attr('title', selectedOption);
if (allowDots) {
$(ddlID).prev('sup').remove();
$(ddlID).before(
"<sup style='font-size: 9.5pt;position: relative;top: -1px;left: -17px;z-index: 1000;background-color: #f7f7f7;border-radius: 229px;font-weight: bold;color: #666;'>...</sup>"
)
}
}
else if ($(ddlID).attr('title') != null) {
$(ddlID).removeAttr('data-toggle')
.removeAttr('title');
}
}
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 made a simple website that reproduces the bug in IE11.
When I hover on the red tab, it shows the blue container. In the blue container, there is a dropdown. If I then click the dropdown and hover on the items, the blue container disappears but the dropdown menu still shows. I tried in chrome, and this bug does not happen there, just IE it seems. I want the blue container to still show while I even go on the dropdown and hover on those things.
Does anyone know how to fix this?
Thanks
html,
body {
margin: 0;
padding: 0;
}
.tab {
background-color: red;
display: inline-block;
}
.hovermenu {
display: none;
width: 100%;
height: 50vh;
background-color: cyan;
position: -ms-page;
position: fixed;
}
.container {
display: inline-block;
}
.container:hover .hovermenu {
display: block;
}
<div class="container">
<div class="tab">TAB</div>
<div class="hovermenu">
<select>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
<option value="5">E</option>
</select>
</div>
</div>
I found out how to fix it a quick and dirty way for IE.
$("select").bind('focus', {}, function (event) {
$(this).closest(".hovermenu").css('display', 'block');
}).bind('blur', {}, function (event) {
$(this).closest(".hovermenu").css('display', '');
});
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>