Delay HTML5 :invalid pseudo-class until the first event

前端 未结 13 2068
旧巷少年郎
旧巷少年郎 2020-11-30 04:35

I recently discovered that the :invalid pseudo-class applies to required form elements as soon as the page loads. For example, if you have this cod

13条回答
  •  一整个雨季
    2020-11-30 05:04

    I can't comment, but to go with @Carl's very useful answer regarding using :not(:placeholder-shown). As another comment mentioned, this will still show the invalid state if you have NO placeholder (as some form designs call for).

    To solve this, simply add an empty placeholder like so

    
    

    Then your CSS, something like

    :not(:placeholder-shown):invalid{ background-color: #ff000038; }
    

    Worked for me!

提交回复
热议问题