Curated by Carsonified

Learn with Treehouse

Accessibility, CSS3, Design, Django, HTML & CSS, HTML5, JavaScript, jQuery, NoSQL, PHP, Responsive Web Design, Ruby, Ruby on Rails, Tools, UX, Version Control, WordPress, iOS and more

Article 24

Styling Submit Buttons for Mobile Safari

By @keirwhitaker

31 August 2010 | Category: Design

As discussed on the most recent episode of Think Vitamin Radio we have been working hard on the redesign of this site and have been looking at how the site reacts when rendered on the iPhone using media queries (if these are new to you then Brian Suda's most recent article will get you started).

One thing both Mike and I had noticed is that submit buttons, when styled with CSS, appear to have a will of their own when viewed on the iPhone.

Submit Button Demo

Here's some basic CSS for a submit button:

input[type="submit"] {
    background: #262C32;
    width: 150px;
    padding: 9px;
    letter-spacing: 1px;
    border: none;
    color: #EFDDA8;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}


There’s nothing complicated here. We are just adding some basic properties such as background colour, padding, letter-spacing and applying vendor-specific border-radius. Here’s what we get when viewed in the browser (Firefox in this case):

So far so good. Now let’s look at a screenshot of that same button viewed on the iPhone (screenshot taken from iPhone 4 Apple SDK simulator):

For some reason the button now has a gradient, the text is in the wrong position, the corners are very rounded and the padding appears to have not been applied. Not the intended result.

The Solution

Luckily there is a very quick solution to this problem. After some searching I found out that unless explicitly told mobile Safari will change the appearance of buttons & controls to resemble a native Apple control. Thankfully there’s a simple fix.

1
2
3
4
5
6
7
8
9
10
11
12
input[type="submit"] {
    background: #262C32;
    width: 150px;
    padding: 9px;
    letter-spacing: 1px;
    border: none;
    color: #EFDDA8;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -webkit-appearance: none;
}

Notice the last line of our CSS? By adding -webkit-appearance: none; we are telling mobile Safari that we explicitly don’t want our button to be styled like a native Apple UI control.

After adding this to our button’s CSS we get the following result on the iPhone:

As you can see (and they are different screenshots) this is identical to our first picture.

If you haven’t already I am sure you will come across this when developing for the iPhone, I hope this saves you a few minutes head scratching.

Further Reading

Follow @thinkvitamin on Twitter Please check out Treehouse

Other Posts You Might Find Interesting

  • Sorry - No Related Posts Found

Comments

  • http://rob.lifford.org/ Rob L.

    Yep, that’s an important tidbit to know. Worth mentioning that you’ll also need -webkit-appearance: none to properly apply custom styles to an HTML5 .

  • http://rob.lifford.org/ Rob L.

    Yep, that’s an important tidbit to know. Worth mentioning that you’ll also need -webkit-appearance: none to properly apply custom styles to an HTML5 .

  • http://rob.lifford.org/ Rob L.

    Guess I needed to escape my angle brackets. That comment should end with ‘an HTML5 input[type="search"]‘.

  • http://rob.lifford.org/ Rob L.

    Guess I needed to escape my angle brackets. That comment should end with ‘an HTML5 input[type="search"]‘.

  • http://dtott.com dan ott

    You can also use this to make selects and other inputs appear more normal/mozilla-y in regular webkit browsers

  • http://dtott.com dan ott

    You can also use this to make selects and other inputs appear more normal/mozilla-y in regular webkit browsers

  • Keir Whitaker

    Hey Rob, great point. Thanks for chipping in.

  • Keir Whitaker

    Hey Rob, great point. Thanks for chipping in.

  • Keir Whitaker

    Hey Dan, Interesting – I hadn’t come across too many other situations but thinking about it the UI controls do appear quite differently across the different browsers. One to remember when styling up any input I guess.

  • Keir Whitaker

    Hey Dan, Interesting – I hadn’t come across too many other situations but thinking about it the UI controls do appear quite differently across the different browsers. One to remember when styling up any input I guess.

  • http://www.webdesign-podcast.de Pascal

    -webkit-appearance: none; – That’s what I’m looking

  • http://www.webdesign-podcast.de Pascal

    -webkit-appearance: none; – That’s what I’m looking

  • http://selfesteemuk.com Julie

    Gotcha, thanks for the code, I am really looking for this for the past few days..wwhheeww..

  • http://selfesteemuk.com Julie

    Gotcha, thanks for the code, I am really looking for this for the past few days..wwhheeww..

  • http://galantdesign.se Anton

    Have been pulling my hair on this one for a while. Great job!

  • http://galantdesign.se Anton

    Have been pulling my hair on this one for a while. Great job!

  • http://www.fasalsalam.com fasalsalam

    Hey this is great article thanks dude

  • Sgomez174

    Wonderful, thank you very much!

  • Ayame

    Is it possible that the submit input type also has a minimum width it expects on iOS, regardless of wether you apply the -webkit-appearance: none; rule?
    It seems that no matter what I do, the button’s text is always prefaced by a certain amount of padding, whether I reset that padding or not. As long as you have large buttons, there’s no problem, no one notices. However I want a 40px button (I know it’s a tad small but never mind that) but there’s always about 11px padding to the left side I cannot get rid of.

    Any suggestions on how to handle this?

  • http://simplyfoodndrink.com/top-10-gordon-ramsay-books/ gordon ramsay

    thank you very much… love it…

  • Saturngod

    Yayyyy. … I found it …. It was problem in my sites …. Thank ….

  • Magnus Magnusson

    Thank you for this. Very useful! :)

  • Meliika

    Thank you, I had exactly the same problem and a lack of ability to test fixes easily so this was a great help.

  • Meliika

    Thank you, I had exactly the same problem and a lack of ability to test fixes easily so this was a great help.

Learn Web Design!

Ads Via The Deck

Think Vitamin Radio
Episode #34: Amazon Fire and Responsive Roundtable

Check out our bi-weekly radio show. Covering the hot topics on the web.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Download Podcast as mp3

Advisory Board

The Think Vitamin Advisory Board in place make sure that you receive the best content possible.