Max Glenister

Web Dev • 3 min read Plain text

Making HTML5 input placeholder text behave in Safari

Making assumptions during web development is never a good idea – especially if your assumptions are about the way a new feature will behave across modern browsers. This is an issue because of two things:

  1. There are a large number of ‘modern’ browsers that need to be supported.
  2. The HTML5 specification isn’t fully supported by all of these browsers, or isn’t supported in the same way from browser to browser.

This issue rears its ugly head in one way or another in my day to day life. First of all, regarding the ‘modern’ browsers that need to be supported, we’ve got:

  1. Internet Explorer - 7, 8, 9
  2. Firefox - 3.6, 4.0, 14.0-*
  3. Chrome

These browsers all have their own implementations of new CSS/HTML5 features, some work out of the box, some need polyfills (What is a polyfill?), some need vendor-prefixes (What are vendor-prefixes?).

Back to the context of placeholder text in Safari then, I had been developing in Chrome on Mac OS X, and I had assumed that as both Safari and Chrome use Webkit as their rendering engine, there would be little difference between them. I was quite wrong.

The issue here is how each browser supports styling of the placeholder text. Here’s a matrix of the style support:

(From John Catterfeld’s blog: Styling the HTML5 placeholder)

As you can see we’re given little in the way of actually styling placeholder text.

::-webkit-input-placeholder {
    // styles
}
::-moz-placeholder {
    // Firefox 19+
    // styles
}
:-ms-input-placeholder {
    // styles
}
:-moz-placeholder {
    // Firefox 18-
    // styles
}

These are the selectors you should use to actually style placeholder text.

Webkit has implemented placeholder text ‘wrong’. Well, at least it’s wrong according to the spec, as the placeholder text does not get cleared when you focus the input element.

User agents should present this hint to the user, after having stripped line breaks from it, when the element’s value is the empty string and/or the control is not focused (e.g. by displaying it inside a blank unfocused control and hiding it otherwise).

WHATWG - the-placeholder-attribute

Anyway, enough background, on to my problem. First of all, let’s see how browsers handle placeholder text with and without a line-height applied to the input.

Firefox

Opera

Chrome

Safari

You can play with the jsFiddle that I made to test this here:

I wrote this blog post after I found the cause for the issue I was having in Safari, so I’ve prepared these screenshots with the solution in place. As you can see, Safari messes up the alignment of placeholder text if you’ve set line-height on the input element. I believe this is a bug, because the input respects the line-height declaration as soon as you begin typing (and the placeholder text is cleared).

Then again, for an element that is only showing a single line of text (not wrapping like textarea maybe setting line-height is incorrect. In which case, it shouldn’t be supported by any browser on the input element.)

Given the huge difference in how different browsers handle the rendering of padding and other box-model metrics for the input element, I’m not surprised.

  • Web Dev · shares #css
    Better link underlines with Sass

    There has been some talk lately about how iOS 8 has started to handle typography descenders when text is underlined. They’ve done away with the usual overlapping line intersect in favour of a more aesthetically pleasing line crop.

    November 2014 • 3 min read
  • Web Dev · shares #css
    Some approaches for creating diagonal section separators for your website

    In their simplest form, web pages are made of stacked rectangles. Every block-level element takes up as much vertical space as the content requires, and fills as much horizontal space as is available. Additionally, block-level elements can have their width/height specified. When you have absolutely positioned, or floated elements this changes a bit but the rule pretty much sticks.

    May 2014 • 5 min read
  • Web Dev · shares #guide
    Getting started with using Sass in your existing website

    Sass is a CSS-compatible preprocessor for CSS. It allows you to reduce the amount of duplication and complexity in your CSS by using variables, mixins (functions), nesting, and other cool things that should have existed in the CSS spec from the beginning.

    January 2014 • 5 min read

Discussion

  • Good to know, thanks!

  • R

    I was having a problem with Safari 6 on the Mac, and your advice allowed me to solve the issue. So it would seem that this is still a problem on the Mac version of Safari 6 (at least in my experience).

  • D

    Got the same problem on FF 21, fixed it by removing the line-height altogether.

  • E

    Detect safariThen use this css:.safari form input[type=”text”] {line-height: normal;}

  • J

    Using javascript to detect the browser for safari works for me.

Join the conversation

Reply from your own site with a Webmention (how?).

Webmentions last fetched 13 November 2026, 07:47