I have ghost text in textfields that disappear when you focus on them using HTML5's placeholder attribute:

<input type="text" name="email" placeholder="Enter email"/>

I want to use that same mechanism to have multiline placeholder text in a textarea, maybe something like this:

<textarea name="story" placeholder="Enter story\n next line\n more"></textarea>

But those \ns show up in the text and don't cause newlines... Is there a way to have a multiline placeholder?

UPDATE: The only way I got this to work was utilizing the jQuery Watermark plugin, which accepts HTML in the placeholder text:

$('.textarea_class').watermark('Enter story<br/> * newline', {fallback: false});
share|improve this question
    
IE seems to handle it properly. Firefox OTOH just ignores the newlines – ekkis Dec 10 '13 at 2:26
1  
stackoverflow.com/questions/7312623/… is a very similar question with good answers too. – Lloyd Dewolf Apr 15 '14 at 15:50

11 Answers 11

up vote 65 down vote accepted

The specification does not allow line feed or carriage return characters.

The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format. The attribute, if specified, must have a value that contains no U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters.

Apparently, the recommendation is to use a title attribute for anything longer.

For a longer hint or other advisory text, the title attribute is more appropriate.

share|improve this answer
32  
except that the title attribute does not behave the same way i.e. it does not show ghost content. in fact it would be perfectly appropriate for placeholders to support multiple lines for text areas since text areas are multi-line creatures. pity the spec doesn't allow it. I guess the hacks will have to do. sigh – ekkis Dec 10 '13 at 2:25
    
This does not work in Chrome 43 – fabsays May 31 '15 at 10:57

I find that if you use a lot of spaces, the browser will wrap it properly. Don't worry about using an exact number of spaces, just throw a lot in there, and the browser should properly wrap to the first non space character on the next line.

<textarea name="address" placeholder="1313 Mockingbird Ln         Saginaw, MI 45100"></textarea>
share|improve this answer
4  
This doesn't work for safari. – Andrei Cristian Prodan Dec 13 '12 at 8:44
2  
Yep multiline placeholders are not supported crossbrowser, have found the latest safari does support but is definitely not supported on IOS5 – Tom Jan 23 '13 at 14:57
    
doesn't work well cross browser – Holly Nov 19 '13 at 19:27
7  
that doesn't work for me in either IE nor Firefox Windows. it just inserts the spaces I asked for it to – ekkis Dec 10 '13 at 2:29
    
Chrome 37 is displaying placeholder text in a textarea without stripping the newlines. Does anyone know what the name of the 'feature' is so that I can a) look for it, and b) test for it? – Ben Jul 2 '14 at 6:35

On most browser, editing the placeholder in javascript do allows multiline placeholder. As it has been said, it's not compliant with the specification.

This example replace all multiline textarea's placeholder.

var textAreas = document.getElementsByTagName('textarea');

Array.prototype.forEach.call(textAreas, function(elem) {
    elem.placeholder = elem.placeholder.replace(/\\n/g, '\n');
});
<textarea class="textAreaMultiligne" 
          placeholder="Hello,\nThis is multiline example\n\nHave Fun"
          rows="5" cols="35"></textarea>

Expected result

Expected result


Update:

Based on comments it seems some browsers accept this hack and others don't.
This is the result of tests I ran (with browsertshots.org and browserstack.com)

  • Chrome: >= 35.0.1916.69
  • Firefox: >= 35.0 (results varies on platform)
  • IE: >= 10
  • KHTML based browsers: 4.8
  • Safari: No (tested = Safari 8.0.6 Mac OS X 10.8)
  • Opera: No (tested <= 15.0.1147.72)

Base on statistics, this means that it works on about 88.7% of currently (Oct 2015) used browsers.

share|improve this answer
    
You are right, you solve @at. problem – Huei Tan Sep 4 '14 at 7:31
1  
That jsfiddle example doesn't work at all... It's only multilined because the size of the textarea. – sebnukem Oct 21 '14 at 17:30
2  
There's a typo, but I can't edit because StackOverflow give me this "Edits must be at least 6 characters" error. Your class should be multiline not multiligne – Daniel Loureiro Feb 14 '15 at 4:05
    
@sebnukem: It works on most browsers that i had test out. And it's not a matter of textarea's size. Can you provide more information on the issue you had ? – Cyrbil May 9 '15 at 15:40
    
Doesn't work on safari it seems... - the \n disappears but everything is on a single line – Hackeron Jun 21 '15 at 12:20

There is actual a hack which makes it possible to add multiline placeholders in Webkit browsers, Chrome used to work but in more recent versions they removed it:


First add the first line of your placeholder to the html5 as usual

<textarea id="text1" placeholder="Line 1" rows="10"></textarea>

then add the rest of the line by css:

#text1::-webkit-input-placeholder::after {
    display:block;
    content:"Line 2\A Line 3";
}

If you want to keep your lines at one place you can try the following. The downside of this is that other browsers than chrome, safari, webkit-etc. don't even show the first line:

<textarea id="text2" placeholder="." rows="10"></textarea>​

then add the rest of the line by css:

#text2::-webkit-input-placeholder{
    color:transparent;
}

#text2::-webkit-input-placeholder::before {
    color:#666;
    content:"Line 1\A Line 2\A Line 3\A";
}

Demo Fiddle

It would be very great, if s.o. could get a similar demo working on Firefox.

share|improve this answer
    
thanks for providing the fiddle link. it makes it easy to verify behaviour in various browsers. on IE 10 both versions fail, as well as on FF 12 (Windows). pity that. Safari 6.1 works :( – ekkis Dec 10 '13 at 2:59
    
it haven't worked on Android :( – someone0 Jan 17 '14 at 17:28
    
Chrome for Android works; FF for Android does not. – Gottox Jan 19 '14 at 10:32
    
Safari IPAD not working – pir abdul wakeel Aug 24 '15 at 8:01
    
No longer works in Chrome - for a long time now, I'd assume. – Mike Rockett Feb 22 at 14:08

The html5 spec expressly rejects new lines in the place holder field. Versions of Webkit /will/ insert new lines when presented with line feeds in the placeholder, however this is incorrect behaviour and should not be relied upon.

I guess paragraphs aren't brief enough for w3 ;)

share|improve this answer
1  
Webkit's behavior is not incorrect since the specification does not say what must happen if CR/LF do exist. – Christian Dec 16 '11 at 11:26
1  
@Christian It does now, it says "User agents should present this hint to the user, after having stripped line breaks from it...". It says this about stripping line breaks: "When a user agent is to strip line breaks from a string, the user agent must remove any "LF" (U+000A) and "CR" (U+000D) characters from that string.". – Richard Turner May 28 '14 at 10:51

If your textarea have a static width you can use combination of non-breaking space and automatic textarea wrapping. Just replace spaces to nbsp for every line and make sure that two neighbour lines can't fit into one. In practice line length > cols/2.

This isn't the best way, but could be the only cross-browser solution.

<textarea class="textAreaMultiligne" 
          placeholder="Hello,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This&nbsp;is&nbsp;multiligne&nbsp;example Have&nbsp;Fun&nbsp;&nbsp;&nbsp;&nbsp;"
          rows="5" cols="35"></textarea>

share|improve this answer

If you're using AngularJS, you can simply use braces to place whatever you'd like in it: Here's a fiddle.

<textarea rows="6" cols="45" placeholder="{{'Address Line1\nAddress Line2\nCity State, Zip\nCountry'}}"></textarea>
share|improve this answer
    
Note: this also works for Angular2. – ARM Dec 22 '16 at 19:12
    
it does not work in Firefox – yaru Feb 17 at 15:19

You can try using CSS, it works for me. The attribute placeholder=" " is required here.

<textarea id="myID" placeholder=" "></textarea>
<style>
#myID::-webkit-input-placeholder::before {
    content: "1st line...\A2nd line...\A3rd line...";
}
</style>
share|improve this answer

Bootstrap + contenteditable + multiline placeholder

Demo: https://jsfiddle.net/39mptojs/4/

based on the @cyrbil and @daniel answer

Using Bootstrap, jQuery and https://github.com/gr2m/bootstrap-expandable-input to enable placeholder in contenteditable.

Using "placeholder replace" javascript and adding "white-space: pre" to css, multiline placeholder is shown.

Html:

<div class="form-group">
    <label for="exampleContenteditable">Example contenteditable</label>
    <div id="exampleContenteditable" contenteditable="true" placeholder="test\nmultiple line\nhere\n\nTested on Windows in Chrome 41, Firefox 36, IE 11, Safari 5.1.7 ...\nCredits StackOveflow: .placeholder.replace() trick, white-space:pre" class="form-control">
    </div>
</div>

Javascript:

$(document).ready(function() {
    $('div[contenteditable="true"]').each(function() {
        var s=$(this).attr('placeholder');
        if (s) {
            var s1=s.replace(/\\n/g, String.fromCharCode(10));
            $(this).attr('placeholder',s1);
        }
    });
});

Css:

.form-control[contenteditable="true"] {
    border:1px solid rgb(238, 238, 238);
    padding:3px 3px 3px 3px;
    white-space: pre !important;
    height:auto !important;
    min-height:38px;
}
.form-control[contenteditable="true"]:focus {
    border-color:#66afe9;
}
share|improve this answer

Watermark solution in the original post works great. Thanks for it. In case anyone needs it, here is an angular directive for it.

(function () {
  'use strict';

  angular.module('app')
    .directive('placeholder', function () {
      return {
        restrict: 'A',
        link:     function (scope, element, attributes) {
          if (element.prop('nodeName') === 'TEXTAREA') {
            var placeholderText = attributes.placeholder.trim();

            if (placeholderText.length) {
              // support for both '\n' symbol and an actual newline in the placeholder element
              var placeholderLines = Array.prototype.concat
                .apply([], placeholderText.split('\n').map(line => line.split('\\n')))
                .map(line => line.trim());

              if (placeholderLines.length > 1) {
                element.watermark(placeholderLines.join('<br>\n'));
              }
            }
          }
        }
      };
    });
}());
share|improve this answer

I do not think that is possible with html/css alone. Might be possible using JavaScript or some other kind of hack - extra spaces to push the text to the next line, etc.

share|improve this answer

protected by Community Feb 8 '14 at 17:55

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.