CSS styles defined later should have greater priority (if the selectors are have the same strength). For instance:
/* Let's make it red */
h1 {
color: red;
}
/* Well... actually blue */
h1 {
color: blue;
}
<h1>Hello World</h1>
I was expecting the same thing to happen for input:valid
and input:invalid
, but it seems like input:valid
is stronger than input.is-valid
(a custom class defined later).
Here is an example:
$("button").click(function () {
$("input").addClass("is-invalid")
})
input:invalid {
border: 2px solid red;
}
input:valid {
border: 2px solid green;
}
.is-invalid {
/* Without adding !important, this isn't applied */
border: 2px solid orange !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" required>
<button>Run custom validation and declare it .is-invalid</button>
So, if we do not add !important
to the .is-invalid
class, input:valid
and input:invalid
will still have greater priority.
How should I address this? Is it a browser bug/feature?