Forwarding XHTML and DOCTYPE switching (MSDN)

Source: Internet
Author: User

UseWebStandard generationASP. 2.0 WebSite

XHTML and DOCTYPE Switch

Specifying DOCTYPE for a Web page affects how the browser renders the page. Internet Explorer, Mozilla Firefox, and Opera all support a feature called "DOCTYPE Switching" (also known as "DOCTYPE sniffing").

The purpose of introducing the DOCTYPE switch is to enable the browser to properly render a standard-compliant web site and a legacy Web site. Most WEB sites are developed to render HTML pages instead of XHTML pages. The browser determines when the page should be rendered using criteria by judging whether there is a DOCTYPE.

Internet Explorer 6+ supports two rendering modes, called Quirks mode and Standards mode, respectively. When Internet Explorer renders a page that contains a valid XHTML (or HTML 4.0) DOCTYPE, it renders the page in standards mode, otherwise it renders the page in quirks mode (for more information, see CSS Enhancemen TS in Internet Explorer 6).

Opera 7+ supports two rendering modes, the same as Internet Explorer: Quirks and standards (for more information, see http://www.opera.com/docs/specs/ doctype/).

Mozilla Firefox 1+ supports three rendering modes: Quirks mode, almost standards mode, and standards mode. Firefox's almost standards mode corresponds to Internet Explorer and Opera's standards mode. When a page contains a valid XHTML 1.0 transitional DOCTYPE (and the page is assigned a text/html MIME type), Firefox renders the page in almost standards mode. When a page contains XHTML 1.0 Strict or XHTML 1.1 DOCTYPE (or the page is assigned as an XML MIME type), the page is rendered in standards mode (for more information, see Http://www.mozilla.org/docs /web-developer/quirks/doctypes.html).

You can determine the current rendering mode of the browser by temporarily adding the following client script to the page, which is valid in the latest version of Internet Explorer, Firefox, and Opera.

<script type= "Text/javascript" > Alert (Document.compatmode); </script>

You need to be concerned about the rendering mode of the browser, because it affects how cascading style sheets are applied to the page. If you convert an existing HTML page to an XHTML page, they may look very different when you open them in a browser.

For example, Internet Explorer calculates the size of page elements in different ways, depending on the rendering mode (which uses a different CSS Box model). In quirks mode, the width of an element is calculated by adding the content, padding, borders, and margins of the element. In standards mode, the width of an element is calculated only by considering the width of the element's content.

For example, consider the following two <div> tags.

<div style= "Width:400px;border:solid 1px black" >

First Box

</div>

<div style= "width:400px;border:solid 1px black;padding:10px" >

Second Box

</div>

These two <div> elements are the same except for the second <div> element's additional padding. In Quirks mode (see Figure 2), these two <div> elements look the same size because the second <div> The width of the element takes into account its additional padding (the total width of these two elements is 400px). In standards mode (see Figure 3), the second <div> element looks wider than the first <div> element because the padding is not considered when the width of the element is calculated (the total width of the two elements is greater than 400px).

Figure 2. Quirks mode

Figure 3. Standards Mode

This is just an example of browser differences in Quirks mode. In Quirks mode, each browser implements the Web-based cascading style sheet standard in quite different ways. One of the beauty of switching to standards mode is that it forces almost all modern browsers to interpret the standards in a very similar way (not exactly the same, but much better).

If you want Web pages to appear in all browsers in the same way, trigger standards mode (in Internet Explorer and Opera) and almost Stan by including XHTML 1.0 Transitional DOCTYPE Dards mode (in Firefox) is a good idea. Fortunately, by default, Visual Studio. NET 2005 and Visual Web Developer automatically add the DOCTYPE to each new ASP.

Forwarding XHTML and DOCTYPE switching (MSDN)

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.