Typically the placeholder attribute on input and textarea is used to provide a call to action within those elements. In this note I will describe several options for styling the text of placeholders, as well as options for their animation
So, the text color of our placeholder can be changed using, for example, the following set of CSS rules:
::-webkit-input-placeholder ( color : #c0392b ;) ::-moz-placeholder ( color : #c0392b ;) /* Firefox 19+ */ :-moz-placeholder ( color : #c0392b ;) /* Firefox 18- */ :-ms-input-placeholder ( color : #c0392b ;)
The length of code for styling such a simple property is due to the lack of a common standard, and therefore each browser has implemented support for placeholder styling in its own way.
Also, not all are supported css properties. Here is a list of exactly supported properties:
font (and related)
background (and related)
color
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
text-indent
text-overflow
opacity
It also happens that the placeholder text “extends” the width of the input or textarea element (a common case for mobile devices). At the same time, it is automatically cut off unsightly.
A CSS property like text-overflow: ellipsis will help us fix this problem, which will add an aesthetic ellipsis to the cropped area of the placeholder. In styles we write like this:
input [ placeholder ] ( text-overflow : ellipsis ;) input ::-moz-placeholder ( text-overflow : ellipsis ;) input :-moz-placeholder ( text-overflow : ellipsis ;) input :-ms-input-placeholder ( text-overflow : ellipsis ;)
To add even more aesthetics to our placeholders, it is possible to add the effect of hiding it when the corresponding element receives focus.
Some examples of such hiding with animation elements:
/* smooth change in placeholder transparency when focused */.input ::-webkit-input-placeholder ( opacity : 1 ; transition : opacity 0.3s ease ;) .input ::-moz-placeholder ( opacity : 1 ; transition : opacity 0.3s ease ;).input :-moz- placeholder (opacity: 1; transition: opacity 0.3s ease;).input:-ms-input-placeholder (opacity: 1; transition: opacity 0.3s ease;).input (opacity: 0; transition: opacity 0.3s ease; ) .input :focus::-moz-placeholder ( opacity : 0 ; transition : opacity 0.3s ease ;).input :focus:-moz-placeholder ( opacity : 0 ; transition : opacity 0.3s ease ;).input :focus :-ms-input-placeholder ( opacity : 0 ; transition : opacity 0.3s ease ;) /* shift the placeholder to the right when focused */.input ::-webkit-input-placeholder ( text-indent : 0px ; transition : text-indent 0.3s ease ;) .input ::-moz-placeholder ( text-indent : 0px ; transition : text-indent 0.3s ease ;).input :-moz-placeholder ( text-indent : 0px ; transition : text-indent 0.3s ease ;).input :-ms-input-placeholder ( text-indent : 0px ; transition : text-indent 0.3s ease ;).input :focus::-webkit-input-placeholder( text-indent : 500px ; transition : text-indent 0.3s ease ;).input :focus::-moz-placeholder ( text-indent : 500px ; transition : text-indent 0.3s ease ;).input :focus:- moz-placeholder ( text-indent : 500px ; transition : text-indent 0.3s ease ;) .input :focus:-ms-input-placeholder ( text-indent : 500px ; transition : text-indent 0.3s ease ;) /* shift the placeholder down when focused */.input ::-webkit-input-placeholder ( line-height : 20px ; transition : line-height 0.5s ease ;) .input ::-moz-placeholder ( line-height : 20px ; transition : line-height 0.5s ease ;).input :-moz-placeholder ( line-height : 20px ; transition : line-height 0.5s ease ;).input :-ms-input-placeholder ( line-height : 20px ; transition : line-height 0.5s ease ;).input :focus::-webkit-input-placeholder( line-height : 100px ; transition : line-height 0.5s ease ;).input :focus::-moz-placeholder ( line-height : 100px ; transition : line-height 0.5s ease ;).input :focus:- moz-placeholder ( line-height : 100px ; transition : line-height 0.5s ease ;).input :focus:-ms-input-placeholder ( line-height : 100px ; transition : line-height 0.5s ease ;)
Let's finish here.
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. Earlier on our website there was an example, with using javascript, 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 new browsers firefox, webkit and blink it is perceived as a 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:
In an effort to reduce shape and reduce visual noise, designers tag
Badly
Fine
Styling: change placeholder color CSS
Change input field style with placeholder CSS
Badly
Placeholder effect for
The placeholder attribute only works for And
An ancient memory method that changes the value of JavaScript
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 control them, you need to use a special CSS rule placeholder
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;
- padding- 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 the browser Google Chrome When focusing on such a field, the color of the hint will smoothly change within one second.
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. To do this, a special non-standard CSS::placeholder pseudo-element is used. With its help you can manage the properties of the tooltip.
You will be interested:
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, older Mozilla browsers, as well as IE, consider placeholder to be a CSS pseudo-class and 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 - group of background properties. The hint block background 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 the CSS placeholder pseudo-element allows you to implement fading 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 only works for And
An ancient memory method that changes the value of JavaScript
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 control them, you need to use a special CSS rule placeholder
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;
- padding- 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 the browser Google Chrome When focusing on such a field, the color of the hint will smoothly change within one second.
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. To do this, a special non-standard CSS::placeholder pseudo-element is used. With its help you can manage the properties of the tooltip.
You will be interested:
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, older Mozilla browsers, as well as IE, consider placeholder to be a CSS pseudo-class and 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 - group of background properties. The hint block background 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 the CSS placeholder pseudo-element allows you to implement fading 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.