How do I style the HTML5 form validation error mes

2019-01-07 13:24发布

How do I style the HTML5 form validation error messages with CSS?

5条回答
干净又极端
2楼-- · 2019-01-07 13:31

Use pseudo selectors, as easwee said. For example, to make the form element green when valid, and red when invalid, do something like this:

:invalid {
    background: #ffffffdd;
         }
:valid{
      background:#ddffdd;
      }

If you need a full reference of these, head to Mozilla's reference.

P.S. Sorry if I'm doing these answers wrong, I'm new to this community.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-07 13:35

A required field will be invalid until the correct input is entered. A field that isn't required but has validation, such as a URL field, will be valid until text is entered.

 input:invalid { 
border:solid red;
}

for more info http://msdn.microsoft.com/en-us/library/ie/hh772367(v=vs.85).aspx

查看更多
混吃等死
4楼-- · 2019-01-07 13:36

For Google Chrome, it is not possible to style validation messages any more: https://code.google.com/p/chromium/issues/detail?id=259050

查看更多
男人必须洒脱
5楼-- · 2019-01-07 13:40

Chrome provides a native look and feel for their validation error speach bubbles. The error bubble is made up of four containing elements that are not normative elements. These four elements are stylable via pseudo-elements that apply to separate sections of the bubble:

::-webkit-validation-bubble

::-webkit-validation-bubble-arrow-clipper 

::-webkit-validation-bubble-arrow 

::-webkit-validation-bubble-message 

Update: This method is obsolete.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-07 13:54

Currently, there is no way to style those little validation tooltips. The only other option, which is what I have chosen to do, is to disable the browser validation all together for now and rely on my own client-side validation scripts.

According to this article: http://blog.oldworld.fr/index.php?post/2010/11/17/HTML5-Forms-Validation-in-Firefox-4

"The simplest way to opt out is to add the novalidate attribute to your <form>. You can also set formnovalidate on your submit controls."

查看更多
登录 后发表回答