Placeholder styling css. The appearance of the field names or how to change the placeholder
If you're not familiar with the placeholder attribute, let's explain a bit what it is and where it's used. It is used in form input fields. The attribute displays an inscription in the input field, representing the role of a hint. Earlier on our site there was an example with using javascript, positive side this method it's cross browser. Now let's talk about styling the placeholder attribute, which is used on the input and textarea elements. The code for the input field in our example would 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:
We can not 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
AT different browsers the rule is written differently, because as long as 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 the new firefox browsers, webkit and blink are treated as pseudo-elements.
Shorten text in placeholder Sometimes hints can be so long that they cannot fully fit into the input field. For these purposes, you can also use additional properties that will shorten the placeholder text in the input field.
As a result, we get an input field with a placeholder like this:
Hide text on click By default, each browser interprets the placeholder response differently. In some browsers, it is hidden immediately when you click on the field, in others it is hidden if there is at least one character in the input field. Let's make it so that the text is hidden when clicking on the field, the same in all browsers.
In an effort to reduce shape and reduce visual noise, tag designers
Badly
Good
Styling: change color of placeholder 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 JavaScript value
The placeholder attribute is used to create tooltips inside empty input fields (tags and
Let's start with an example for those who don't yet know what a placeholder is and how to use it:
When developing interfaces, you should take into account the behavior of placeholders. The placeholder text disappears as soon as typing begins. That is why you should not use them to convey information about the input field itself (how many and what characters it should contain). For these purposes, it is best to use the tag or a paragraph with a little hint. You can read more about the features of using placeholders.
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 in each browser is different. You can learn more about all the necessary prefixes at caniuse .
If you're using a CSS preprocessor, you'll probably find it best to write a simple mixin to style placeholders. Example in Sass:
Depending on the context, the mixin can be used to apply styles globally or on individual elements (try it out on SassMeister):
// Globally for each input field@include placeholder ( color : blue ; ) // For certain input fields.input ( @include placeholder ( color : green ; ) )
Example
Blue text for the placeholder (never do this):
.blue-text ::-webkit-input-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
Animation ideas belong to the html5.by blog.
All of the following examples are written using the Sass preprocessor. Each comes with a link to SassMeister where you can find the compiled CSS code.
You will most likely want to apply animations to placeholders when the focus is on an input field. All this is done quite simply. It is enough just to use the previously written placeholder mixin a few times:
.input( @include placeholder( // Styles for the normal state) & :focus ( @include placeholder ( // Styles after the focus event }
}
}
The larger the width of 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 manually. The width of the input field and the text-indent value determine the speed of the animation. To shift to the 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 desired line-height value will be within 100px . Unfortunately, using the line-height property, it is impossible to implement the effect of moving up, since the property does not accept negative values.
To make it nice and convenient to use the animation code for placeholders, you can write a small library of mixins for any preprocessor. The library looks like this ():
Its very easy to use. It is enough to connect the desired mixin to any input field, or simply create one global rule for all the 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 are not using a preprocessor and still do not want your original css file turned into a mess of prefixes for all possible browsers, then take a look at Autoprefixer . With it, 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 paceholders, it is enough to use 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 the browsers you specified.
What's next
As I wrote above, everything related to applying styles to placeholders has not yet been standardized. It will be fixed soon. A pseudo-class was added to the Selector Level 4 specification: placeholder-shown , which will finally bring to the standard all that crazy mixture of pseudo-classes and pseudo-elements that exists now. You can check support on caniuse (currently not supported in any browser).
Applying styles with :placeholder-shown will be much easier:
.input :placeholder-shown ( color : blue ; )
Typically the placeholder attribute on input and textarea is used to 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 bulk of the code for styling such a simple property is due to the lack of a common standard, in connection with which each browser 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 text of the placeholder "goes" beyond the width of the input or textarea element (a common case for mobile devices). At the same time, it is automatically cropped ugly.
A css property such as text-overflow: ellipsis will help us fix this trouble, which will add an aesthetic ellipsis to the cropped area of the placeholder-a. In styles we write like this:
The placeholder attribute is used to call to action inside empty input and textarea elements. In this article, we will look at the possibilities of styling placeholder-a text, as well as some tricks that will make it more convenient and functional.
So, let's start with an example for those who don't know what a placeholder is.
html
Placeholder-a style can be changed using the following set of css rules:
Looks scary, doesn't it? The fact is that this is still not in the standards. Each browser has implemented support for placeholder styling in its own way.
in IE and old firefox(before the 18th) placeholder is considered a pseudo-class, and in new firefox, webkit and blink are pseudo-elements.
Let's see what happened:
I must say that not all possible css properties are supported. Most modern browsers allow you to change:
font (and related properties)
background (and related properties)
color
word-spacing
letter spacing
text-decoration
vertical-align
text-transform
line-height
text indent
text overflow
opacity
What if the placeholder doesn't fit?
Sometimes text input fields are reduced in width due to layout features, in particular on mobile devices. In this case long text placeholder will be ugly cut off. To prevent this, you can use text-overflow: ellipsis . This syntax will work in all new browsers.