Targeting mobile-optimized CSS at Windows Phone 7


There have been several posts on how to style sites for mobile devices.  These approaches all use CSS3 Media Queries.  As many of the articles point out, not all mobile browsers support this feature.  The Windows Phone 7  browser’s base functionality is built on Internet Explorer, which does not support CSS3 Media Queries until Internet Explorer 9.  The browser team’s primary goal in the initial release of Windows Phone 7 was to ensure any site designed for Internet Explorer would render correctly and then add optimizations where we could to enable mobile scenarios.  Some examples of these additional features can be seen in Joe’s post.

Getting reacquainted with an old friend: IE Conditional Comments

Conditional Comments have been part of Internet Explorer since version 5.  They enable developers to include or exclude lines in the HTML document based on a features existence or version.  On Windows Phone 7 we have added a "IEMobile" feature.  Its version information matches the mobile token in our user agent string introduced here.  

<!–[if IEMobile]>
<p>Welcome to Internet Explorer Mobile.</p>
<![endif]—>
<![if !IEMobile]>
<p>All other browsers</p>
<![endif]>

The first block will display "Welcome to Internet Explorer Mobile" only on Windows Phone 7 devices.  The second block will display "All other browsers" on Internet Explorer, Firefox, Webkit, or Opera. This is because the second block is a downlevel-revealed comment, instead of the more common downlevel-hidden type.  More information on conditional comments is available on MSDN.

In the example above, the conditional comments surround HTML content.  While it is possible to use conditional comments on HTML, script, or CSS the remainder of this post will focus on the CSS case.  

Mixing it up with queries and comments

Add a couple lines where you include your mobile style sheet to add Windows Phone 7 support to your site.

<!—Mobile –>
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen and (max-device-width: 480px)" />
<!–[if IEMobile 7]>
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen"  />
<![endif]–>

A caveat to this method is that some of your mobile webkit CSS might not work on Internet Explorer and need to be tweaked.  Also this does not provide a mechanism to support import style media queries like below since conditional comments are only supported in the HTML markup not in CSS files.

@media screen and (max-device-width: 480px) { div {border:none;}}

One advantage of this approach is that unlike media queries, which download all the referenced style sheets even if the criteria is not met, conditional comments are part of the parsing stage and anything excluded from parsing will not be downloaded later.

Adding a conditional comment for Windows Phone 7 to a solution such as the one proposed in "Bulletproof CSS3 Media queries" would enable you to target Webkit based smartphone browsers, Windows Phone 7, and a large range of desktop browsers.

Mike O’Malley
Program Manager
Windows Phone


Comments (18)

  1. Alex Grande says:

    I can't wait for you guys to be using Webkit so my app will work in your phone. WP7 is really slick, but needs webkit.

  2. I want to like windows phone but.. says:

    Many people are in search of a cross platform mobile solution. It seems like HTML 5 is the answer to this issue since it works on all modern mobile devices (even Balckberry). All except for Windows Phone 7. This is coming fast and unless MS wants to get caught flat footed (again) you really need to have this in your pipeline.

  3. Mihir says:

    Looks like Windows phone 7 Emulator is not in sync with the real phone. The web app. I am building for mobile phones looks perfect on all phones and even on MS phone  emulator.

    Surprise: When I look it on real windows phone. It looks completely different and chaotic.

    Finally, RIM has done it all perfectly for BBs. I Hope MS will soon start using webkit too.

    It's much easier for developers.

    So, @MS please go for webkit.

    Cheers!

  4. Mark says:

    @Alex Grande and Mihir

    I don't understand why you guys want Microsoft to adopt WebKit. Trident 5.0 (IE9's layout engine) renders 99% of the web correctly without workaround. All Microsoft has to do is port it to Windows Phone 7. The Windows Phone team also have more experience with Trident than Webkit so it's not just a matter of pride.

  5. Matt Stevens says:

    Thanks for posting this, it's good to know this option is available.

    Can you share any of your roadmap for IE Mobile? Are you planning to move the base to IE 9 or bring features like media queries back into Mobile? I've been very impressed by Windows Phone but the browser capabilities do seem a bit dated compared to the competition.

  6. HadesMR says:

    WebKit is just as flawed as any other engine. You have even bigger problems when you consider that it is very fragmented and you can't expect that two "WebKits" work the same…

    I would have been happy if IE9 would have been ported to WP7.

    @2nd comment: HTML5 has nothing to do with that. The web as a common denominator is important, but it doesn't really matter if it is HTML5 or 4. The problem is that not two browsers are the same…

  7. iemoblog says:

    @Matt, @HadesMR: We haven't yet made any announcements on IE9 for WP, but we are always working to improve our browser experience. Thanks for the comments, we do appreciate the feedback.

  8. Developr says:

    This is the main reason why I've switched back to my Nexus One from my HTC 7 Mozart… the browser just wouldn't render sites correctly. Hopefully trident or webkit adoption is taken up sharpish so that I can go back to playing Alphajax soon…

  9. Rama Krishna Vavilala says:

    I was looking to handle touch events in JavaScript. It looks like they are not supported yet. Is it in the works?

  10. Typhoon87 says:

    This is not so much a developer question but will IE for Windows Phone 7 be updated so that the address bar can be accesed when the phone is in landscape mode? This is very annoying to have to flip the phone back to portrait to  type into the address bar.

  11. iemoblog says:

    @Rama: they are not supported yet. We're taking a look at them.

    @Tom: we've heard this feedback, we're always considering ways to improve the UX. No definite decisions yet.

  12. corey says:

    is there anywhere i can get a supported list of CSS and javascript elements that are supported on IE Mobile?

  13. iemoblog says:

    corey: IE Mobile 7 is essentially the rendering engine of IE7 with some scripting enhancements from IE8, so it supports the same CSS that IE7 does. We also added support for getElementsByClassName and the querySelector and querySelectorAll JavaScript APIs.

    You can also check the last few pages of: http://bit.ly/giUNXg

  14. Matt says:

    Hello, why do you have used the IE7 engine in your Windows Phone? I don't understand why you didn't have used a new rendering engine like Webkit, to propose an enhanced web experience with your mobile…

    You are a break for the web development, because in the past, with Internet 6 (horrible) and IE7, and with Windows Phone today, it is very difficult to create innovating web applications…

  15. Mark says:

    @Matt

    Microsoft started working on Windows Phone 7 in a time IE8 was still in beta, WebKit was still in its infancy and Google Chrome doesn't yet exist: that's why.

  16. Thomas says:

    With IE8 and IE9, finally there was no more need for conditional comments.

    But now they are back 🙁

    A switch to webkit would be the best choice. Its open source, mobile proofed, and much easier to implement than to add all the IE9 features to mobile IE.

    And with the right PR, this doesn't state "we suck in creating a good mobile browser" but "we support open source and listen to the (developer) community"

  17. David says:

    Awesome.  So Microsoft is using a browser that's already almost 2 years out of date and has well-known issues in their current mobile OS?  Good choice.  Looks like my IE 7 hacks aren't going anywhere.

  18. Adam says:

    To be honest I think it's ridiculous that you guys have the the Win7 phone browser in the state its in, especially with all the other devices. If it wasn't enough to continue to be the bain of the desktop world you want to drop your grubby mits into mobile devices. IE for win7 phone is absolutely awful. Whether that be with media queries, or lack of decent anti-aliasing on hi-res down-scaled imagery for larger resolution displays, to just generally messing up layouts with the finnesse of IE6.

    I can test my layouts successfully on BB, Android, and iOS to great success but IE as always presents unforgivable and irritating problems that should not be occuring on such a new device.

    Sort it out or get out.