顯示具有 Web 標籤的文章。 顯示所有文章
顯示具有 Web 標籤的文章。 顯示所有文章

2013年1月3日 星期四

Different Stylesheets for Differently Sized Browser Windows


Otherwise known as "resolution dependent layouts". Single website, different CSS files for rearranging a website to take advantage of the size available.
There is a W3C standard way of declaring them. One way is to test the "device-width", like this:
<link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" />
The above code will apply the 800.css styling to the document only if the device viewing it has a width of 800px or wider. And... supports "media queries" in this way.
Keep in mind this is the device width, not the current width of the browser window. On the iPhone (3G(s)), that means 480px. My fancy new MacBook Pro is going to return 1920px for the device width. But my actual browser window is only half of that at this exact moment. The device width is quite useful when dealing with mobile devices where the browser is probably 100% of the screen whenever in use, but less useful in laptop/desktop browsers.
Seems to me far more useful is the current width of the browser window ("viewport"). We can even specify stylesheets that are only to be used when the viewport is between two different pixel sizes:
<link rel='stylesheet' media='screen and (min-width: 701px) and (max-width: 900px)' href='css/medium.css' />
That stylesheet will only take affect when the current browser window is between 701 and 900 pixels in width.

Browser Support

IE 9+, Firefox 3.6+, Safari 3+, Any Chrome, Opera 10+. Mozilla suggests starting the media attribute of the <link /> with "only" which will hide the stylesheet from older browsers that don't support media queries. That may or may not be what you actually want to do... case dependent of course.
<link rel="stylesheet" media="only screen and (color)" href="example.css" />
Generally, layout is one of those things that it's tough to sell "progressive enhancement" on. Rounded corners becoming squares, no big deal. Messed up layout, that is a big deal. Devices like the iPhone right now are kind of great, since browser choice is so limited (there used to be just Mobile Safari and that's it, now there is Opera as well) and they are very good browsers, you can rely on techniques like this to work.
If IE support is paramount, we always have JavaScript!

Doing it with jQuery

Using JavaScript, we can test the windows width and change the active CSS file accordingly. This will work across all browsers. You can have an ID for a <link /> element like any other, so let's add that:
<link rel="stylesheet" type="text/css" href="main.css" />
<link id="size-stylesheet" rel="stylesheet" type="text/css" href="narrow.css" />
Then we can use that as a hook and change the href value of the stylesheet. The browser will see that change and unapply the old CSS and reapply the newly linked CSS. We'll run our little adjustment test once right away, and then anytime the window is resized thereafter.
function adjustStyle(width) {
    width = parseInt(width);
    if (width < 701) {
        $("#size-stylesheet").attr("href", "css/narrow.css");
    } else if ((width >= 701) && (width < 900)) {
        $("#size-stylesheet").attr("href", "css/medium.css");
    } else {
       $("#size-stylesheet").attr("href", "css/wide.css"); 
    }
}

$(function() {
    adjustStyle($(this).width());
    $(window).resize(function() {
        adjustStyle($(this).width());
    });
});

Is jQuery really necessary just for this?

No, but I'm sure you know that's just how I roll in general and it makes it easier. Kevin Hale wrote about dynamic resolution layouts literally five years ago. It's "raw" JavaScript and still works great today.
Also, there is a kick ass polyfill: Respond.js

Example One
This one has a special stylesheet for narrow (less than 700px), medium (701 - 900px), and wide (greater than 901px) browser windows.

Example Two

This does the exact same thing as example one, only through jQuery/JavaScript instead of CSS media queries.

Example Three

This example shows that we can target mobile devices by testing for a maximum device width.

Download

Snag all of these for reference in one place.

2012年12月5日 星期三

Web Development for Android / iPhone Devices: Mobile Joomla!


While many developers are actively working on an Android app for their client or business, others are going the web app route by creating websites that look and act like apps when viewed on mobile clients. If you're already using the Joomla! CMS, the Mobile Joomla extension will do most of the work for you, as we will show you in this tutorial.
Mobile Joomla is a free extension that works like this: if you visit a Mobile Joomla-ready website using your desktop web browser, you will see the site like you always do--a normal, standard Joomla! site. If, however, you are using an Android or WAP device, iPhone or iPad, you will see the same website appropriately displayed for your device. It allows you to redirect mobile users to a specific subdomain, or it can use a specific theme for each device. That allows Android users to see one thing, iPhone users another, and Windows Phone 7 users to see something altogether.
You can see it in action by checking out my own test site which I created as I wrote this article,AndroidWho. View it in as many devices as you have access to, and you will see an immediate difference (or, if you are using an Android device running the Dolphin HD browser, you can change the user agent to Android, Desktop, iPhone or iPad, and check it out that way).

Setting Up My Test Site: AndroidWho.com

For this tutorial, I created a site from scratch. I bought the domain name AndroidWho.com, and set it up with my webhost, 1and1.com. Once I had pointed the DNS records to 1and1.com, and created a home directory for the new domain, I created a new MySQL database to use with a fresh Joomla! 1.6 installation.
I won't bore you with the Joomla! installation details, as we have already covered the topic in detail here on HTMLGoodies. Suffice it to say, once you have created the MySQL database, it's pretty much plug and play (or fill in the blanks, more precisely). Here's what my fresh Joomla! installation looked like when it was installed on my host (and yes, I did install the sample data that's included):
Fresh Joomla Install
Since I am not the type of person who can just leave things in their default status, I looked around and found a Joomla! theme that looked more like something an Android nut like myself would like. It's called Modern Design and was available for both Joomla! 1.5 and 1.6, so I was set.
Again, we have already discussed how to install a Joomla! theme, so I won't go over the details, but once I was done installing the theme (and assigning modules to positions within that theme, etc.), the site looked a bit better:
Modern Joomla
Now we get to the installation of Mobile Joomla itself. Mobile Joomla is free to download, and the Mobile Joomla website includes ample documentation:
Modern Joomla
As I mentioned before, I installed Joomla! 1.6, rather than the 1.7 release candidate, because the folks at Mobile Joomla are still working out the issues of upgrading to support the new Joomla! release. They have their own release candidate coming out with support for 1.7, but we'll leave that for a future article.

Mobile Joomla Installation

Before you begin the installation, make sure that your webhost has the following features or configurations:
  • PHP 5.0 (but you need PHP 5.2.4 or higher to run Joomla! 1.6)
  • MySQL 5.0
  • Joomla! 1.5 or 1.6
  • GD2, a PHP image library for image scaling
  • Zip PHP extension
  • bzip2 (PHP bz extension for TeraWURFL device database, explained below)
  • mysqli (also for TeraWURFL)
  • 100 MB of database disk space
  • at least a 64 MB PHP memory limit
Also, one part of Mobile Joomla, TeraWURFL, takes up around 25Mb in the MySQL database, so you'll need enough space for it to be installed. TeraWURFL is where the information (the user agent data from just about every mobile device out there) is stored, and it's required for Mobile Joomla to function.
While Mobile Joomla works on most web hosts, GoDaddy is known to have issues, so be forewarned. Now, let's begin.
Once you've downloaded the Mobile Joomla file itself, you just need to un-compress it (the actual file you'll be using is a .tar file), and Joomla! enables you to simply upload it as is by using the Extension Manager, as shown here:
Extension Installation
Once it's installed, you can either leave it as it is, or further customize it. Here's how that same page we showed you above appears now when viewed by someone using an iPhone:
iPhone View
And here it is on a WAP device, such as a Nokia 6630:
WAP View
Here's what it looks like on a 7 inch Android tablet, through the default browser:
Android View

Changing the Mobile Joomla Configuration

To really dig into the Mobile Joomla settings, just to to the Joomla! Administration page, Components, Mobile Joomla!, Settings, as shown below:
Mobile Joomla Settings
Once you're in the Mobile Joomla! settings, you will see an interface with 5 tabs: Global settings, Smartphone (XHTML), iPhone, WAP and iMode. On the General settings tab, you can enable or disable caching, browser caching, and subdomain support (you can point each type of mobile device to a device specific subdomain, such as iphone.androidwho.com). On the iPhone tab, you can choose to select iPad support, image scaling, and the modules that are used for the iPhone display. You can see the iPhone tab below:
iPhone Settings
There are too many options to list in this article, but suffice it to say, you can style the display for each device however you want, and completely customize your website's appearance by changing the settings. By default it works very, very well, and I am looking forward to seeing the next version of Mobile Joomla!, as it promises to support the Joomla! 1.7, with updated mobile functionality.

2012年11月29日 星期四

Responsive Web Design


Designing for many devices

Research into mobile device use on the Web shows a large gap between supply and demand. More consumers use a mobile phone than a computer to access the Web. At the same time, only nine percent of websites are actually completely mobile compatible.
Largely this is due to complexity. Designing for desktop alone is complex. When you include mobile it becomes exceedingly complex. There are many factors to take into account:
  • The number of Web enabled devices on the market increases.
  • Different devices have different capabilities.
  • Smartphone models are upgraded rapidly but older phones still need support.
  • Old browsers in corporate environments don't support recent functionality.
  • Compared to fixed data connections, mobile networks tend to be slower and often have a measurably higher latency. This can lead to long retrieval times, especially for lengthy content and navigation between pages.
  • Mobile users have different interests to users on fixed or desktop devices.
  • Mobile users have to pay for bandwidth.
Year 2011 was commonly recognized as the year of mobile commerce in Web design circles. The rapid and extensive adoption of mobile devices to access the Web really hit the mainstream. This forced the design community to come up with better ways to build websites.

Origins of Responsive Web Design

Much debate followed about best practices for designing mobile websites. Ethan Marcotte proposed that addressing browser capability, screen sizes and orientations by creating flexible, fluid and adaptive websites was the answer. This approach, known as Responsive Web Design, seemed to overcome most challenges in most circumstances.
Today, RWD is considered a best practice. Google and Bing endorse it and Google uses it on their own pages. W3C opposes the idea of creating separate mobile and desktop versions and advocates that any web page should have one set of HTML available at one URL, implicitly endorsing RWD. New articles in support of Responsive Web Design appear on the Web daily. One commentator describes it as the "new black".

Benefits

Responsive Web Design offers the following benefits:
  • You can start from an existing desktop website and adapt what already works rather than start from scratch.
  • The need to maintain multiple sites is eliminated. This can be a dramatic cost-saver.
  • All renditions of the site are available at the same URL. This is good for search engine optimization.

How Magnolia CMS uses Responsive Web Design

Magnolia CMS implements the principles of RWD in the Standard Templating Kit. Web pages rendered with the system are compliant with HTML5 and CSS3. Content is abstracted from presentation and fed from a single back-end. You can serve multiple devices without unnecessary replication.
A website built with the STK uses a flexible grid. For desktops browsers, the design adapts dynamically to the viewport. Below is the Standard Article page from the demo-project sample website at screen widths of 1200, 1024 and 768 pixels. Note how the extras and promos areas float from the top right to the bottom. This is an example of reorganizing page elements to fit the viewport.
The same reorganization takes place when the site is viewed on a smartphone. Content that does not work well on the small screen or is not of interest to smartphone users is either left out or reformatted. An iPhone user sees a minimized, modified version of the site.
Here are the two layouts side by side. Note how the navigation is pushed down in the smartphone layout, giving preference to the main area which typically has the most important content such as the story of a news item. This has the benefit that the important content is displayed "above the fold". The phone user can see it in the first screenful without having to scroll. The promos area is left out altogether in the smartphone variation. The red line is an approximate viewport size.
Tip
If you don't have a smartphone at hand, you can view the smartphone variation on a desktop browser by adding the mgnlChannel=smartphone attribute to the URL:http://demopublic.magnolia-cms.com/demo-project/about/subsection-articles/article.html?mgnlChannel=smartphone

Technology

Responsive Web Design relies on three technical ingredients to detect the requesting device's capabilities and adapt the design accordingly:
  • CSS media queries detect the type of device and the viewport size.
  • Flexible grids allow page elements to float, reformat and reorganize depending on the viewport size.
  • New variations of images are generated on-demand to fit in the available space.
These mechanisms implement the basic tenet behind Responsive Web Design: that it is better to design one website that adapts to the ever-increasing number of devices than to create many disconnected designs. Each device is treated as a facet of the same experience.

CSS media queries

CSS media queries is an extension to CSS3 that allows you to control how page elements are rendered on different devices. Media queries are always used in conjunction with media types. The media type identifies the device. The media query is a logical expression that evaluates to either true or false.
Here is an example of a media query:
only screen and (min-width: 1200px)
The query targets the media type screen which is intended for desktop browsers. The expression evaluates to true if the website is viewed with a browser where the viewport size is very large, 1200px wide or more. When true, a style sheet that optimizes the experience for a wide, cinematic screen is applied.
In Magnolia CMS, media queries are configured in a theme. The media attribute contains the media query and the link attribute the style sheet that is applied when the expression evaluates to true. Here is an example from the pop theme. Pop includes several style sheets. The wide sheet that is expanded in the screenshot show the media query and a link to the associated style sheetwide.css. Note how the link property contains just a reference to the style sheet. The actual sheet is stored in the resources workspace. You can edit it in Templating Kit > Resources.
Magnolia CMS ships with a basic collection of style sheets targeted at different media types:
  • styles is the basic style sheet that optimizes the experience for desktop browsers
  • print is intended for printed documents and the print preview mode.
  • wide is for wide screens. It displays the promos area since there is more screen real estate.
  • small is for narrow screens. The main area displays two columns of teasers instead of three.
  • ie6 is a specialized CSS only used for Internet Explorer 6.
  • ieStyles includes styles for IE7 and IE8, applied with a conditional comment.
  • mobile is for smartphones and other handheld devices, see also Device Detection
In addition to the basic set, you can create your own stylesheets. Define matching media queries if you need to render content for other types of devices such as Web-enabled TVs.
Magnolia CMS uses conditional comments to overcome the limitations of legacy browsers. The ie6 and iestyles configuration nodes have an additional attribute conditionalComment that contains a statement that is interpreted by Microsoft Internet Explorer. Magnolia CMS uses them to provide and hide code to and from Internet Explorer.
In the page HTML, the media query and the style sheet link are rendered as a link element inside the head.
<head>
   <link rel="stylesheet" type="text/css" 
      href="/demo-project/resources/templating-kit/themes/pop/css/wide.css" 
      media="only screen and (min-width: 1200px)">
</head>
Typically you will find several link elements like this on the page. The style sheets are loaded incrementally. The basic styles sheet contains a lot of style information that is used on all devices and viewports. The more specific sheets loaded later such as wide define only additions and exceptions.
Here is the viewport comparison again, this time with the style sheet name included.
Note
The mobile media query is used when the default site is rendered on a desktop browser. It is a kind of fallback. Magnolia CMS has a more advanced mechanism for tailoring the mobile experience. The Device Detection module and the channels functionality allow you to format existing content but also target unique content to mobile users. The smartphone variation uses its own theme, pop-mobile, with the media property set simply to screen. The pop-mobile theme is loaded when the User-Agent header identifies the requesting device as a smartphone.

2010年12月15日 星期三

The State of CSS3

Just because you didn’t get to go to that awesome conference doesn’t mean that you can’t still watch the lectures! Each weekend, we’ll feature a recommended web development lecture on Nettuts+.
In this recent lecture, from Fronteers 2010, Håkon examines the history of CSS3, its current status in the browsers, and what the future of CSS holds.


Håkon Wium Lie | CSS3 | Fronteers 2010 from Fronteers on Vimeo.