Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Making Select Fields Styled when Option Value is Defined

This question extends on making input fields styled when they contain values

I have gone with David Thomas' suggestion of using CSS only to do this for text entry fields and it works really well. I have included his solution in my code for info.

Now, I would like to extend the same idea for select lists.

I have this code, which isn't working as expected:

.field-required
  {border: 1px solid #888;background-color: white;width: 75%;border-radius: 6px;padding: 4px 8px;color: #232856;text-align: left;font-size: 16px;height: 40px;}

.field-required:placeholder-shown
  {border: 2px solid #023a64;background-color: #d0e9f1;}

.list-required
  {border: 1px solid #888;background-color: white;width: 75%;border-radius: 6px;padding: 4px 8px;color: #232856;text-align: left;font-size: 16px;height: 40px;}

.list-required option[value="Select"]
  {border: 2px solid #023a64;background-color: #d0e9f1;width: 100%;border-radius: 6px;padding: 4px 8px;color: #232856;text-align: left;font-size: 16px;height: 40px;}
<form method="post" action="xxx.php" enctype="multipart/form-data" value="multipart/form-data">
  
  <label for="field">Text Field:</label><br>
  
  <input class="field-required" type="text" name="field" id="field" placeholder="Enter text &hellip;"><br><br>
  
  <label for="list">Select &hellip;</label><br>
  
  <select id="list" name="list" class="list-required">
    <option value="Select" selected>Select &hellip;</option>
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
  </select><br>

</form>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I've added required into the Select tag and I've added disabled attribute into the first option. in CSS I've added the select:required:invalid that is change the background into light blue and I also hide the first option

.field-required {
  border: 1px solid #888;
  background-color: white;
  width: 75%;
  border-radius: 6px;
  padding: 4px 8px;
  color: #232856;
  text-align: left;
  font-size: 16px;
  height: 40px;
}

.field-required:placeholder-shown {
  border: 2px solid #023a64;
  background-color: #d0e9f1;
}

.list-required {
  border: 1px solid #888;
  background-color: white;
  width: 75%;
  border-radius: 6px;
  padding: 4px 8px;
  color: #232856;
  text-align: left;
  font-size: 16px;
  height: 40px;
}

select:required:invalid {
  border: 2px solid #023a64;
  background-color: #d0e9f1;
  width: 100%;
  border-radius: 6px;
  padding: 4px 8px;
  color: #232856;
  text-align: left;
  font-size: 16px;
  height: 40px;
}

option[value=""][disabled] {
  display: none;
}

option {
  color: #232856;
  background-color: white;
}
<form method="post" action="xxx.php" enctype="multipart/form-data" value="multipart/form-data">

  <label for="field">Text Field:</label><br>

  <input class="field-required" type="text" name="field" id="field" placeholder="Enter text &hellip;"><br><br>

  <label for="list">Select &hellip;</label><br>

  <select id="list" name="list" class="list-required" required>
    <option value="" disabled selected>Select &hellip;</option>
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
  </select><br>

</form>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!