Source: http://bbs.html5cn.org/forum.php? MoD = viewthread & tid = 11516 & extra =
The screen resolution range is large, from 320px (iPhone) to 2560px (large display), or even greater. The user does not just use a desktop computer to access the web site. He uses mobile phones, laptops, and tablets. Therefore, the traditional website width setting is fixed and cannot meet the requirements. The web design needs to adapt to this new requirement, and the page layout needs to be automatically adjusted according to the different resolutions of the access device. This tutorial will introduce how to use HTML5 and css3.
Media queries implements a responsive design across browsers.
Demo preview: http://webdesignerwall.com/demo/adaptive-design/final.html
Demo: http://www.webdesignerwall.com/file/adaptive-design-demo.zip
First Run
Before starting, we can view
The final demo to view the final effect. Adjust the size of your browser. We can see that the page will automatically adjust the layout according to the size of the window.
More examples
You can visit the following address to view more examples:
WordPress Themes. I designed the following media queries:
Tisa,
Elemin, Suco,
Itheme2, funki,
Minblr, and wumblr.
Overview
By default, the width of the page container is 980px, which optimizes the resolution greater than 1024px. Media query is used to check the viewport width. If it is less than 980px, it will change to the narrow screen display mode, and the page layout will replace the fixed width with the flow width. If the viewport is smaller than pixel PX, it will change to Mobile Display Mode. Content, sidebar, and other content will change to a separate column layout mode, and their width will fill the screen width.
HTML code
Here, I will not introduce the details in the following HTML code. The following is the main frame of the layout page. A "pagewrap" container encapsulates "Header", "content", "sidebar", and "footer ".
- <Div id = "pagewrap">
- <Header id = "Header">
- <Hgroup>
- <H1 id = "Site-logo"> demo
- <H2 id = "Site-description"> site description </H2>
- </Hgroup>
- <Nav>
- <Ul id = "Main-nav">
- <Li> <a href = "#"> Home </a> </LI>
- </Ul>
- </Nav>
- <Form ID = "searchform">
- <Input type = "Search">
- </Form>
- </Header>
- <Div id = "content">
- <Article class = "Post">
- Blog Post
- </Article>
- </Div>
- <Aside id = "sidebar">
- <Section class = "widget">
- Widget
- </Section>
- </Aside>
- <Footer id = "footer">
- Footer
- </Footer>
- </Div>
Copy code
Html5.js
Note that I used HTML5 labels in the demo. However, ie9 does not support new HTML5 labels such as The html5.js file will solve this problem.
- <! -- [If lt ie 9]>
- <SCRIPT src = "http://html5shim.googlecode.com/svn/trunk/html5.js"> </SCRIPT>
- <! [Endif] -->
Copy code
CSS
Set HTML5 elements to block elements
The following CSS sets the HTML5 elements (article, aside, figure, header, footer, etc.) as block elements.
- Article, aside, details, figcaption, figure, footer, header, hgroup, menu, NAV, section {
- Display: block;
- }
Copy code
CSS subject structure
I will not explain the details of the CSS file here. The width of pagewrap is set to 980px. The header is set to a fixed height of 160px. The width of "content" is 600px, floating to the left. The width of "sidebar" is set to 280px, floating on the right.
- # Pagewrap {
- Width: 980px;
- Margin: 0 auto;
- }
- # Header {
- Height: 160px;
- }
- # Content {
- Width: 600px;
- Float: left;
- }
- # Sidebar {
- Width: 280px;
- Float: right;
- }
- # Footer {
- Clear: both;
- }
Copy code
Step 1 demo
You can use the demo to view the current effect. At this time, we have not used media queries to adjust the browser width, and the page layout will not change.
Css3 media Query
You can learn more through HTML5 practices-css3 media queries.
Contains the media queries Javascript file
IE8 and earlier Browsers Do not support css3 media queries, and you can add css3-mediaqueries.js to the page to solve this problem.
- <! -- [If lt ie 9]>
- <SCRIPT src = "http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"> </SCRIPT>
- <! [Endif] -->
Copy code
Contains media queries CSS
Create the CSS required for media query and add references to the page.
- <Link href = "media-queries.css" rel = "stylesheet" type = "text/CSS">
Copy code
Viewport less than 980px (flow layout)
When the viewport is smaller than 980px, the following rule is used:
- Pagewrap = set the width to 95%
- Content = width: 60%
- Sidebar = width is set to 30%
TIPS: the percentage (%) can be used to make the container unfixed.
- @ Media screen and (max-width: 980px ){
- # Pagewrap {
- Width: 95%;
- }
- # Content {
- Width: 60%;
- Padding: 3% 4%;
- }
- # Sidebar {
- Width: 30%;
- }
- # Sidebar. widget {
- Padding: 8% 7%;
- Margin-bottom: 10px;
- }
- }
Copy code
Viewport smaller than pixel PX (Single Column Layout)
When the viewport is smaller than pixel PX, the following rule is used:
- Header = set height to auto
- Searchform = reset the position of searchform 5px top
- Main-nav = set the location to static
- Site-logo = set the location to static
- Site-description = set the location to static
- Content = set the width to auto (this will change the container width to fullwidth) and get rid of floating
- Sidebar = set the width to 100% and get rid of floating
- @ Media screen and (max-width: pixel px ){
- # Header {
- Height: auto;
- }
- # Searchform {
- Position: absolute;
- Top: 5px;
- Right: 0;
- }
- # Main-nav {
- Position: static;
- }
- # Site-logo {
- Margin: 15px 100px 5px 0;
- Position: static;
- }
- # Site-description {
- Margin: 0 0 15px;
- Position: static;
- }
- # Content {
- Width: auto;
- Float: none;
- Margin: 20px 0;
- }
- # Sidebar {
- Width: 100%;
- Float: none;
- Margin: 0;
- }
- }
Copy code
Viewport less than PX
The following CSS is used to deal with screen sizes smaller than PX. This is the width of the iPhone landscape screen.
- Html = Disable text size adjustment. By default, the font size of the iPhone is increased to make it easier to read. You can use-WebKit-text-size-adjust: none; to cancel this setting.
- Main-nav = set the font size to 90%
- @ Media screen and (max-width: 480px ){
- HTML {
- -WebKit-text-size-adjust: none;
- }
- # Main-nav {
- Font-size: 90%;
- Padding: 10px 8px;
- }
- }
Copy code
Elastic Images
To make the image size more elastic, you can simply add max-width: 100% and Height: auto. This method works normally in IE7 and cannot work in IE8. You need to use width: auto \ 9 to solve this problem.
- IMG {
- Max-width: 100%;
- Height: auto;
- Width: auto \ 9;/* IE8 */
- }
Copy code
Elastically embedded video
To make the embedded video more elastic, you can also use the above method. But I don't know why. Max-width: 100% cannot work properly in Embedded resources in safari. We need to replace it with width: 100%.
- . Video embed,
- . Video object,
- . Video IFRAME {
- Width: 100%;
- Height: auto;
- }
Copy code
Initial-scale meta tag (iPhone)
By default, the iPhone's Safari browser will contract the page to adapt to its screen. The following statement tells the iPhone Safari browser to use the device width as the viewport width and disable initial-scale.
- <Meta name = "viewport" content = "width = device-width; initial-scale = 1.0">
Copy code
Final Effect
View the final demo, adjust the browser size, and view the media query behavior. You can also use the iPhone, iPad, new BlackBerry, and Android to view the effects of the modile version.
Summary
Reliable media queries Javascript
You can use css3-mediaqueries.js to solve the problem that browsers do not support media queries.
- <! -- [If lt ie 9]>
- <SCRIPT src = "http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"> </SCRIPT>
- <! [Endif] -->
Copy code
CSS media queries
This technique allows you to create an adaptive design and rewrite the CSS layout based on the width of the viewport.
- @ Media screen and (max-width: 560px ){
- # Content {
- Width: auto;
- Float: none;
- }
- # Sidebar {
- Width: 100%;
- Float: none;
- }
- }
Copy code
Elastic Images
Use max-width: 100% and Height: auto to make the image more elastic.
- IMG {
- Max-width: 100%;
- Height: auto;
- Width: auto \ 9;/* IE8 */
- }
Copy code
Flexible embedded video
Using width: 100% and Height: auto can make embedded videos more flexible.
- . Video embed,
- . Video object,
- . Video IFRAME {
- Width: 100%;
- Height: auto;
- }
Copy code
WebKit font size adjustment
Use-WebKit-text-size-adjust: none to disable font size adjustment on the iPhone.
- HTML {
- -WebKit-text-size-adjust: none;
- }
Copy code
Set iPhone viewport and initial scale
The following statement sets the viewport and in1_scale using the meta tag on the iPhone.
- <Meta name = "viewport" content = "width = device-width; initial-scale = 1.0">
Copy code
Now, the tutorial is complete.