I would like to display some text to the user when my div
element is empty.
I have tried adding a placeholder attribute to my div
but the text is not being displayed.
<div placeholder="Enter the text"> </div>
How can I display a message when my div
element is empty?
Placeholders are supported only by html5 elements explicitly created for text input such as input or textarea , so to make a DIV with a placeholder, we have to start writing some code!
CSS ::placeholder SelectorThe ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text. The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.
If you want to set a hint for text area or input field, then use the HTML placeholder attribute. The hint is the expected value, which gets displayed before the user enters a value, for example, name, details, etc.
There are lot of options using javascript, but if you want to do it in css.Try this:
html:
<div contentEditable=true data-text="Enter text here"></div>
css:
[contentEditable=true]:empty:not(:focus):before{ content:attr(data-text) }
Demo
i have already answered to such a question here with this test : http://codepen.io/gcyrillus/pen/hzLIE
div:empty:before { content:attr(data-placeholder); color:gray }
see If editable div have no text than set placeholder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With