With the current markup, I don't think this is possible only with CSS, but:
This is possible if it allows you to add a label element after input - for example:
<div> <input type="password" name="password" id="password" placeholder=" " /> <label for="password">Password</label> </div>
The trick here is to use a pseudo-class:
From specification :
Input elements can sometimes display placeholder text as a hint to the user about what to enter. See, for example, the placeholder attribute in [HTML5]. Under the alias : placeholder-shown corresponds to an input element that displays such placeholder text.
The idea here is the text " Password * " to simulate the placeholder text.
Placeholders always appear on input elements - even if they are focused - if no value is set on them.
Using the pseudo-class :placeholder-shown on input - we can determine when to display the label text.
This is the key selector:
input:placeholder-shown + label { display: block; }
This means: when the input pointer is displayed, the "placeholder" label is displayed. (otherwise we will hide it)
For this reason, we still need to set the non-stationary placeholder attribute on the input (a simple space is enough)
div { position: relative; } label { position: absolute; height: 20px; left: 5px; top: 0; bottom: 0; margin: auto; display: none; pointer-events: none; color: #757575; font-size: 16px; font-family: system-ui; } label:after { content: " *"; color: red; } input:placeholder-shown + label { display: block; } input { width: 200px; border: none; border-bottom: solid 1px #8d97a0; padding: 5px; border-radius: 0; background: #fff; box-shadow: none; height: 20px; font-size: 16px; margin: 5px 0; }
<div> <input type="password" name="name" id="name" placeholder=" " /> <label for="name">Name</label> </div> <div> <input type="password" name="name" id="name" placeholder="Email" /> </div> <div> <input type="password" name="name" id="name" placeholder="Phone" /> </div> <div> <input type="password" name="password" id="password" placeholder=" " /> <label for="name">Password</label> </div>
Danield
source share