Web page Width Height adaptive size

Source: Internet
Author: User

Today, the resolution of the display more and more, the terminal has become diversified, web development page Adaptive problems more and more, if not done, once the resolution of the display changes, the content of the display may appear many unexpected typesetting problems. The problem with presentation of different terminals can be achieved through a responsive layout, without the need for a responsive layout when we need to avoid these problems.

Width Adaptive:
1, set the outermost container (such as DIV) width is 100%;
2, if the site has a picture of the head, it is not easy to set the width of 100%, the situation will appear repeat, or the picture size beyond the outermost container width, you can set the outermost container width is fixed value: 960px, 980px, 1000px, and set margin:0 auto;
3, if it is found that the larger resolution of the display 960px width is not generous and beautiful, you can set different width according to different resolutions (the same to set margin:0 Auto), and make the corresponding size of the picture replacement can:

   1:  $ (function() {
   2:      var screenwidth = window.screen.width;    
   3:      var width;
   4:      var imgurl;
   5:      if (screenwidth >= 1440) {
   6:          "1400px";
   7:          "1400.png";
   8:      Elseif (1024x768 < screenwidth && ScreenWidth < 1440) {
   9:          "1200px";
  Ten:          "1200.png";
One   :      else {
  :          "980px";
  :          "980.png";
  :      }
  :      $obj. CSS ("width", width);  //$obj The jquery object to set the width
  :      $img. CSS ("BackgroundImage","url (")");  //$img The jquery object to set the background
  :  })

Highly adaptive:
1, can directly set the outermost container and HTML, body height of 100%;
2, sometimes, the content of the Web page will be different depending on the permissions or the full extent of the data display is not the same size, so that we need to compare the size of the page and the resolution height of the monitor to make corresponding adjustments:

   1:  function autoheight (objId) {
   2:      var nowheight;
   3:      if (window.innerheight) {//ff
   4:          nowheight = window.innerheight;
   5:      }else{
   6:          nowheight = document.documentElement.clientHeight;
   7:      }
   8:      if(Nowheight > Document.body.clientHeight) {
   9:          document.getElementById (objId). Style.height = nowheight  ' px ';
  :      }else{
One   :          ' px ';
  :      }
  :  }

3, if the page has a footer (copyright information, etc.), the use of the method of 2 may make the footer hanging in the middle of the page, at this time, the page will often be header, main, footer such a structure, there will be an outer container container, Method 2 is to set the height of the outer container. Of course, we can set the height of the main by subtracting the height of the header and footer after acquiring the new height of the container, which avoids the situation where the footer appears in the middle of the page.
In addition, we have another way to solve the problem of footer: position.
Set the position:relative of container, Main and footer Position:absolute (the remaining CSS properties omitted):

   1:  #container {
   2:    position:relative;  
   3:  }
   4:  
   5:  #main {
   6:    Position:absolute;
   7:    top:80px;    Height of/*header */
   8:    bottom:40px;      Height of/*footer */
   9:  
  Ten:  
One   :  #footer {
  :    Position:absolute;
  :    bottom:0;
  :  }

This combination of the above width to set, found that after setting the position, margin:0 Auto is invalid, because the reason for leaving the flow of the document, so we need to set container margin-left is half the width of the negative value, namely:

   1:  $ (function() {
   2:      var screenwidth = window.screen.width;    
   3:      var width;
   4:      var imgurl;
   5:      var left;
   6:      if (screenwidth >= 1440) {
   7:          "1400px";
   8:          " -700px";
   9:          "1400.png";
  :      Elseif (1024x768 < screenwidth && ScreenWidth < 1440) {
One   :          "1200px";
  :          " -600px";
  :          "1200.png";
  :      else {
  :          "980px";
  :          " -490px";
  :          "980.png";
  :      }
  :      $obj. css ({"width": width,"Margin-left": Left});  //$obj The jquery object to set the width
  :      $img. CSS ("BackgroundImage","url (")");  //$img The jquery object to set the background
  :  })

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.