Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change border color of required input fields?

Tags:

css

I'm trying to change browser's default border style for required field which uses box-shadow in firefox and outline in chrome.

So, I'm doing like this:

*{
    box-shadow: none;
    outline: none;
}
input{
    border: 2px solid black;
}
input:invalid{
    border: 2px solid red;
}

But this shows red border already on load of the window. But I wanted it to be red only when it's invalid.

demo

How can I solve the problem?

like image 522
Bhojendra Rauniyar Avatar asked Aug 21 '14 11:08

Bhojendra Rauniyar


3 Answers

Since we only want to denote it invalid once it has focus. you could try code like:-

input:focus:required:invalid {border: 2px solid red;}
input:required:valid { border: 2px solid black; }

DEMO HERE

like image 147
Vivek Pratap Singh Avatar answered Nov 15 '22 04:11

Vivek Pratap Singh


Should remove required to make your input it is valid or else for required field you can do the following

input:invalid {
  background: hsla(0, 90%, 70%, 1);
}
input:valid {
  background: hsla(100, 90%, 70%, 1);
}
input:focus {
  background: hsla(100, 90%, 70%, 1);
}
input:required {
  border-color: #800000;
  border-width: 3px;
}

UPDATED DEMO

RESOURCE

like image 29
Benjamin Avatar answered Nov 15 '22 03:11

Benjamin


Without color:

input:required {
    border-color: #ccc !important;
    box-shadow: none !important;
    -webkit-box-shadow: none !important;
}
like image 32
Hernaldo Gonzalez Avatar answered Nov 15 '22 03:11

Hernaldo Gonzalez