Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input placeholder using CSS only

I know there are lot's 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.

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

EDIT2: 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"

like image 368
Akki619 Avatar asked Aug 20 '13 07:08

Akki619


3 Answers

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');

like image 40
Hafenkranich Avatar answered Sep 22 '22 18:09

Hafenkranich


: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 element.

If you can't edit your HTML code manually, you're still able to that by using JavaScript:

document.getElementById("tb1").setAttribute("placeholder", "Enter your number");

Update

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 do. You'll not able to check the value of <input> by CSS. If you write something inside the <input>, after blur event, the generated placeholder will be displayed over the <input> again.

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

like image 83
Hashem Qolami Avatar answered Sep 23 '22 18:09

Hashem Qolami


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

like image 37
Tieson T. Avatar answered Sep 21 '22 18:09

Tieson T.