Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply multiple styles to a single placeholder in an input element

I've got a basic input element on a form:

<input type="text" name="where" placeholder="Location or Place">

But I want to style the placeholder inline with the design below:

Currently I've got the following styles:

::-webkit-input-placeholder, ::-moz-placeholder, :-ms-input-placeholder, input:-moz-placeholder {
    color: white;
}

Obviously this doesn't handle the light blue 'or' text. I'd love to do this with CSS3 where possible. It it possible to style this using just CSS?

like image 629
Aidan Ewen Avatar asked Apr 11 '14 08:04

Aidan Ewen


People also ask

How do you assign a placeholder style?

Use the ::placeholder pseudo-element to style your placeholder text in an <input> or <textarea> form element. Most modern browsers support this, but for older browsers, vendor prefixes will be required.

How can I change the color of placeholder of particular input?

In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector.

What selectors are used to style placeholders?

The ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text.

What is the :: placeholder pseudo-element used for?

The ::placeholder pseudo-element represents placeholder text in an input field: text that represents the input and provides a hint to the user on how to fill out the form. For example, a date-input field might have the placeholder text YYYY-MM-DD to clarify that numeric dates are to be entered in year-month-day order.


1 Answers

The only solution i see is to avoid the use of placeholder and replace its behaviour with javascript. The old-style way!

enter image description here

See this demo

HTML

<div class="location">
    <span class="holder">Location <span class="blue">or</span> Place</span>
  <input id="input" size="18" type="text" />&nbsp;
</div>

Javascript

$(function() {
    $("span.holder + input").keyup(function() {
        if($(this).val().length) {
            $(this).prev('span.holder').hide();
        } else {
            $(this).prev('span.holder').show();
        }
    });
    $("span.holder").click(function() {
        $(this).next().focus();
    });
});

CSS

div.location > span.holder {
position: absolute;
margin: 5px 8px;
color: #ddd;
cursor: auto;
font-family: Helvetica;
font-size: 11pt;
z-index: 1;
}

div.location > span.holder > span.blue{
    color: #819FF7;
}

div input {
    padding:5px;
    font-size:11pt;
    background-color: #0B7DAB;
    color: white;
     border-radius:15px;
     -moz-border-radius:15px;
     -webkit-border-radius:15px;
    border: none;
}
like image 108
Getz Avatar answered Oct 05 '22 23:10

Getz