Standard W3C box model and IE box model CSS layout classic box model)

Source: Internet
Author: User
The box model is an important concept in CSS. Only by understanding the box model can the layout be better. In fact, there are two types of box models: the IE box model and the standard W3C box model. Their interpretation of the box model is different. Let's take a look at the well-known standard box model:

We can see that the range of the standard W3C box model includes margin, border, padding, and content, and the Content Part does not contain other parts.

IE Box Model

 

 

We can see that the IE box model also has the range of margin, border, padding, and content. Unlike the standard W3C box model, the content part of the IE box model contains border and pading.

For example, the margin of a box is 20px, the border is 1px, the padding is 10px, the content is 200px, and the height is 50px. If we use the standard W3C box model, the location of the box is: width: 20*2 + 1*2 + 10*2 + 200 = 262px, height: 20*2 + 1*2*10*2 + 50 = pixel PX, the actual size of the box is: width: 1X2 + 10x2 + 200 = 222px; Height: 1X2 + 10x2 + 50 = 72px; if the IE box model is used, the location of the box is 20*2 + 200 = 240px, 20*2 + 50 = 70px, and the actual size of the box is Px in width and 50px in height.

Which box model should be selected? Of course it's the standard W3C box model. How can we choose the "Standard W3C Box Model? Simply add the doctype statement to the top of the webpage. If the doctype statement is not added, Each browser will understand the webpage based on its own behavior, that is, ie will use the IE box model to explain your box, FF will use the standard W3C box model to explain your box, so the web page will be displayed differently in different browsers. Otherwise, if the doctype statement is added, all browsers will use the standard W3C box model to explain your box, and the web page will be consistent in each browser.

Let's use the jquery example to confirm it.

Code 1:

<HTML>
<Head>
<Title> what is the box model you use? </Title>
<Script language = "JavaScript" src = "jquery. Min. js"> </SCRIPT>
<Script language = "JavaScript">
VaR sbox = $. boxmodel? "Standard W3C": "ie ";
Document. Write ("your page currently supports:" + sbox + "Box Model ");
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

The preceding Code does not contain the doctype statement. The "ie Box Model" is displayed in the IE browser, and the "Standard W3C Box Model" is displayed in the FF browser ".

Code 2:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<HTML>
<Head>
<Title> the box model you use is a standard W3C Box Model </title>
<Script language = "JavaScript" src = "jquery. Min. js"> </SCRIPT>
<Script language = "JavaScript">
VaR sbox = $. boxmodel? "Standard W3C": "ie ";
Document. Write ("your page currently supports:" + sbox + "Box Model ");
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

The only difference between Code 2 and code 1 is that the doctype declaration is added at the top. The standard W3C box model is displayed in all browsers ".

So in order to make the webpage compatible with various browsers, let's use the standard W3C box model.

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.