How to change the color of the tooltip text in a placeholder using CSS styles? Add CSS style for the placeholder Styling: change the color of the placeholder CSS

Placeholders, those frequently grayed out text elements inside an input, can be a pain to style. Fortunately we"ve sourced a short but effective CSS solution to style your input"s placeholder text any color and opacity you wish. Read on for the code.

Changing placeholder text color

Let"s start with a simple input and some placeholder text, for this example we"ll use the word "search" but you can use anything you want. The basic HTML is below:

HTML

Input (and textarea) placeholder text defaults to a light gray color, however, we can change that with a few lines of CSS. Here we"ll color the input text red using an HTML color name, but any color method will suffice (HEX, RGB, HSL).

CSS
::-webkit-input-placeholder ( /* Chrome */ color: red; ) :-ms-input-placeholder ( /* IE 10+ */ color: red; ) ::-moz-placeholder ( /* Firefox 19 + */ color: red; opacity: 1; ) :-moz-placeholder ( /* Firefox 4 - 18 */ color: red; opacity: 1; )

Note that it"s important to include the different vendor prefixes in order to support as many browsers as possible. Only Firefox"s input placeholder text defaults to a slight transparency so its unnecessary to set the opacity property on IE or Chrome.

Changing placeholder focus text color

Alright, we"ve successfully changed the color of the placeholder text to red, but it would be nice if something happened when a user clicks inside our input. Using the same vendor prefixed CSS properties, we can alter the opacity of the input placeholder text on focus.

CSS
input ( outline: none; padding: 12px; border-radius: 3px; border: 1px solid black; ) ::-webkit-input-placeholder ( /* Chrome */ color: red; transition: opacity 250ms ease-in-out ; ) :focus::-webkit-input-placeholder ( opacity: 0.5; ) :-ms-input-placeholder ( /* IE 10+ */ color: red; transition: opacity 250ms ease-in-out; ) :focus :-ms-input-placeholder ( opacity: 0.5; ) ::-moz-placeholder ( /* Firefox 19+ */ color: red; opacity: 1; transition: opacity 250ms ease-in-out; ) :focus:: -moz-placeholder ( opacity: 0.5; ) :-moz-placeholder ( /* Firefox 4 - 18 */ color: red; opacity: 1; transition: opacity 250ms ease-in-out; ) :focus:-moz-placeholder (opacity: 0.5; )

In the example above we"ve thrown in a few basic styles on the input itself, and added a transition on the opacity to make the experience just a little nicer. Check out the demo and experiment with other CSS properties and transitions.

Placeholder is an element of an input field in which a tooltip can be placed. When the user begins to enter data, the supporting text disappears so as not to interfere. Each browser has its own opinion on how this element should be displayed, and sometimes default styles break the entire design. To manage them, you need to use a special CSS placeholder rule.

Where is the placeholder?

The problem is that the input field's tooltip is firmly hidden in the shadow DOM and is not that easy to get to. For this, a special non-standard ::placeholder is used. With its help you can manage the properties of the tooltip.

Styling a placeholder in CSS looks like this:

Input::placeholder ( color: red; opacity: 1; font-style: italic; )

Browser support

The CSS placeholder pseudo-element is handled well by all modern browsers, and to support older browsers you can use the following prefixes:

  • ::-webkit-input-placeholder - for webkit browsers (Safari, Chrome, Opera);
  • ::-moz-placeholder - for Firefox browsers above version 19;
  • :-moz-placeholder - for older Firefox;
  • :-ms-input-placeholder - for Internet Explorer above version 10.

As you can see, old Mozilla browsers, as well as IE, consider placeholder to be a CSS pseudo-class, not a pseudo-element. Let's not argue with them, we'll just take this aspect into account when styling the input field.

Styling options

You can set the following options for the placeholder pseudo-element in CSS:

  • background - the tooltip block group applies to the entire input field. You can set not only the color (background-color), but also the image (background-image).
  • text color - color;
  • transparency - opacity;
  • underlining, overlining or striking out - text-decoration;
  • case - text-transform;
  • internal indents - padding. Not supported by all browsers. As with inline elements, top and bottom padding is ignored.
  • font display - properties of the font group, line-height and various indents (text-indent, letter-spacing, word-spacing);
  • vertical alignment in a line - vertical-align;
  • trimming text when the container overflows - text-overflow.
.input1::placeholder ( background-image: linear-gradient(lime, blue); color: white; ) .input2::placeholder ( text-decoration: line-through; color: purple; font-weight: bold; ) . input3::placeholder ( font-size: 16px; letter-spacing: 10px; ) .input4::placeholder ( background: brown; color: white; text-overflow: ellipsis; )

In focus

By default, the tooltip disappears from the input field only if at least one character is entered into it. But placeholder allows you to implement the disappearance immediately when focusing on the field. To do this, you need to combine it with the pseudo-class:focus.

Input:focus::placeholder ( color: transparent; )

In some browsers, it is possible to animate changes in a number of placeholder properties using the transition statement.

Input::placeholder ( color: black; transition: color 1s; ) input:focus::placeholder ( color: white; )

IN Google browser Chrome tooltip color when focusing on such a field will smoothly change within one second.

The placeholder attribute is used to create tooltips inside empty input fields (tags And