How to target an invalid <select> element with the :invalid pseudo-class

One of the challenges I'm facing is with a <select>:

select:invalid {
  color: red;
}
<select required>
  <option disabled selected>- please choose -</option>
  <option value="1">A</option>
  <option value="2">B</option>
</select>

Despite that, the disabled select is not displaying in red as intended.

Any suggestions on how to address this issue?

I am focused on styling the select element rather than the options themselves.

Answer №1

Ensure that you provide a value="" for the initial option. Omitting the value="" attribute will result in the option's value being implicitly set to its text content, satisfying the required condition.

Example:

select:invalid { color: red; }
<select required>
  <option disabled value="" selected>- please choose -</option>
  <option value="1">A</option>
  <option value="2">B</option>
</select>

The CSS above will render the select element and all options in red until a selection is made.

If you prefer the options to always appear black, you can modify the CSS as follows:

select:invalid { color: red; }
select option { color: black; }
<select required>
  <option disabled value="" selected>- please choose -</option>
  <option value="1">A</option>
  <option value="2">B</option>
</select>

Answer №2

One recommendation is to utilize the option:disabled selector.

select option:disabled {
  color: red;
}
<select required>
  <option disabled selected>- please choose -</option>
  <option value="1">A</option>
  <option value="2">B</option>
</select>

Answer №3

.highlight_selection{
    background-color : blue;
}
.selection{
    background-color : yellow;
}
<select required 
    onchange="this.className=this.options[this.selectedIndex].className" 
    class="highlight_selection">
    <option value="0" class="highlight_selection">- make a selection -</option>
    <option value="1" class = "selection">Option A</option>
    <option value="2" class = "selection">Option B</option>
</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

Avoid automatic background resizing to match the full width of the header text

I've been trying to use CSS to style my <h1> element in a specific way, but it seems the default browser behavior is getting in the way: https://i.sstatic.net/oRR5N.png Despite my efforts, the browser continues to display it like this: https: ...

Scrolling problems with numerous bootstrap modals on a Webpage

I am faced with an issue on a page where I have set the property overflow: auto. This page features a hyperlink to open the first modal, and within the first modal, there is a link to the second modal. My steps are as follows: Open Page 1 Click on the l ...

What is the best way to add child elements to existing elements?

When it comes to creating elements with jQuery, most of us are familiar with the standard method: jQuery('<div/>', { id: 'foo', href: 'http://google.com', }).appendTo('#mySelector'); However, there ar ...

"Struggling to position the bottom navbar at the center in Bootstrap?

Can someone help me center the icons in my second navigation bar? I've tried aligning them left and right, and that works, but when I try to center them, it doesn't work at all. Did I miss something in the code or make a mistake? Any assistance w ...

Struggling to display the default value on a <select> element using the defaultValue prop in React

My objective is to create a dropdown menu that lists all the US state names. If there is a stateName in this.props (obtained from the redux store with values like NY, KY, or an empty string), I want the dropdown to display it by default. Otherwise, I want ...

My Python script using selenium isn't functioning properly - what could be the issue?

Here is a code snippet: from selenium import webdriver driver = webdriver.Chrome(executable\_path=**"C:\\\\browserdrivers\\\\chromedriver.exe") driver.get("https://www.aliexpress.com/item/1005 ...

Getting the most out of fonts with Webpack sass-loader

I recently set up a custom font in my project like this: $font-path: '~@/assets/fonts/'; @font-face { font-family: 'mainFont'; font-weight: 300; font-style: normal; src: url('#{$font-path}mainFont/mainFont.eot&apos ...

Unique logo loading exclusively on the landing page's navbar

I am currently developing a website using Bootstrap and Flask and have encountered an issue with the logo in the navbar. The problem is that the logo only displays on the landing (home) page and not on any other pages. The navbar code causing the issue is ...

Present user-generated incorrect HTML content without disrupting the webpage layout

My website offers users the ability to input and save text content, which can then be displayed as desired. Users have the freedom to utilize HTML tags for formatting purposes such as design, fonts, divs, tables, and more... However, when it comes to disp ...

Calculate the total sum of varying values extracted from both the entered text and selected

Currently, I am facing an issue with summing up the value of an input text dynamically changing with a radio button that also changes dynamically. While some aspects are working correctly, there is still something wrong as the sum does not update when expe ...

Utilizing an Ajax request to access an API and fetch JSON information. What is the best way to handle the returned

In the process of developing a Chrome application that utilizes my REST API to fetch basic user details in JSON format, I have successfully tested the connection and retrieval functionality within the Chrome developer tab preview. My query pertains to dyn ...

Attaching data to Vue model on the fly within a component

Currently, I am working on creating a straightforward form that will allow users to input various types of currency. Below is a link to a fiddle that demonstrates what I am aiming for, although it is not functioning as intended at the moment: https://jsfi ...

Printing Multiple Pages Using JavaScript and Cascading Style Sheets

Encountering difficulties displaying page numbers when printing multiple multipage reports Here is the provided HTML Format : <style type="text/css> body { counter-reset: report 1 page 0; } td.footer:after { counter-increment: page; content ...

Integrate HTML code from one file into another HTML file

I have a specific task in mind: I want to incorporate a header and footer from an external HTML file into another HTML file. Although there are numerous code snippets available here, I'm facing some challenges: Issue: I am unable to modify the exte ...

The column with a class of col-md-3 is not rendering properly

The design shown below was created in photoshop utilizing a 12 grid system: https://i.sstatic.net/MSGHc.jpg I am attempting to replicate this layout using bootstrap with class='col-md-3' for each input. However, when I do this, they appear next ...

CSS-LESS animation fails to function

I'm currently facing an issue with my script. In the section designated for avatars - images, I am attempting to change the border color upon hovering over the image, however, it doesn't seem to be working as expected. img { margin-right:15 ...

What is the best way to customize the style of a react.js component upon its creation?

Is there a way to set the style of a react.js component during its creation? Here is a snippet of my code (which I inherited and simplified for clarity) I want to be able to use my LogComponent to display different pages of a Log. However, in certain ins ...

The placement of a material-ui component at the beginning of the render method can impact the styling of the following components in the hierarchy

After creating some styled components, I noticed that they were not rendering correctly for me. However, when I transferred the code to a codesandbox environment (which had a material-ui button in the template), the components rendered perfectly fine. It w ...

The input group addon is not displaying properly in the Mozilla browser

I am presenting the following data in a table: <table class="neo-table"> <tr> <td>Day of final discharge home</td> <td>{{ form_widget(form.mHomeDischargeDay, {'id': 'M_Home_discharge_day', ' ...

Obtain the Xpath for search results on Google

Currently, I am developing a Python Selenium script to extract URL links for LinkedIn profiles from a Google search. However, I am facing challenges in refining my XPath query to retrieve only the search result links on Google. linkedin_urls = driver.find_ ...