Delay HTML5 :invalid pseudo-class until the first event

前端 未结 13 2081
旧巷少年郎
旧巷少年郎 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 04:59

    These answers are out of date. Now you can do this by checking for a placeholder pseudo-class with CSS.

    input:not(:placeholder-shown):invalid {
        background-color: salmon;
    }
    form:invalid button {
        background-color: salmon;
        pointer-events: none;
    }

    It starts with a normal background and turns pink as you enter you incomplete email address into it.

提交回复
热议问题