Screen resolution nowsaday ranges from 320px (iPhone) to 2560px (large monitor) or even higher. Users no longer just browse the web with desktop computers. Users now use mobile phones, small notebooks, tablet devices such as iPad or Playbook to access the web. So the traditional fixed width design doesn't work any more. Web design needs to be adaptive. The layout needs to be automatically adjusted to fit all display resolution and devices. This tutorial will show you how to create a cross-browser responsive design with HTML5 & CSS3 media queries. See It in Action FirstBefore you start, check the final demo to see how it looks like. Resize your browser to see how the layout automatically flows based on the width of the viewport (browser viewing area). More ExamplesIf you want to see more examples, check out the following WordPress themes that I designed with media queries: Tisa, Elemin, Suco, iTheme2, Funki, Minblr, and Wumblr. OverviewThe page's container has a width of 980px which is optimized for any resolution wider than 1024px. Media query is used to check if the viewport is narrower than 980px, then the layout will turn to fluid width instead of fixed width. If the viewport is narrower than 650px, it expands the content container and sidebar to fullwidth to form a single column layout. HTML CodeI'm not going to go through the details of the HTML code. Below is the main structure of the layout. I have a "pagewrap" container that wraps the "header", "content", "sidebar", and "footer" together.
HTML5.jsNote that I use HTML5 markup in my demo. Internet Explorer prior than version 9 doesn't support the new elements introduced in HTML5 such as <header>, <article>, <footer>, <figure>, etc. Including the html5.js Javscript file in the HTML document will enable IE to acknowledge the new elements.
CSSReset HTML5 Elements to BlockThe following CSS will reset the HTML5 elements (article, aside, figure, header, footer, etc.) to block element.
Main Structure CSSAgain, I'm not going to get into the details. The main "pagewrap" container is 980px wide. Header has a fixed height 160px. The "content" container is 600px wide floated left. The "sidebar" content is 280px wide floated right.
Step 1 DemoHere is the design demo. Note that the media queries haven't been implemented yet. Resize the browser window and you should see that the layout is not scalable. CSS3 Media Query StuffsNow here comes the fun part — media queries. Include Media Queries JavascriptInternet Explorer 8 or older versions doesn't support CSS3 media queries. You can enable it by adding the css3-mediaqueries.js Javascript file.
Include Media Queries CSSCreate a new stylesheet for the media queries. Check out my previous tutorial to see how media queries work.
Viewport Smaller Than 980px (Fluid Layout)For viewport narrower than 980px, the following rules will apply:
Tips: use percentage (%) value to make the containers fluid.
Viewport Smaller Than 650px (One-Column Layout)Next I have another set of CSS for viewport narrower than 650px:
Viewport Smaller Than 480pxThe following CSS will apply if the viewport is narrower than 480px which is the width of the iPhone screen in landscape orientation.
Flexible ImagesTo make the images flexible, simply add
Flexible Embedded VideosTo make the embedded videos flexible, use the same trick as mentioned above. For unknown reason,
Initial Scale Meta Tag (iPhone)By default, iPhone Safari shrinks HTML pages to fit into the iPhone screen. The following meta tag tells iPhone Safari to use the width of the device as the width of the viewport and disable the initial scale.
Final DemoView the final demo and resize your browser window to see the media queries in action. Don't forget to check the demo with the iPhone, iPad, Blackberry (newer versions), and Android phones to see the mobile version. Summaries
Updates |
|
來自: 命運之輪 > 《UI、UX、UED》