Here is a cross-browser solution that does not use Javascript:
Live demo
The built-in elements of such an input do not support :before and :after . As it turned out, complicating the placeholder selector and its pseudo-classes do not fully support all browsers.
Thus, the workaround is to add a label located relative to the top of the input field, with the for attribute pointing to the input field. Thus, when the user clicks the label (false placeholder), the focus moves to the input field.
Replace your class="required" with the required="required" attribute. This gives you the opportunity to use the : invalid and : valid selectors, and also allows the browser to display a validation error when submitting the form with the required fields blank.
<form> <input type="text" id="name" name="name" required="required" /> <label for="name">Name</label> <br/> <input type="email" id="email" name="email" placeholder="Email" /> <br/> <input type="submit" /> </form> input { width: 160px; } input[type=submit] { width: auto; } input[required] + label { color: #999; font-family: Arial; font-size: .8em; position: relative; left: -166px; } input[required] + label:after { content:'*'; color: red; } input[required]:invalid + label { display: inline-block; } input[required]:valid + label{ display: none; }
Since no email is required, leave a local placeholder there and just crack the name.
I also changed your email address from type="text" to type="email" for the convenience of users on mobile devices.
source share