13

I know there are lots of questions regarding this query here but none of them provide the solution for me.

HTML

<input id="tb1" type="text" class="note" />

<br>
    
<p class="note1"> This is not done.</p>

CSS

p.note1:before{
    content: "Note:";
}

tb1.note:before{
    content: "Enter your number";
}

I am trying with above code and the variation as found on the web but none seems to work for input tag. It's working for p tag.


I can't add value attribute to input tag and manage CSS for the desired result. It's the limitation of the system.


Forget about my CSS, is there any way that placeholder text is possible without using placeholder attribute and just with plain CSS for input type="text"

9
  • 2
    What's wrong with using placeholder="text here" Commented Aug 20, 2013 at 7:39
  • can't add because of system limitations... Commented Aug 20, 2013 at 7:39
  • What is limiting you from using plain HTML? Commented Aug 20, 2013 at 7:40
  • :before only works on elements that can have child nodes, also inputs don't have a closing tag Commented Aug 20, 2013 at 7:41
  • html is rendered from existing framework controls and I do not have control to ask/change this. Commented Aug 20, 2013 at 7:42

6 Answers 6

16

From MDN:

::before creates a pseudo-element that is the first child of the element matched.

The selected element must be a container tag. An empty tag like <input> doesn't have any children elements.


If you want to achieve this by using CSS only, you need to have a container element wrapping your <input> (or come after it).

BUT It doesn't work correctly as placeholder does. You'll not be able to check the value of <input> using CSS. If you write something inside the <input>, after a blur event, the generated placeholder will be displayed again over the <input>.

HTML:

<label>
    <input id="tb1" type="text" class="note">
</label>

CSS:

label {
  position: relative;
}

label::after {
  content: 'Enter your number';
  position: absolute;
  left: 5px;
  top: 0;
  color: #bbb;
}

#tb1 {
  position: relative;
}

#tb1:focus {
  z-index: 10;
}

JSBin Demo


For JavaScript, if you can't edit your HTML code manually, you could still do that by using JavaScript:

document.getElementById("tb1").setAttribute("placeholder", "Enter your number");
Sign up to request clarification or add additional context in comments.

2 Comments

I like it, however, when tb1 loses the focus, the placeholder comes back even if you have put some text in it.
Javascript is honestly the most reliable (albeit hacky) method I've found, when I can't edit the HTML directly.
2

It doesn't work for the simple fact that this:

<input id="tb1" type="text" class="note"></input>

is not valid. <input /> elements are not containers. As the spec notes, endtags are forbidden (and essentially ignored by the browser): http://www.w3.org/TR/html401/interact/forms.html#h-17.4

1 Comment

I got this part then how it will be possible to render placeholder text on my input field with css only.
2

If you cant manipulate the html and use placeholder="". Use javascript to manipulate the placeholder. Every css approach is hack-isch anyway. E.g. with jQuery: $('#myFieldId').attr('placeholder', 'Search for Stuff');

Comments

1

I have found this method but not supported by all browsers:

#tb1.note:empty:before{
    content: "Enter your number";
}

Note: you have forgot to place an id selector # tb1.note

see this link

Comments

1

EDIT:

Try this for starters: (Note: you'll need some js to detect if text has been entered in the input)

Apart from this - I don't think this there is a css solution for placeholder text on an input element without using the placeholder attribute.

FIDDLE

Markup

<div class="container">
<input />
<div class="fakePlaceholder">Some placeholder text</div>
</div>

css

.container
{
    position: relative;
}
input
{
    background: transparent;
}
input:focus + .fakePlaceholder
{
    display: none;
}
.fakePlaceholder
{
    color:gray;
    position:absolute;
    top: 3px;
    left: 5px;
    z-index: -1;
}

You can't use pseudo elements on an input tag - or any other non-container elements for that matter

From the Pseudo-Elements tag info:

you cannot use them (pseudo elements) with replaced elements (see below) which do not have actual content. This is because the generated content resides within the element.
...

Replaced Elements

Any element whose appearance and/or dimensions are determined by some external resource is considered to be a replaced element. Some pseudo-elements cannot be applied to replaced elements because they have no "content" or get replaced with something (such as user interface controls). Replaced elements include images (<img>), inline frames (<iframe>), line breaks (<br>), horizontal rules (<hr>), plugins (<object>), form elements (<button>, <textarea>, <input>, and <select>), videos (<video>), audio sounds (<audio>), and canvases (<canvas>). Any other element is considered to be a non-replaced element.

Comments

1

Another way this can be accomplished, and have not really seen any others give it as an option, is to instead use an anchor as a container around your input and label, and handle the removal of the label via some color trickory, the #hashtag, and the css a:visited. (jsfiddle at the bottom)

Your HTML would look like this:

<a id="Trickory" href="#OnlyHappensOnce">
    <input type="text" value="" id="email1" class="inputfield_ui" />
    <label>Email address 1</label>
</a>

And your CSS, something like this:

html, body {margin:0px}
a#Trickory {color: #CCC;} /* Actual Label Color */
a#Trickory:visited {color: #FFF;} /* Fake "Turn Off" Label */

a#Trickory:visited input {border-color: rgb(238, 238, 238);} /* Make Sure We Dont Mess With The Border Of Our Input */

a#Trickory input:focus + label {display: none;} /* "Turn Off" Label On Focus */

a#Trickory input {
    width:95%;
    z-index:3;
    position:relative;
    background-color:transparent;
}
a#Trickory label {
    position:absolute;
    display:block;
    top:3px;
    left:4px;
    z-index:1;
}

You can see this working over at jsfiddle, note that this solution only allows the user to select the field once, before it removes the label for good. Maybe not the solution you want, but definitely an available solution out there that I have not seen others mention. If you want to experiment multiple times, just change your #hashtag to a new 'non-visited' tag.

http://jsfiddle.net/childerskc/M6R7K/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.