Utilizing CSS to style the disabled selection in the absence of an optgroup

I am encountering an issue with a select component that has grouped options and a default option. The CSS styling is not being applied to the option without a group.

I would like to change the color of the hidden disabled option to match the placeholder. Is there a way to achieve this?

   <select required>
        <option hidden disabled selected> -- select -- </option>
        <optgroup label="one">
            <option > A </option>
            <option > B </option>
            <option > C </option>
            <option > D </option>
        </optgroup>
        <optgroup label="two">
            <option > E </option>
            <option > F </option>
            <option > G </option>
            <option > H </option>
        </optgroup>
    </select>

option[disabled]{
  color: red;
}
<select required>
    <option hidden disabled selected> -- select -- </option>
    <optgroup label="one">
        <option > A </option>
        <option > B </option>
        <option > C </option>
        <option > D </option>
    </optgroup>
    <optgroup label="two">
        <option > E </option>
        <option > F </option>
        <option > G </option>
        <option > H </option>
    </optgroup>
</select>


Updated

The desired font color change should only occur initially. Upon the drop-down being changed, the default color should be applied again.

The hidden attribute is being used to serve as the placeholder within the option. Since placeholders cannot be used, I am attempting to replicate the behavior with this method.

Utilizing first-child does not resolve the issue as the options array is dynamically generated, leading to potential changes in position.

Answer №1

The issue was resolved by incorporating select:required:invalid in the CSS and setting value="" for the default selected option.

Utilizing the :invalid pseudo selector is effective when a dropdown is designated as required and the default value is an empty string.

select:required:invalid {
  color: red;
}
optgroup, option {
  color: black;
}
<select required>
    <option hidden disabled selected value=""> -- select -- </option>
    <optgroup label="one">
        <option > A </option>
        <option > B </option>
        <option > C </option>
        <option > D </option>
    </optgroup>
    <optgroup label="two">
        <option > E </option>
        <option > F </option>
        <option > G </option>
        <option > H </option>
    </optgroup>
</select>

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

Designing a drop-down menu that appears visually above its parent

As I design a menu positioned at the bottom using position:absolute; bottom:0, I want the submenu items to align perfectly above their parent. Each li should appear above the ul itself. The trick to achieving this is by applying a negative margin. However ...

When the browser width is reduced, the text appears outside of the image

When pasting text into an image in this example, everything looks fine at first. However, as the browser width is reduced, the text starts slipping out of the picture. To make matters worse, unsightly line breaks are also added. Is there a way to prevent ...

Mastering the 'wing' corner effect using only CSS

Is there a way to achieve the corner effect shown in this image (top corners)? I'm not familiar with the name of this effect. If it is possible, how would you go about implementing it? Update: Some have suggested that this question is a duplicate. Ho ...

How can I make two lines equal in length by stretching them?

Looking to replicate the design shown in the image below using CSS: https://i.stack.imgur.com/cZiFT.png It's crucial for me that both lines are of equal length: https://i.stack.imgur.com/8phWR.png I attempted to recreate it with the following code ...

What could be causing PostgreSQL to remove HTML entities when using ts_headline()?

In the process of developing a prototype for a full-text search capability, I am working on returning the "headlines" of found documents in the search results. Here is an adapted example from the Postgres documentation: SELECT ts_headline('english&ap ...

What could be causing my HTML email to appear distorted when viewed in Gmail?

After spending the last 6 hours tackling this issue, I am still stuck. Despite my extensive googling, it seems that divs are supposed to work in emails post-2017. All the online HTML email testers I have used indicate that my email layout is perfectly fine ...

create a division in the organization of the identification numbers

Is there a way to always change pages when the id changes in a foreach loop to separate the printed pages? Take a look at this code snippet: var data = [ {Id: "552", valor: "50.00", Descricao: "Fraldas", }, {Id: "552", valor: "35.00", Descrica ...

Convert text to bold when checkbox is selected and alter color upon selecting a radio button

Having just started learning HTML, CSS, and PHP, I find myself in need of assistance. Despite extensive research online, I have hit a dead end and cannot seem to find any productive solutions. Any guidance or suggestions would be greatly appreciated! I am ...

Utilizing media queries to customize the appearance of a jQuery accordion widget

Hello, I'm struggling with implementing a jQuery accordion for mobile platforms that destroys itself on larger screens. While my code is mostly working, I've encountered two issues: The accordion only gets destroyed when the window is resized ...

Including an embedded iframe in an asp.net web form

I need to integrate an iframe into a web form by retrieving the URL from an API call to a third-party payment service. I am working with ASP.NET 4.5 and C# 6.0. The code for my web form includes implementing an iframe similar to that in an ASP.NET MVC cod ...

Styling for older versions of Internet Explorer (IE10 and earlier)

Could it be true that IE 10, 9, and others no longer support conditional statements? Is it also accurate to say that JQuery does not support the browser object above version 1.9? I am facing an issue with CSS rendering differently in Chrome and IE. A Goog ...

Adjust the jQuery resizable handlers on the fly

I am encountering an issue when attempting to change the handler on a resizable element. Initially, I used : $(line) .draggable({containment:"parent"}) .resizable({ containment:"parent", handles: "e, w" }); N ...

css problem with stacking order of child elements

Trying to arrange 3 HTML elements on the z-plane: .bank { width: 200px; height: 200px; background-color: grey; position: absolute; z-index: 100; transform: translateY(10%); } .card { width: 100px; height: 100px; background-color ...

Building vue js logic to manage state across multiple ul li elements

I have a situation where I have an unordered list with a static number of list items (menu elements) that are rendered on the server side. I need to add some state-based logic to each list item, such as changing the color of the active item and expanding ...

Having trouble getting items to align properly in Bootstrap

As a student, I am struggling with my development skills while attempting to create a personal website. I am encountering difficulty aligning items on my rows in bootstrap. Here is what I am trying to accomplish: 1. However, I am currently stuck at this po ...

The Youtube Subscribe Embed feature is causing my image to lose its corners

When I use the official embed code from Google Developers to embed my YouTube channel, it cuts off the corners of my image and leaves white corners. This doesn't look good on my website with a black background. <script src="https://apis.google ...

Ways to define css attributes for the "before" and "after" pseudo elements in the DOM

I'm looking to modify the color of the "border-left" property from its default red to a different color within the scope, such as blue. .hello:before { content:' '; display:inline-block; width: 22px; height: 25px; ...

Creating a sleek navigation bar and sliding feature within the header of your Ionic 2

I am looking to create a sliding header for my website where the gallery hides and the navbar moves to the top as I scroll down, similar to the gif provided. Any help or ideas on how to achieve this would be greatly appreciated. Thank you. https://i.sstat ...

What is the best way to retrieve the value that has been submitted in a Django form?

I am in the process of developing an online marketplace where users can submit bids through a form. The bid must be higher than both the listing price and any existing bids. I am seeking assistance in extracting the value submitted by the user to ensure it ...

Is there a more efficient method for showcasing model objects in Thymeleaf rather than utilizing a form?

I've been exploring Spring Boot, Thymeleaf, and frontend development in general. Currently, I have a frontend table that displays a list of objects from the backend, along with a form to allow users to view properties of each item in the list. Howeve ...