Css determines whether different resolutions display different width la implement adaptive width and css adaptive

Source: Internet
Author: User

Css determines whether different resolutions display different width la implement adaptive width and css adaptive

1. When the resolution is smaller than or equal to PX (pixels) in the css div webpage layout, the DIV layout object will display PX width and PX width when the resolution is greater than PX. CSS is used to change the Browser display width so that the layout width of the web page changes dynamically (the web page width automatically varies with the browser display width ).
With the development, more and more computer users have a higher display resolution, but some users still use a 1200 PX display (according to the resolution statistics platform of several browsers, there are currently very few users who use less than resolution, but we still need to consider at least 1024 PX resolution users during CSS layout). If the webpage layout width is fixed to pixel PX, a scroll bar will appear below the browser when users browse the webpage at resolution. To solve this problem, you can use the CSS3 style to determine the width of your browser and call different layout widths.

Use CSS words and syntax

The Code is as follows:

@ Media screen and (Judgment attribute) {CSS style selector} note that the CSS style selector needs to be changed in curly brackets.

2. show different width styles at different resolutions

1. DIVCSS small case description
First, we set the CSS name of a DIV box to ". abc", set its height to 300px, css border to black, and set the margin: 0 auto layout to center. These two styles are pre-configured for easy observation.
We manually drag the browser to display the width, and then observe the variation of the box width. When the browser width is adjusted to a width not greater than PX, the corresponding box width is displayed PX; when the browser width is adjusted to be no greater than 901px, ". abc "indicates that the box width is PX. When the browser width is adjusted to be greater than PX, the box object width is PX. When the browser width is smaller than PX, the box object width is PX.

2. CSS code

The Code is as follows:


. Abc {height: 300px; border: 1px solid #000; margin: 0 auto}
@ Media screen and (min-width: 1201px ){
. Abc {width: pixel PX}
}
/* Css comment: When the browser width is set to not less than 1201px, abc displays the pixel PX width */

@ Media screen and (max-width: pixel px ){
. Abc {width: 900px}
}
/* If the browser width is set to not greater than pixel PX, abc will display 900px width */

@ Media screen and (max-width: 901px ){
. Abc {width: 200px ;}
}
/* If the browser width is set to not greater than 901px, the abc will display the 200px width */

@ Media screen and (max-width: 500px ){
. Abc {width: 100px ;}
}
/* If the browser width is set to not greater than 500px, abc will display 100px width */

It should be noted that the CSS code sequence, from the Big to the small typographical CSS (the larger the browser width is, the more it is placed before), this is because of the logical relationship, @ media determines that CSS troubleshooting will lead to failure of judgment.

 

3. HTML code

The Code is as follows:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> untitled document </title>
<Style>
. Abc {height: 300px; border: 1px solid #000; margin: 0 auto}
@ Media screen and (min-width: 1201px ){
. Abc {width: pixel PX}
}
/* If the browser width is set to not less than 1201px, the abc will display the pixel PX width */

@ Media screen and (max-width: pixel px ){
. Abc {width: 900px}
}
/* If the browser width is set to not greater than pixel PX, abc will display 900px width */

@ Media screen and (max-width: 900px ){
. Abc {width: 200px ;}
}
/* If the browser width is set to not greater than 900px, the abc will display the 200px width */

@ Media screen and (max-width: 500px ){
. Abc {width: 100px ;}
}
/* If the browser width is set to not greater than 500px, abc will display 100px width */

</Style>
</Head>
<Body>
<Div class = "abc"> DIVCSS5: the width of the DIV varies with the browser width. Try to change the browser width. </div>
</Body>
</Html>
 

4. To be compatible with browsers earlier than IE9, you need to add a google JS file. Of course, you can download

The Code is as follows:
<! -- [If lt IE 9]>
<Script src = "http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"> </script>
<! [Endif] -->

Put the JS Code in front of the 5. Perfect compatibility with HTML + CSS + JS source code of various browsers

The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> untitled document </title>
<Style>
. Abc {height: 300px; border: 1px solid #000; margin: 0 auto}
@ Media screen and (min-width: 1201px ){
. Abc {width: pixel PX}
}
/* Css Note: When the browser width is set to not less than 1201px, abc displays the pixel PX width */

@ Media screen and (max-width: pixel px ){
. Abc {width: 900px}
}
/* If the browser width is set to not greater than pixel PX, abc will display 900px width */

@ Media screen and (max-width: 900px ){
. Abc {width: 200px ;}
}
/* If the browser width is set to not greater than 900px, the abc will display the 200px width */

@ Media screen and (max-width: 500px ){
. Abc {width: 100px ;}
}
/* If the browser width is set to not greater than 500px, abc will display 100px width */

</Style>
<! -- [If lt IE 9]>
<Script src = "http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"> </script>
<! [Endif] -->
</Head>
<Body>
<Div class = "abc"> DIVCSS5: the width of the DIV varies with the browser width. Try to change the browser width. </div>
</Body>
</Html>

 

 

 

 

Related Article

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.