Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background text in input type text

Tags:

html

css

People also ask

How do you put a background color in input field?

To add background color to the form input, use the background-color property.

How do you add a background to text in HTML?

How do you change text background color in HTML? You can change the background color of text in HTML by adding a background-color property to a paragraph (p) or heading (H1, H2, H3... ) element. Add this property either via inline CSS or on your website's CSS code.

How can I set text background color in HTML?

You can use CSS (Cascading Style Sheets) Style Tags to add a color background to your form input boxes. The INPUT tag is used to create input fields within a web page form. You can change the font, input text color, input text size and the background color of your INPUT box by using the STYLE attribute.

What is placeholder text in HTML?

Definition and Usage The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the input field before the user enters a value.


Here is how you can get a placeholder using HTML5:

<input id="textfield" name="textfield" type="text" placeholder="enter something" />

EDIT:

I no longer recommend hacking together your own polyfills as I showed below. You should use Modernizr to first detect whether a polyfill is needed in the first place, and then activate a polyfill library that fits your needs. There is a good selection of placeholder polyfills listed in the Modernizr wiki.

ORIGINAL (contd):

And here is a polyfill for compatibility:

<input id="textfield" name="textfield" type="text" value="enter something" onfocus="if (this.value == 'enter something') {this.value = '';}" onblur="if (this.value == '') {this.value = 'enter something';}">

http://jsfiddle.net/q3V4E/1/

A better shim approach is to run this script on page load, and put your placeholders in the data-placeholder attribute, so your markup looks like this:

<input id="textfield" name="textfield" type="text" data-placeholder="enter something">

and your js looks like this:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
    inputs[i].value = inputs[i].getAttribute('data-placeholder');
    inputs[i].addEventListener('focus', function() {
        if (this.value == this.getAttribute('data-placeholder')) {
            this.value = '';
        }
    });
    inputs[i].addEventListener('blur', function() {
        if (this.value == '') {
            this.value = this.getAttribute('data-placeholder');
        }
    });
}

http://jsfiddle.net/q3V4E/4/


HTML5 provides a placeholder attribute that addresses this particular issue. See this link for more information (JSFiddle)

<input type="text" placeholder="insert your name" />

You can always provide a non-HTML5 javascript fallback, like the one explained here, or in None jquery placeholder fallback? if you aren't using JQuery.


I think you are looking for HTML5 form field placeholder... :)

http://webdesignerwall.com/tutorials/cross-browser-html5-placeholder-text


The jquery version of the non-html 5 accepted answer:

    var inputs = $("input[type='text']", context);
    $.each(inputs, function (i, obj) {
        $(obj).val($(obj).attr('data-placeholder'));
        $(obj).on('focus', function () {
            if ($(this).val() == $(this).attr('data-placeholder')) {
                $(this).val('');
            }
        });
        $(obj).on('blur', function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr('data-placeholder'));
            }
        });
    });

Changing the color of the placeholder can be achieved with CSS:

::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  color: pink;
}
::-moz-placeholder { /* Firefox 19+ */
  color: pink;
}
:-ms-input-placeholder { /* IE 10+ */
  color: pink;
}
:-moz-placeholder { /* Firefox 18- */
  color: pink;
}

Source: https://css-tricks.com/almanac/selectors/p/placeholder/