2

question. I want the input element here to be without any style when he is empty and not get style from the in-range selector, like he is "in-range"...

how can I do this? What I need do that the input will not get style here? Here is the code:

li {
    list-style: none;
    margin-bottom: 1em;
  }
 
  input {
    border: 1px solid black;
 }
 
  input:in-range {
    background-color: rgba(251, 255, 0, 0.815);
  }
 
  input:out-of-range {
    background-color: rgba(255, 0, 0, 0.25);
    border: 2px solid red;
  }
 
  input:in-range + label::after {
    content: 'okay.';
  }
 
  input:out-of-range + label::after {
    content: 'out of range!';
  }
<!DOCTYPE html>
<html>  
 
<head>
<meta charset="UTF8">
<title>Practice vsc</title>
<link rel="stylesheet" href="css/style.css">
</head>
 
 
<body>
 
  <ul>Values between 1 and 10 are valid.
    <li>
      <input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12">
      <label for="value1">Your value is </label>
    </li>
  </ul>  
 
</body>
 
</html>
DCR
  • 14,737
  • 12
  • 52
  • 115

2 Answers2

2

li {
    list-style: none;
    margin-bottom: 1em;
  }
 
  input {
    border: 1px solid black;
 }
 
  input:in-range {
    background-color: rgba(251, 255, 0, 0.815);
  }
 
  input:out-of-range {
    background-color: rgba(255, 0, 0, 0.25);
    border: 2px solid red;
  }

  input:placeholder-shown {
    background-color: white;
  }
 
  input:in-range + label::after {
    content: 'okay.';
  }
 
  input:out-of-range + label::after {
    content: 'out of range!';
  }
<!DOCTYPE html>
<html>  
 
<head>
<meta charset="UTF8">
<title>Practice vsc</title>
<link rel="stylesheet" href="css/style.css">
</head>
 
 
<body>
 
  <ul>Values between 1 and 10 are valid.
    <li>
      <input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12">
      <label for="value1">Your value is </label>
    </li>
  </ul>  
 
</body>
 
</html>

This uses the :placeholder-shown selector, as seen here.

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32
1

Here's how to do it with JS but the better answer is placeholder-shown

function check(){

  var input = document.getElementById('value1');
  
  if (input.value ==''){
    input.classList.add("nob"); 
  }else{
    input.classList.remove("nob"); 
  }
}
li {
    list-style: none;
    margin-bottom: 1em;
  }
 
  input {
    border: 1px solid black;
 }
 
  input:in-range {
    background-color: rgba(251, 255, 0, 0.815);
  }
 
  input:out-of-range {
    background-color: rgba(255, 0, 0, 0.25);
    border: 2px solid red;
  }
 
  input:in-range + label::after {
    content: 'okay.';
  }
 
  input:out-of-range + label::after {
    content: 'out of range!';
  }
  
  input.nob{
    background-color: white;
  }
  
<!DOCTYPE html>
<html>  
 
<head>
<meta charset="UTF8">
<title>Practice vsc</title>
<link rel="stylesheet" href="css/style.css">
</head>
 
 
<body>
 
  <ul>Values between 1 and 10 are valid.
    <li>
      <input id="value1" class='nob' onkeyup="check()" name="value1" type="number" placeholder="1 to 10" min="1" max="10" >
      <label for="value1">Your value is </label>
    </li>
  </ul>  
 
</body>
 
</html>
DCR
  • 14,737
  • 12
  • 52
  • 115