Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda