Alter the color of the initially chosen option in the dropdown selection

I'm struggling to modify the color of the initial selection while keeping the remaining selection-options at their default black color.

Unfortunately, I haven't been successful in achieving this. Currently, all colors are #ccc. What I want is for the disabled first option to be #ccc and the rest to be in their default color.

Below is the HTML code:

<select class="form-control select form__blank">
  <option disabled selected value> - select - </option>
  <option>Full Furnished</option>
  <option>Semi Furnished</option>
  <option>UnFurnished</option>
</select>

CSS:

.form-control {
  border-radius: 25px;
  border: 1px solid #323a56;
  width: 100%;
  padding: 10px 15px;
  font-size: 14px;
}
.select {
  border-radius: 25px;
  border: 1px solid #323a56;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-position: right 50%;
  background-repeat: no-repeat;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAMCAYAAABSgIzaAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NDZFNDEwNjlGNzFEMTFFMkJEQ0VDRTM1N0RCMzMyMkIiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NDZFNDEwNkFGNzFEMTFFMkJEQ0VDRTM1N0RCMzMyMkIiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo0NkU0MTA2N0Y3MUQxMUUyQkRDRUNFMzU3REIzMzIyQiIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo0NkU0MTA2OEY3MUQxMUUyQkRDRUNFMzU3REIzMzIyQiIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PuGsgwQAAAA5SURBVHjaYvz//z8DOYCJgUxAf42MQIzTk0D/M+KzkRGPoQSdykiKJrBGpOhgJFYTWNEIiEeAAAMAzNENEOH+do8AAAAASUVORK5CYII=);
  height: inherit;
}
.form__blank:first-child {
  color: #ccc;
}

JSFIDDLE: https://jsfiddle.net/v9gasouf/

Hopefully, this explanation is clear.

Answer №1

The primary issue here is customizing options/select and it can be quite challenging due to the varying behavior of different browsers when it comes to displaying them.

You can refer to this StackOverflow thread for a detailed explanation and numerous potential workarounds.

For Chrome v.57.0.2987, you can utilize the CSS selector: select option:disabled which should help achieve the desired styling:

.form-control {
  border-radius: 25px;
  border: 1px solid #323a56;
  width: 100%;
  padding: 10px 15px;
  font-size: 14px;
}
.select {
  border-radius: 25px;
  border: 1px solid #323a56;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-position: right 50%;
  background-repeat: no-repeat;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAMCAYAAABSgIzaAAAAGXRFWHRTb2Z0d2FyZQ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C9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PuGsgwQAAAA5SURBVHjaYvz//z8DOYCJgUxAf42MQIzTk0D/M+KzkRGPoQSdykiKJrBGpOhgJFYTWNEIiEeAAAMAzNENEOH+do8AAAAASUVORK5CYII=);
  height: inherit;
}
.form__blank select option:disabled {
  color: #ccc;
}
<select class="form-control select form__blank">
  <option disabled selected value> - select - </option>
  <option>Full Furnished</option>
  <option>Semi Furnished</option>
  <option>UnFurnished</option>
</select>

Answer №2

Avoid using

.form__blank:first-child {
   color: #ccc;
}

Try using the following code instead:

.form__blank option:first-child {
   color: #ccc;
}

Answer №3

Choose the initial child of the selection rather than picking the select option.

option:first-child {
 color: #999;
}

Answer №4

Instead of applying styles to :first-child, try using the [disabled] property for styling.

select option:disabled{
  color:red;
}

For a practical demonstration, you can refer to the fiddle example linked below.

https://jsfiddle.net/achuakshuu/e97nqLuh/

Answer №5

Here is an example to try out:

<select id="selid" class="form-control select form__blank">
.....
</select>

JavaScript (using JQuery)

$('#selid').change(function () {
$(this).find('option').css('color', 'inherit');
$(this).find('option:selected').css('color', 'red');
}).trigger('change');

Alternatively,

$('#selid').change(function () {
$(this).css("color", $("option").css("color", 'inherit'));
$(this).css("color", $("option:selected").css("color", 'blue'));
}).trigger('change');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loop through a non-array or non-object / handling both arrays and non-arrays equally

Sometimes, I find myself needing to handle arrays and single objects in a similar manner. For instance, I may have an object property that can be either an array or just a string (like the scale property): [ { "name": "Experiment type14", "id": ...

Access the keys of a Flow object type within your codebase

Is it feasible to extract keys from a Flow object type definition within the application code (in other words, are Flow type definitions reflected in any way in the runtime code)? Scenario: type Props = { userID: string, size: number | PhotoSize, s ...

Is there a way to apply an event function after adding elements through append?

When I click the button, a div is appended inside the body. However, I am trying to make it so that when I click on this newly appended div, an alert message pops up. I have tried implementing this with a highlighted area, but have been unsuccessful. How c ...

Position Bootstrap buttons to the right side

I am looking to reposition the buttons in the header, shifting them from the default right side to the left side. Specifically, I want to move Abcd to the right side and have A1, A2, A3, A4, and A5 displayed on the left side. Here is my current code: < ...

What are the steps to integrating D3js into a WordPress website?

After installing Wordpress and the d3js plugin, I'm looking for the most effective way to upload data in order to create and display graphs on my website. Should I directly upload csv files? And if so, where should I store them? If I prefer storing ...

AngularJS restricts inputs to be read-only

I'm facing an issue with readonly inputs in AngularJS. I have a select element that changes the values of readonly inputs through a script. However, when I attempt to display these values using ng-model in a table as {{ng-model}}, they don't appe ...

The function is not operational while executing addEventListener

I'm encountering some bugs in my Angular 1.5 project with TypeScript. I'm trying to retrieve the scrollTop value from the .uc-card element. public container = document.querySelector(".uc-card"); In my $onInit, I have: public $onInit() { this ...

Error: The fetch request was unsuccessful [NextJS API]

I keep encountering an error - TypeError: fetch failed after waiting for 300 seconds while requesting an optimization server that requires a response time longer than 300 seconds. The request is being sent from the provided API in NextJS. Is there a way ...

How can one utilize XPath and the R language to selectively extract portions of a text string when the strings are varied and inconsistent?

Can xpath and R (not PHP) be used to extract just the city from a longer address string? Below is the relevant excerpt from a webpage: <table id="offices" cellspacing="8" width="700" height="100" border="0"> <tbody> <tr> <td valign ...

Building an interactive array with checkboxes and mapping in React JS: A step-by-step guide

I am currently working on mapping a list of formations and would like to implement a dynamic array that updates when a checkbox is selected in the onChange event. The goal is to keep my organization updated with the formations they are able to dispense. ...

Is there a way to decrease a field in a MongoDB database on a daily basis?

In the process of constructing an Angular2 application using MEAN stack architecture, I have a field called Remaining Days in my MongoDB database. I am interested in having this field automatically decrement by 1 each day. Do you know if this is possible ...

Using the jQuery click function to duplicate posts when checkboxes are checked

I am facing a challenge that seems quite simple. My goal is to create a checkbox that, when clicked, updates a field in a database to '1'. When unchecked, it should set the flag back to '0'. I have successfully set up the post side of t ...

What is the best way to include additional columns in a multiselect dropdown using jQuery select2?

I have implemented a multiselect dropdown feature using the jQuery select2 JavaScript library. The list items in this dropdown pertain to various medical drugs. I am looking to enhance this feature by adding a new column next to each selected drug, where ...

Differences between encoding URL variables in HREF and using JS window.location for onclick events

For some reason, this particular hyperlink is not functioning properly. I have a Javascript redirect (window.opener.location) where I pass several variables through the URL. The problem arises when these variables contain apostrophes. In PHP, I am using UR ...

Is there a way to execute a PHP script using Ajax without needing any return values?

Currently, I am attempting to execute a php script with ajax without needing any output or echos. Is there a method to achieve this? Below is the approach I have taken: $('button')[1].click(function () { $.ajax({ met ...

The functionality of jquery ujs seems to be impaired when I try to load partials or content remotely using remote calls or

Currently, I am using a piece of jQuery code to populate an element on the webpage with some content: var content = $("#note_"+note.id).html(); $("another_div").html(content); Although this successfully replaces the HTML of the other div, the issue is ...

Adjust image to fit inside a compact popup window - Implementing React.js and Bootstrap

Hello, I could really use some help with a problem I'm facing. I am currently working on my personal portfolio website using react.js and bootstrap. I've integrated the react popupbox package, but I noticed that on small screens, my picture is no ...

export module from the express framework

function affirm(){ console.log("yes") } Element={} Element=affirm Element.something="something" Element.nothing="nothing" DEPICTED IN WEB BROWSER: In the code snippet above, if you were to console.log(Element), it would display ...

How to achieve an endless cycle using Promise recursion in a NodeJS environment

I am planning to replace my blocking infinite while loop with promises. My run function is quite simple - it lights up an LED and then turns it off before moving on to the next one. Since Promises do not work inside while loops, I'm exploring how I c ...

Upon receiving the ajax request, the entire code page is called back

I'm having trouble saving data using a button in Laravel. When I use console.log, the entire code appears in the console and I don't understand why this is happening. Here is my input field and button: <form action="{{ url('sendcom& ...