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.
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
Let's start with an example for those who do not yet know what a placeholder is and how to use it:
When developing interfaces, you should take into account the behavior of placeholders. Placeholder text disappears as soon as typing begins. That is why you should not use them to transmit information about the input field itself (how many and what characters it should contain). For these purposes, it is best to use the tag
Stylization
The following rules are used to style placeholders:
::-webkit-input-placeholder ( color : blue ; ) ::-moz-placeholder ( color : blue ; ) /* Firefox 19+ */ :-moz-placeholder ( color : blue ; ) /* Firefox 18- * / :-ms-input-placeholder ( color : blue ; )Looks terrible, doesn't it? And there is a reason for this - until now, CSS rules for placeholders have not been standardized and the implementation is different in each browser. More information about all the necessary prefixes can be found on caniuse.
If you're using a CSS preprocessor, you'll likely be most comfortable writing a simple mixin to apply styles to placeholders. Example in Sass:
@mixin placeholder ( & : :- webkit-input-placeholder ( @content ; ) & : - moz-placeholder ( @content ; ) & : :- moz-placeholder ( @content ; ) & : - ms-input-placeholder ( @content ; ) )Depending on the context, a mixin can be used to apply styles either globally or on an individual element basis (try it out on SassMeister):
// Globally for each input field@include placeholder ( color : blue ; ) // For specific input fields.input ( @include placeholder ( color : green ; ) )Example
Blue text for placeholder (never do this):
.blue-text ::-webkit-input-placeholder ( color : #2e8ece ; ) .blue-text :-moz-placeholder ( color : #2e8ece ; ) .blue-text ::-moz-placeholder ( color : #2e8ece ; ) .blue-text :-ms-input-placeholder ( color : #2e8ece ; )
Supported Properties
Not every CSS property can be used with placeholders. Here full list supported properties:
- font and all related properties (font-size, font-family, etc.)
- background and all related properties (background-color, background-image, etc.)
- opacity
- text-indent
- text-overflow
- color
- text-transform
- line-height
- word-spacing
- letter-spacing
- text-decoration
- vertical-align
Animations
The animation ideas belong to the blog html5.by.
All of the following examples are written using the Sass preprocessor. Each one comes with a link to SassMeister where you can find the compiled CSS code.
Most likely, you will want to apply animations to placeholders when the input field has focus. All this is done quite simply. It is enough to just use the previously written placeholder mixin a few times:
.input ( @include placeholder ( // Styles for normal state) & :focus ( @include placeholder ( // Styles after the focus event } } }Change transparency
.input ( @include placeholder ( color : #aaa ; opacity : 1 ; transition : opacity 300ms ; ) & :focus ( @include placeholder ( opacity : 0 ; ) ) )
Shift right and left
The wider the input field, the larger the value of the text-indent property should be. For a standard input field, 500px will be enough; for wider ones, you should select it manually. The animation speed depends on the width of the input field and the text-indent value. To shift left you need to use negative values, for example -500px .
Shift down
As in the previous example, the animation depends on the size of the input, but in this case, pay attention to the height. For the vast majority of input fields, the required line-height value will be within 100px . Unfortunately, the line-height property cannot be used to create an upward shift effect, since the property does not accept negative values.
.input ( @include placeholder ( text-indent : 0px ; transition : text-indent 300ms ; ) & :focus ( @include placeholder ( text-indent : 500px ; ) ) )
Together
To make using animation code for placeholders pleasant and convenient, you can write a small library of mixins for any preprocessor. The library looks like this ():
@mixin placeholder ( & : :- webkit-input-placeholder ( @content ) & : - moz-placeholder ( @content ) & : :- moz-placeholder ( @content ) & : - ms-input-placeholder ( @content ) ) @mixin pl-shift ($side : left , $position : 500px , $duration : 200ms ) ( @include placeholder ( text-indent : 0 ; transition : text-indent $duration ; ) & :focus ( @include placeholder ( text-indent : if ($side == left , - $position , $position ); ) ) ) @mixin pl-slide-down ($position : 5 , $duration : 200ms ) ( @include placeholder ( line-height : 1 ; transition : line-height $duration ; ) & :focus ( @include placeholder ( line-height : $position ; ) ) ) @mixin pl-fade-out ($duration : 200ms ) ( @include placeholder ( opacity : 1 ; transition : opacity $duration ; ) & :focus ( @include placeholder ( opacity : 0 ; ) ) )It's very easy to use. It is enough to connect the desired mixin to any input field or simply create one global rule for all paceholders on the page:
// For individual elements.pl-shift-right ( @include pl-shift ( right ); ) .pl-fade-out ( @include pl-fade-out ; ) // For everything else@include pl-shift ( left );Autoprefixer
If you don't use a preprocessor and still don't want your CSS source file to turn into a mess of prefixes for everyone to see possible browsers, then pay attention to Autoprefixer. With his help pure CSS can be made dirty (but in a different file) by adding all the necessary prefixes to all properties. To make the plugin work with placeholders, just use the pseudo-element::placeholder:
::placeholder ( color : orange ; ) . input::placeholder(color:blue;)After parsing your styles, Autoprefixer will create a separate CSS file in which it will write all the necessary prefixes for all browsers you specified.
What's next
As I wrote above, everything related to applying styles to placeholders has not yet been standardized. This will be fixed soon. The Selector Level 4 specification added a pseudo-class: placeholder-shown , which will finally standardize the crazy mixture of pseudo-classes and pseudo-elements that currently exists. You can monitor support on caniuse (currently not supported in any browser).
Applying styles with :placeholder-shown will be much easier:
.input :placeholder-shown ( color : blue ; )Hello, today you won’t surprise anyone using a placeholder for input. Placeholder is a temporary text with an example inside the input that disappears when you enter text. We will try to write the CSS style for the placeholder today.
Task: make your own CSS style for placeholder
Let's say we have a stylish website that uses a placeholder in the input. We need to make the gray, boring text style of the placeholder look consistent with the general style of the site. How can this be achieved? I’ll say right away that this does not work in all browsers yet. Specifically, it doesn’t work at all in IE versions 9 and below; in 10 and later it still works with great difficulty. Firefox and webkit browsers are another matter.
Solution: css style for placeholder
So, we have set the task, how are we going to solve it?
First, let's create a testing ground:
Done, now let's see what we can do with the style: webkit and mozilla have their own modifiers that allow you to assign a special style to the placeholder: ::-webkit-input-placeholder and:-moz-placeholder. Let's see how to use them:
Let's describe the style of the input itself and the text in it (blue color for contrast in the example):
Input( width: 250px; color: blue; font-weight: normal; font-style: normal; )
Now let’s write a special style for the placeholder text in this input for Webkit browsers (Chrome, Safari, Opera):
Input::-webkit-input-placeholder( color: red; font-style: italic; font-weight: bold; )
Let's make the placeholder text red bold italic. Note that unlike other CSS pseudo-classes, which are separated by a single colon, the style for placeholder in webkit is separated by a double colon.
Now let's write exactly the same style for Mozilla Firefox browsers:
Input:-moz-placeholder( color: red; font-style: italic; font-weight: bold; )
Ready. CSS style for placeholder, of course you can write any one that is more suitable for your design. A working demo can be viewed at
If you are not familiar with the placeholder attribute, then let's explain a little what it is and where it is used. It is used in form input fields. The attribute displays a text in the input field, representing the role of a certain hint. Previously on our site there was an example using javascript, the positive side this method It's cross-browser compatible. Now let's talk about styling the placeholder attribute, which is used on input and textarea elements.
The input field code in our example will look something like this:
At the output we get the following field:
Now let's imagine that we need to style the placeholder, for this we need to write a set of rules in CSS:
::-webkit-input-placeholder ( color: #c1c1c1; ) ::-moz-placeholder ( color: #c1c1c1; ) /* Firefox 19+ */ :-moz-placeholder ( color: #c1c1c1; ) /* Firefox 18- */ :-ms-input-placeholder ( color: #c1c1c1; )
We get:
We cannot change all properties, but a list of most of them supported by modern browsers is given below:
font(also related properties)
background(also related properties)
color
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
text-indent
text-overflow
opacity
IN different browsers the rule is written differently, because While there is no single standard, this set of records continues to be relevant. In the IE browser, as well as firefox below version 18, placeholder is perceived as a pseudo-class, and in newer ones firefox browsers, webkit and blink are treated as pseudo-element.
Shortening text in placeholder
Sometimes tooltips can be so long that they cannot completely fit into the input field. For these purposes, you can also use additional properties that will allow you to shorten the placeholder text in the input field.
Input ( text-overflow:ellipsis; ) input::-moz-placeholder ( text-overflow:ellipsis; ) input:-moz-placeholder ( text-overflow:ellipsis; ) input:-ms-input-placeholder ( text-overflow :ellipsis; )
As a result, we get an input field with a placeholder like this:
Hide text when clicking on a field
By default, each browser interprets placeholder's response differently. In some browsers it is hidden immediately when you click on the field, in others it is hidden when there is at least one character in the input field. Let's make the text disappear when you click on the field, the same in all browsers.
:focus::-webkit-input-placeholder ( color: transparent; ) :focus::-moz-placeholder ( color: transparent; ) :focus:-moz-placeholder ( color: transparent; ) :focus:-ms-input -placeholder ( color: transparent; )
We get this action: