Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make only placeholder italics in input

How can I format the text of a placeholder in italics, but when someone writes inside the textbox, the text shouldn't be in italics.

code:

<input type="text" id="search" placeholder="Search" style="font-style:italic"> 

How can I place italic text only on placeholder but not whole input?

like image 695
prabin badyakar Avatar asked Dec 25 '14 04:12

prabin badyakar


People also ask

How do you make a placeholder italic in HTML?

From MDN, you can use the :placeholder-shown pseudo-class. This has the advantage of being un-prefixed, and the browser support is decent (92-ish%) at the time of this writing: https://caniuse.com/#feat=css-placeholder-shown.

Can you change placeholder font?

<input type="text" placeholder="A red placeholder text..">

Can you style placeholder text?

The ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text. The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.

How do I change placeholder text?

The “placeholder” property is used to get or set the placeholder of an input text. This can be used to change the placeholder text to a new one. The element is first selected using a jQuery selector. The new placeholder text can then be assigned to the element's placeholder property.


2 Answers

Sure. Just apply the style to these CSS rules:

::-webkit-input-placeholder {    font-style: italic; } :-moz-placeholder {    font-style: italic;   } ::-moz-placeholder {    font-style: italic;   } :-ms-input-placeholder {      font-style: italic;  } 

Probably not all of these rules are needed. I always just reference CSS Tricks because I always forget how to do it.

Edit: Note that these rules cannot be combined into one selector. They must be declared separately as noted by Dave Kennedy in the comments below.

like image 168
Joseph Marikle Avatar answered Sep 21 '22 01:09

Joseph Marikle


From MDN, you can use the :placeholder-shown pseudo-class.

input:placeholder-shown {    font-style: italic; } 

This has the advantage of being un-prefixed, and the browser support is decent (92-ish%) at the time of this writing: https://caniuse.com/#feat=css-placeholder-shown.

like image 43
Zac Avatar answered Sep 22 '22 01:09

Zac