Practical Tips-3 simple steps to turn your site into a reactive form

Source: Internet
Author: User

Original: http://www.cnblogs.com/lhb25/archive/2013/03/06/making-a-website-responsive-in-3-easy-steps.html

--Layout

When creating a responsive site, or making an existing Web site responsive, the first thing to focus on is the layout of the elements. I am building a responsive site, always first to create a non-responsive layout, the page width fixed size. If the non-responsive version is done very well, I'll add the media query (mediumQueries) and the Responsive Code. This mode of operation makes it easier to implement responsive features and focus on a single task at the same time.

When you have completed the unresponsive site, the first thing to do is to paste the following code between the HTML page. This will set the screen to a size of 1:1, which provides a full view of the website in the browser of the IPhone and other smartphones, and prevents users from zooming in on the page.

1 <Metaname= "Viewport"content= "Width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">2 <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge,chrome=1">3 <Metaname= "handheldfriendly"content= "true"> 

Now it's time to add some media queries. According to the web site, media queries consist of media types and conditional expressions for 0 or more media queries. By using media queries, skin rendering can target output devices in a specific range without changing the content itself. In other words, media queries make your site look good on a variety of displays, from small smartphones to big computer screens and so on.

Media queries depend on the layout of your site, so it's difficult for me to provide you with a code snippet that you can use out of the box. However, the following code is a good starting point for most Web sites. In this example, #primary is the main content area, #secondary is the sidebar.

As you can see from the code, I've defined two specs: First there is a maximum width of 1060px, optimized for the horizontal display of the Tablet PC. The #primary occupies the 67% of its parent container width, #senondary takes up 30%, plus 3% of the left margin. The second specification is for tablets and smaller screen sizes.

Because of the small screen size of the smartphone, I decided to set the width of the #primary to 100%, #secondary also set the width of 100%, he will be under the #primary. As I have already said, you may have to modify this code bit to accommodate the specific needs of your site.

1  /*Tablet Landscape*/2 @media screen and (max-width:1060px)3{4 #primary {width:67%; }5 #secondary{width:30%;Margin-left:3%;}6 }7 /*tabled Portrait*/8 @media screen and (max-width:768px)9{Ten #primary {width:100%; } One #secondary{width:100%;margin:0;Border:None; } A  }

When you're done, let's see how your layout responds. To do this, I used this Matt Kersley to create a very responsive test tool.

Media

A responsive layout is the first step in implementing a responsive web site. Now, let's focus on the very important aspect of another modern website: media, such as video or images. The following CSS code will ensure that your images will never be larger than their parent container, the code is very simple and works for most Web sites. Please note that max-width directives are not supported by older browsers such as IE6.

1{ max-width: 100%;}

While the above techniques are effective, sometimes you may need to have more control over the image, such as displaying different images based on the client's display size.

This is a good method invented by Nicolas Gallagher. Let's look at the HTML:

1 <  src= "Image.jpg"  data-src-600px= "image-600px.jpg"  data-src-800px  = "image-800px.jpg"  alt= "">

As you can see, we use the Data-* property to store the URL of the replacement image. Now, let's use the powerful CSS3 to specify a replacement image for the media that matches the min-device-width condition:

1 @media (min-device-width:600px)2{3 img[data-src-600px]4     {5 content:attr (data-src-600px, URL);6}7 }8 @media (min-device-width:800px)9{Ten img[data-src-800px] One     { A content:attr (data-src-800px, URL); -} - }

Impressive, isn't it? Now, let's look at another media that is very important in today's website-video. Since most websites use videos from third-party websites, I decided to focus on Nick La's elastic video technology, which allows you to embed responsive videos.

Html:

1 <Divclass= "Video-container">2     <iframesrc= "Http://player.vimeo.com/video/6284199?title=0&byline=0&portrait=0"width= "$"Height= "The "frameborder= "0"></iframe>3 </Div>

Css:

1 . Video-container2{3 position:relative;4 Padding-bottom:56.25%;5 Padding-top:30px;6 Height:0;7 Overflow:Hidden;8}9 . Video-container iframe,Ten . Video-container object, One . Video-container Embed A{ - position:Absolute; - Top:0; the  Left:0; - width:100%; - Height:100%; -}

After applying the code on your website, the embedded video is also responsive (responsive).

3– fonts

The final step of this tutorial is absolutely important, but often overlooked by web developers-fonts. Up to now, most developers, including myself, have used the image to define the size of the font. Although pixels are OK for normal site use, there should be a responsive font for responsive web sites. In fact, a responsive font size should be associated with the width of its parent container so that it adapts to the client's screen.

  The CSS3 specification introduces a new unit called REM, similar to the EM class, but REM is easier to use relative to HTML elements.

REM is relative to the HTML element, and don't forget to reset the HTML font size:

1{ font-size:100%

When you are finished, you can define the font size of the response, as follows:

1{ body {font-size:1rem;}  2{ body {font-size:1.2rem;}  3{ body {font-size:1.5rem;}  }

Note that the old browser does not support REM units, so do not forget to implement an alternative.

Practical Tips-3 simple steps to turn your site into a reactive form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.