The iPhone, like Safari and many other browsers, runs on WebKit, which is one of the most blazing fast, powerful, standards-compliant rendering engine ever. Although there is no official Cocoa API for iPhone app development, it’s not like we have nothing to work with. Writing web apps for the iPhone will be a pretty good experience for two reasons:
- We don’t have to worry about IE support when writing our web app
- WebKit is amazing.
Highlighted below are ten CSS rules that make WebKit extra-great. Many of the previews require a WebKit-based browser like Safari, OmniWeb or Shiira.
WebKit and CSS
1. Native Controls
A feature unique to the later versions of WebKit is the ability to style form controls. For a long time, the form inputs were kept as the operating system’s native controls, but now they are specifically styled that way. This means, we can easily over-ride that styling, or make elements appear like native controls.
Take a look at these text inputs:
If you’re running a WebKit-based browser like Safari, you’ll see the first text input as a normal OS X-style control, but the second one is a rounded control (like the search input minus the cancel widget).
Now look at these anchors:
Both are <a> tags, but the second one appears as a push-button in Safari.
That’s not all. Here are two button inputs:
Notice how the second button is a square button?
Have I caught your interest? Here’s how you do it.
Mimic native widgets / controls
Elements are styled to look like native controls for WebKit-based browsers using the -webkit-appearance CSS property. Some of the values this can take are (in two columns if you’re on WebKit):
- checkbox
- radio
- push-button
- square-button
- button
- button-bevel
- listbox
- listitem
- menulist
- menulist-button
- menulist-text
- menulist-textfield
- slider-horizontal
- slider-vertical
- sliderthumb-horizontal
- sliderthumb-vertical
- searchfield
- searchfield-decoration
- searchfield-results-decoration
- searchfield-results-button
- searchfield-cancel-button
- textfield
- textarea
Setting -webkit-appearance simply makes the element appear as if it were the control. This could be of enormous use within WebViews inside Cocoa applications or with web apps for the iPhone.
If don’t have access to a WebKit-based browser, here’s a screenshot of what some of the “controls” look like:

The important thing to note is that although the controls can be made to look like other controls using -webkit-appearance, their function is not affected. That is, textboxes will still accept text when they’re made to appear like push-button and so on.
2. Element Drag
Apparently dragging doesn’t work with the iPhone, as it’s used for scrolling. That’s to be expected, I guess.
Generally there is a lot of javascript involved in making an element draggable. Now you can achieve the same thing with only one line of CSS. Javascript will still be required to have drop targets, etc. but this allows the user to drag the entire element rather than just text or an image.
Try dragging this element (from the side):
Notice how the whole element is dragged? It’s just a regular div (made to act as a push-button) with the CSS rule -webkit-user-drag:element applied to it. Easy!
3. Edit the webpage
Doesn’t work on the iPhone. You can click in the element, but the keyboard doesn’t show up. This is likely to work when there are copy-paste functions in the phone.
This is a paragraph that you can edit if you’re running a WebKit-based browser like Safari or the iPhone. Just click here and edit the text like a normal textbox.
To allow users to edit elements of the page (or all of it) in real time, there exists a property -webkit-user-modify. Obviously this defaults to read-only, but you can set it to the following:
- read-only
- read-write
- read-write-plaintext-only
read-write allows the user to copy and paste in rich text with formatting, while read-write-plaintext-only pastes in only the text. This could be useful with debugging or as part of a WYSIWYG editor, etc. Just be careful with the security side of things.
4. Selection (Highlighting)
I am a paragraph that you can’t select (if you’re running Safari). Try and highlight me and realize that your attempts are futile. Bwah, ha ha…
To disallow selection (highlighting) of text for a particular element, set the CSS rule -webkit-user-select:ignore. This may be useful for many web apps, but use it sparingly to avoid irritating the user.
I am a paragraph that you can select. If your browser supports the `::selection` selector, highlighting this will result in a black background and white text for the highlighted region.
5. Rounded corners
Rounded corners: Look, no hacks!
-webkit-border-radius specifies the radius of the corners. You can specify certain corners only by using one of
- -webkit-border-top-left-radius
- -webkit-border-top-right-radius
- -webkit-border-bottom-left-radius
- -webkit-border-bottom-right-radius
For example, the following has the top-right and bottom-left corners set to a radius of 7px:
A bit of fancy corner-work
Elliptical corners are possible by specifying two values, eg. -webkit-border-radius: 3px 7px;.
6. Shadows
text-shadow
This text has a shadow when viewed with a compliant browser.
For the text-shadow CSS property, the parameters are
- horizontal offset (negative is to the left)
- vertical offset (negative is up)
- blur radius (higher is more blurred)
- shadow color
For example, using text-shadow: 1px 1px 7px #000; we get:
this excellent text with a blurred, but visible shadow.
box-shadow
This entire element has a shadow using -webkit-box-shadow:
To add a shadow to the actual element rather than just the text, use -webkit-box-shadow. Parameters are the same as for the text-shadow property.
- the horizontal offset
- the vertical offset
- the blur radius
- shadow color
The shadow automatically compensates for any rounded corners you may have added.
This element has a shadow, but also a border-radius.
text-stroke
I guess I’m really just a string of text. Oh, I’m stroked.
Use -webkit-text-stroke to set a border or stroke around the text. Specify thickness and color.
To change the fill color, use -webkit-text-fill-color; otherwise value for the color property is used.
7. Backgrounds
WebKit allows multiple background images using CSS. If you’re running Safari or other WebKit-based browser, see this great example of multiple background images with CSS.
The syntax for multiple backgrounds:
background-image: url("first-img"), url("second-img"),
url("other-img"), url("another-img");
background-repeat: no-repeat, no-repeat,
repeat-y, repeat-x;
background-position: top left, top right,
left, top;
The first image is associated with the first background-repeat, and the first background-position value and so on. The order in which the images appear also determines the stacking order - earlier images are stacked higher.
Shorthand background notation also works, the example above is equivalent to:
background: url("first-img") top left no-repeat,
url("second-img") top right no-repeat,
url("other-img") left repeat-y,
url("another-img") top repeat-x;
This could be really handy with making the background of a web-app, such as for the iPhone, without the need for multiple nested elements.
8. Border Image
To set fancy borders, you often need to use images. However, you can do away with all the javascript and CSS hacks and use -webkit-border-image.
Syntax:
-webkit-border-image: url("image-url") size fit fit;
“image-url” needs to be an image divided into nine parts for each corner and edge and the content. The size is the height / width of the squares that the image is divided into. Fit can be round or stretch, which are ways of fitting the image into the border. To show the border, you need to actually have a border rule, but that can be integrated into the border-image by using something like:
-webkit-border-image: url("image-url") 27 / 27px round round;
The / 27px bit specifies the width of the border and makes it show if it isn’t already.
9. Resizing
This element is actually resizable.
If you’re using a compatible browser, drag the handle at the bottom right to resize this box.
Max-width is restricted to 100%.
Making an element resizable is really easy: just set the resize property to vertical, horizontal or both.
This box can only be resized horizontally (along the x-axis)
To make scrollbars appear when the content doesn’t fit completely inside the box, it’s a good idea to set overflow:auto;.
Restrict the size that an element can be resized to by setting min-width, min-height, max-width and / or max-height.
10. Colors and Opacity
I am a-hued, a-saturated, and a-alpha-ed.
Colors can be set using rgb(red, green, blue), rgba(red, green, blue, alpha), hsl(hue, saturation, lightness) or hsla(hue, saturation, lightness, alpha).
The example above has the rule background-color: hsla(100,90%,50%,0.6);.
Opacity is inherited by any child elements.
Set opacity as a number between 0 (completely transparent) and 1 (completely opaque). If you don’t want the opacity to be inherited by child elements, set the background color using rgba or hsla and specify the alpha value there.
40 Comments so far
Leave a commentPages: « 1 [2] Show All
iPhone issues re: this page:
Overall: Wide single columns are very difficult to read on the iPhone. WordPress blogs are specifically guilty at this. I recommend a slightly narrower column if you are going to want people to read this on the iPhone
Section 1: The third text-input doesn’t look any different then the 2nd on the iPhone — no X like in Safari 3.0. The “go to top” button was shaded, but didn’t have a rounded rect around it like Safari 3.0. The second button wasn’t square like the Safari 3.0 one was.
Section 2: There appears to be no way to drag items on the iPhone like you can in Safari.
Section 3: You could add text to the text box in Safari, but when you tapped on the box on the iPhone the keyboard does not slide open thus you can’t enter any text.
Section 4: You do not appear to be able to select text in either text box. I don’t think selection is supported on the iPhone.
Section 5: All your rounded corners examples looked the same in both browsers.
Section 6: Text shadow worked, but box shadow did not. Text stroke did not.
Section 7/8: As others have noted, something in your file in either section 7 or 8 when rendered blows the iPhone’s safari away, crashing you back to the apps menu.
Section 9 and 10: I don’t know, I can’t view that far!
I encourage you to join the http://www.iPhoneWebDev.com community to discuss this topic. I have started a thread on it at http://groups.google.com/group/iphonewebdev/browse_thread/thread/7e21f809ceedc524
I think you will be interested in many of the other discoveries we are making in that community. Hope to hear from you there, or see you at the free barcamp-style http://www.iPhoneDevCamp.com conference this upcoming Friday in SF!
mentioned by Christopher Allen on July 2, 2007 3:17 pm | Permalink
FYI, the multiple background technique demonstrated at http://www.decaffeinated.org/archives/projects/multibg/background-image.html in your section 7 works great on the iPhone.
Still wondering what in heck you are doing to crash safari
reasonded by Christopher Allen on July 2, 2007 3:26 pm | Permalink
Thanks for all the info, Chris. Is it still crashing on section 7? It could be the <pre><code>.
revealed by Ankur on July 2, 2007 11:41 pm | Permalink
Doing more of a zoom-pinch to make the screen nice and big, I scrolled down slowly to near the bottom of 7 with no problems, but sometime early in 8 it still crashes. So I expect that it is something different in that section.
determined by Chstopher Allen on July 3, 2007 1:03 am | Permalink
On #4: I can still start selecting above the ‘protected’ paragraph and select all of it in one go. So it’s a bit hard to see what the point of that would be.
stated by ssp on July 5, 2007 7:30 pm | Permalink
True. From what I’ve heard, the iPhone apparently doesn’t support selecting text anyway.
stated by Ankur on July 5, 2007 10:26 pm | Permalink
Hi Ankur,
Very helpful page. Much appreciated.
Please take a look at a post I put together detailing how I was able to use the ‘-webkit-border-image’ property to create iPhone buttons that horizontally scale with the use of only one image. I’m fairly confident this was not the way the border-image tag was intended to be used, but it works nonetheless. Let me know your thoughts.
http://www.launchpadhq.com/blog/2007/07/07/replicating-iphone-buttons-the-webkit-way/
Thanks again,
Matthew
posted by Matthew Krivanek on July 8, 2007 6:03 am | Permalink
Matthew, I think that’s exactly how border-image is meant to be used. Using -webkit-border-image means you only need one element rather than four to six and the buttons are flexible.
If I had an iPhone (sigh), I think I would have done exactly the same thing.
revealed by Ankur on July 12, 2007 3:37 am | Permalink
it will be excellent to see some of these features supported in FFX as well, especially resizeability, image borders, and sliders.
declared by John Manoogian III on August 13, 2007 5:32 am | Permalink
I think the crashes are happening on the RESIZE PROPERTY - try visiting this page with your iPhone (a CSS3.info link) and you’ll also get a crash…
http://www.css3.info/preview/resize/
Apple’s gotta fix this bug - it’s a bad one.
stated by John Morris on November 2, 2007 4:46 am | Permalink
About Example 4 : Selection (Highlighting): Just replace ‘ignore’ by ‘none’. It works with -webkit-user-select: none; with lastest webkit nightly.
mentioned by Dimitri on November 18, 2007 6:22 am | Permalink
iPhone Web Developer Tool
http://www.manifestinteractive.com/iphone/
This application allows you to use your iPhone for Web Developing. You can View Source, Find on Page, Outline Divs & Tables, etc. Similar to the Firefox Web Developer Extension. Simply drag bookmarks into the web browser that you have your iPhone or iPod Touch synced to and you’re good to go. Works on any site.
spoken by Peter Schmalfeldt on November 21, 2007 2:42 pm | Permalink
about css parameters
http://css-lessons.ucoz.com/css-parameters.htm
composed by xxcemil on June 1, 2008 7:26 am | Permalink
HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
disclosed by asp-dersi on June 13, 2008 4:24 am | Permalink
-webkit-user-select: ignore; seems not to work.
-webkit-user-select: none; seems to work quite well.
And it’s what Apple uses in some of its web apps.
posted by Matt Patenaude on June 30, 2008 4:41 pm | Permalink
With the iPhone 2.0 release my web apps “broke” in that my textfields now bring up the standard keyboard instead of the numeric even though the field names contain the text “phone.” I suspect this feature was deprecated but then I have to ask how is one SUPPOSED to do this. I haven’t been able to find it in the docs.
Any help would be appreciated.
announced by Don Babcock on July 15, 2008 12:22 am | Permalink
Thanks, this is a reference I’ve bookmarked. Should come in handy later on.
announced by Michael meyer on October 24, 2008 4:25 am | Permalink
Thank you. I will try all the sections when i will make app.
revealed by vishwa on June 29, 2009 4:47 pm | Permalink
And, if we have a 50 year mission, all of Mozilla needs to be looking over the horizon in this way. ,
recorded by Stinky80 on October 22, 2009 9:30 pm | Permalink
Pages: « 1 [2] Show All
Leave a comment