Is it possible to add placeholder in div tag

HtmlCss

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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)
}

http://jsfiddle.net/d3975/1/"><b>Demo</b></a>

Solution 2 - Html

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 https://stackoverflow.com/questions/17753819/if-editable-div-have-no-text-than-set-placeholder/17754905#17754905

Solution 3 - Html

This can also be achieved without the use of the contentEditable attribute, using plain CSS.

Using the :empty pseudo-class along with the ::before pseudo-element you can add a placeholder to an empty element.

The following example provides a fallback placeholder for div elements that do not have a data-placeholder attribute defined.

Example:

div:empty::before {
  color: grey;
}
div[data-placeholder]:not([data-placeholder=""]):empty::before {
  content: attr(data-placeholder);
}
div:empty::before {
  content: 'fallback placeholder';
}

<div data-placeholder='data- placeholder'></div>
<br>
<div></div>

Solution 4 - Html

Assuming what you really want to do is to have the user input some text and show him some placeholder in the input field, this is what you can use:

<input type="text" value="Enter the text" onfocus="if(this.value=='Enter the text'){this.value='';}" onblur="if(this.value==''){this.value='Enter the text';}" />

Of course you can wrap the input in a div.

<div><input [...] /></div>

You can see this in action here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser123View Question on Stackoverflow
Solution 1 - HtmlJoke_Sense10View Answer on Stackoverflow
Solution 2 - HtmlG-CyrillusView Answer on Stackoverflow
Solution 3 - Htmlmatt.View Answer on Stackoverflow
Solution 4 - HtmlSquareCatView Answer on Stackoverflow