5

Is it possible to style individual parts of an input placeholder? An example:

Please enter here. Please also be reminded that you can only enter once.

user247702
  • 23,641
  • 15
  • 110
  • 157

2 Answers2

8

You can't do that with standard placeholder attribute. I will elaborate on another approach, making custom placeholder with some wrapper around input element.

HTML

<div class="placeholder-wrap">
    <input type="text" name="userName" />
    <span class="placeholder">
        This is a <b class="important">placeholder</b>
    </span>
</div>

CSS:

.placeholder-wrap {
    margin: 20px;
    display: inline-block;
    position: relative;
    background: #FFF;
}
.placeholder-wrap .placeholder {
    position: absolute;
    top: 50%;
    left: 5px;
    color: #888;
    margin-top: -.5em;
    line-height: 1em;
    z-index: 9;
    overflow: hidden;
    white-space: nowrap;
    width: 100%;
}
.placeholder-wrap input {
    background-color: transparent;
    border: 1px #999 solid;
    padding: 4px 6px;
    position: relative;
    z-index: 10;
}
.placeholder-wrap input:focus + .placeholder {
    display: none;
}

Yes, quite a few code, but gives you some flexibility with styling.

Demo: http://jsfiddle.net/dfsq/xD5Lq/

UPD. There is however a problem (thanks @AlexG for reporting). Once the value is entered and the input loses focus, placeholder appears again on top of the value. There are two ways to address this issue. The first one is pure CSS again using :invalid pseudo-class, which would also need required attribute on input:

.placeholder-wrap {
    display: inline-block;
    position: relative;
    background: #FFF;
    overflow: hidden;
}
.placeholder-wrap .placeholder {
    position: absolute;
    top: 50%;
    left: 5px;
    color: #888;
    margin-top: -.5em;
    line-height: 1em;
    z-index: 9;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
}
.placeholder-wrap input {
    background-color: transparent;
    border: 1px #999 solid;
    padding: 4px 6px;
    position: relative;
    z-index: 10;
}
.placeholder-wrap input:focus + .placeholder,
.placeholder-wrap input[required]:valid + .placeholder,
.placeholder-wrap input.not-empty + .placeholder {
    display: none;
}


input {width: 300px;}
.important {color: brown;}
<p>CSS fix</p>

<div class="placeholder-wrap">
    <input type="text" name="userName" required />
    <span class="placeholder">
        This is a <b class="important">placeholder</b> long text goes here
    </span>
</div>

<p>Javascript fix</p>

<div class="placeholder-wrap">
    <input type="text" name="userName" 
           onchange="this.className = this.value 
             ? this.className + ' not-empty'
             : this.className.replace(/\bnot-empty\b/, '')" 
    />
    <span class="placeholder">
        This is a <b class="important">placeholder</b> long text goes here
    </span>
</div>
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Came here from another question. Note on this one: Once you leave the input (loose focus), the placeholder will display again. – AlexG Jun 08 '16 at 10:27
  • @AlexG True. There are two solutions to this problem: the first will need a little javascript, the second is pure CSS. I will update the question. – dfsq Jun 08 '16 at 10:40
-1

Not possible if you are looking to use the standard input[type=text].

You could probably create your "input" via a div and setting the contenteditable to true.

<div contenteditable="true" id="input">
<strong>Please enter here.</strong> Please also be reminded that you can only enter once.</div>

Recreating it to look like an input field with css.

<style type="text/css">
#input {
border: 1px solid black;
width: 420px;
font-size: 12px;
font-family: Arial;
padding: 5px;
</style>

I've uploaded a demo here.

Edit: Someone mentioned about the placeholder text not being removed. A simple jQuery code fixes the problem.

$("#input").html("");
Rachelle
  • 16
  • 5
  • It doesn't work like placeholder. When one types into the field, then placeholder text should be removed. That's not happening in your code. – Mazzu Mar 26 '14 at 09:09
  • Oh I forgot about that point. OP could use jQuery to remove the text on click though. I'd probably edit my code. – Rachelle Mar 26 '14 at 09:11
  • Yup, you need to apply some jquery code for it. You may refer to link http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html for such scenario.. :) – Mazzu Mar 26 '14 at 09:12
  • @Mazzu yeap! I've edited my code :-) Hope that helps, although I was downvoted for my previous answer without the jQuery! – Rachelle Mar 26 '14 at 09:14
  • It don't works straight forwardly. You need to catch the element's "div#input" keypress or focus or blur event to apply $("#input").html(""); code. Then it will work :) – Mazzu Mar 26 '14 at 09:19
  • @Mazzu yeap that's correct! Just FYI for future users if they're into using this idea of "contenteditable". – Rachelle Mar 26 '14 at 09:21
  • That's ok. But atleast you should provide the complete scenario, so that it will be helpful for others. :) – Mazzu Mar 26 '14 at 09:24