Skip to content
Fix Code Error

Change a HTML5 input’s placeholder color with CSS

March 13, 2021 by Code Error
Posted By: Anonymous

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn’t do anything to the placeholder’s value:

input[placeholder], [placeholder], *[placeholder] {
    color: red !important;
}
<input type="text" placeholder="Value">

But Value will still remain grey instead of red.

Is there a way to change the color of the placeholder text?

Solution

Implementation

There are three different implementations: pseudo-elements, pseudo-classes, and nothing.

  • WebKit, Blink (Safari, Google Chrome, Opera 15+) and Microsoft Edge are using a pseudo-element: ::-webkit-input-placeholder. [Ref]
  • Mozilla Firefox 4 to 18 is using a pseudo-class: :-moz-placeholder (one colon). [Ref]
  • Mozilla Firefox 19+ is using a pseudo-element: ::-moz-placeholder, but the old selector will still work for a while. [Ref]
  • Internet Explorer 10 and 11 are using a pseudo-class: :-ms-input-placeholder. [Ref]
  • April 2017: Most modern browsers support the simple pseudo-element ::placeholder [Ref]

Internet Explorer 9 and lower does not support the placeholder attribute at all, while Opera 12 and lower do not support any CSS selector for placeholders.

The discussion about the best implementation is still going on. Note the pseudo-elements act like real elements in the Shadow DOM. A padding on an input will not get the same background color as the pseudo-element.

CSS selectors

User agents are required to ignore a rule with an unknown selector. See Selectors Level 3:

a group of selectors containing an invalid selector is invalid.

So we need separate rules for each browser. Otherwise the whole group would be ignored by all browsers.

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color:    #909;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
   color:    #909;
   opacity:  1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
   color:    #909;
   opacity:  1;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color:    #909;
}
::-ms-input-placeholder { /* Microsoft Edge */
   color:    #909;
}

::placeholder { /* Most modern browsers support this now. */
   color:    #909;
}
<input placeholder="Stack Snippets are awesome!">

Usage notes

  • Be careful to avoid bad contrasts. Firefox’s placeholder appears to be defaulting with a reduced opacity, so needs to use opacity: 1 here.
  • Note that placeholder text is just cut off if it doesn’t fit – size your input elements in em and test them with big minimum font size settings. Don’t forget translations: some languages need more room for the same word.
  • Browsers with HTML support for placeholder but without CSS support for that (like Opera) should be tested too.
  • Some browsers use additional default CSS for some input types (email, search). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance and -moz-appearance to change that. Example:
    [type="search"] {
        -moz-appearance:    textfield;
        -webkit-appearance: textfield;
        appearance: textfield;
    }
Answered By: Anonymous

Related Articles

  • Why are CSS keyframe animations broken in Vue…
  • Active tab issue on page load HTML
  • How to detect Safari, Chrome, IE, Firefox and Opera browser?
  • Ukkonen's suffix tree algorithm in plain English
  • loop and eliminate unwanted lines with beautiful soup
  • Navbar not filling width of page when reduced to mobile view
  • What does do?
  • How to prevent scrolling the whole page?
  • What are the currently supported CSS selectors…
  • Vue js vuecli3 application does not work in ie11…
  • Jquery fadeToggle Trouble
  • Web colors in an Android color xml resource file
  • CSS selector for first element with class
  • CSS animation as a Google Map Marker (Polymer 1.0)?
  • What is WebKit and how is it related to CSS?
  • Animating Elements in One by One
  • Imitating a blink tag with CSS3 animations
  • Increasing size of semi circle using css
  • How to write specific CSS for mozilla, chrome and IE
  • How to detect my browser version and operating…
  • Is there any "font smoothing" in Google Chrome?
  • Python Key Error=0 - Can't find Dict error in code
  • How to set HTML5 required attribute in Javascript?
  • Is it possible to apply CSS to half of a character?
  • during wct test: Failed to load resource: the server…
  • Positioned element is off when on Safari`
  • After a little scroll, the sticky navbar just is not…
  • Javascript validate all checkboxes are selected
  • How to change the color of vaadin-select-text-field…
  • Java - Find shortest path between 2 points in a…
  • CSS customized scroll bar in div
  • How does PHP 'foreach' actually work?
  • highcharts will not render to my div
  • Converting between strings and ArrayBuffers
  • Export to CSV using jQuery and html
  • What are -moz- and -webkit-?
  • How to remove the arrow from a select element in Firefox
  • How to generate a random string of a fixed length in Go?
  • JavaScript Loading Screen while page loads
  • How to detect chrome and safari browser (webkit)
  • CSS Input with width: 100% goes outside parent's bound
  • How to set width of mat-table column in angular?
  • Preventing scroll bars from being hidden for MacOS…
  • Detect if Visual C++ Redistributable for Visual…
  • How to display a range input slider vertically
  • CSS3 Rotate Animation
  • Limit file format when using ?
  • Best approach to real time http streaming to HTML5…
  • Is CSS Turing complete?
  • For loop for HTMLCollection elements
  • Paper checkbox and -webkit-column-count
  • Manually adding a Userscript to Google Chrome
  • Playing HTML5 video on fullscreen in android webview
  • Three.js: Cannot display mesh created with texture array
  • How to prevent column break within an element?
  • how to access parent component scope from a child…
  • How to solve Internal Server Error in Next.Js?
  • Text blinking jQuery
  • Using CSS for a fade-in effect on page load
  • Program crashing after a few seconds because of…
  • How to render web component in Polymer
  • How can I draw vertical text with CSS cross-browser?
  • Updating VueJS component data attributes when prop updates
  • how to integrate selenium driver for chrome and…
  • Show/hide 'div' using JavaScript
  • Disable/turn off inherited CSS3 transitions
  • How to create a drop shadow only on one side of an element?
  • Javascript change webkit style with variables
  • Am I paranoid? "Brutally" big Polymer website after…
  • Using Shadow Dom CSS Animations on distributed…
  • center 3 items on 2 lines
  • How do I convert Word files to PDF programmatically?
  • Image comparison - fast algorithm
  • Putting -moz-available and -webkit-fill-available in…
  • How to generate a random number in C++?
  • load and execute order of scripts
  • Handling touchstart/touchmove/touchend the same way…
  • How do you get string interpolation working on a…
  • Google map markers not working with Shadow DOM in Safari
  • How to create a Carousel in NextJS?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Super slow preflight OPTIONS in Chrome only
  • Selenium using Java - The path to the driver…
  • How to resolve "Could not find schema information…
  • Error: the entity type requires a primary key
  • In CSS Flexbox, why are there no "justify-items" and…
  • Clearing a polyline on a Polymer Google Map
  • Google Forms file upload complete example
  • Run chrome in fullscreen mode on Windows
  • HTML 5 Favicon - Support?
  • How can I wrap all BeautifulSoup existing…
  • Should ol/ul be inside or outside?
  • Daisy is overlapping the other contents of the div
  • Do not close the mode in the mouse up event
  • Triggering text area by clicking related select/checkbox
  • Image Greyscale with CSS & re-color on mouse-over?
  • What is the cleanest way to disable CSS transition…
  • How to simplify sequential logic design by…
  • Bootstrap 4 - Responsive cards in card-columns
  • Pass array and/or object data between Polymer elements

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I vertically align text in a div?

Next Post:

Creating a div element in jQuery

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error