Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add placeholder in div tag

Tags:

html

css

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?

like image 211
user123 Avatar asked Nov 30 '13 12:11

user123


People also ask

Can I put a placeholder in a div?

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!

Can we add placeholder using CSS?

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.

How do you put a placeholder in HTML?

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.


2 Answers

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

like image 74
Joke_Sense10 Avatar answered Sep 24 '22 11:09

Joke_Sense10


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

like image 22
G-Cyrillus Avatar answered Sep 23 '22 11:09

G-Cyrillus