2010年12月13日

IE Common Browser Issue

Internet Explorer Conditional Statements

Internet Explorer provides a way of adding conditional statements that allow the designer to specify which version(s) of the browser should display certain sections. These conditional statements appear as simple HTML comments to other browsers (e.g, Firefox) and are therefore ignored. This can be very useful for addressing CSS bugs in older versions of Internet Explorer without having any impact on other browsers.

The syntax is shown below.

Syntax

<!--[if IE 7]> HTML Code only shows up on Internet Explorer 7<![endif]-->

As you can see, the beginning if statement starts with the beginning of an HTML comment (<!--) and the endif statement ends with the end of an HTML comment (-->), so all other browsers view the whole contents as a comment and do not render any of the contained code.

The code shown above will only be displayed in Internet Explorer 7, but you can use operators to specify multiple versions that will display a block of code. The operators are shown below:

Conditional Operators
Operator Description Example
! not <!--[if ! IE 7]>
lt less than <!--[if lt IE 7]>
gt greater than <!--[if gt IE 7]>
lte less than or equal <!--[if lte IE 7]>
gte greater than or equal <!--[if gte IE 7]>

1. Double-Margin Bug

The double-margin bug manifests itself in Internet Explorer 6 and earlier when an element has a margin in the same direction in which it floats (e.g, a left margin on an element that floats left). In this case, IE 6 and earlier double the margin as shown in the example below.

<style type="text/css">
#SideBar {
float: left;
margin-left: 50px;
width: 200px;

background-color:green;
}
#MainPage {
float: left;
width: 400px;
background-color:yellow;
}
</style>
<div id="SideBar">
This is the side bar.
</div>
<div id="MainPage">
This is the main page.
</div>

The fix is very strange: you simply set the display property of the offending element (in this case, the sidebar) to "inline". Because all floats are block elements, doing so has no effect on the display in other browsers; however, it does, for some reason, fix the display in Internet Explorer.

The double margin bug is fixed in IE7.

<!--[if IE]>
<style type="text/css">
#SideBar {
display:inline;
}
</style>

<![endif]-->

2. 3-Pixel Gap Bug

Internet Explorer has a weird bug, in which it adds a three-pixel margin between a floating element and the subsequent element. If the subsequent element has a width defined, Internet Explorer pushes the whole element to the right. If no width is defined, Internet Explorer just pushes the content to the right. Examine the code below.

#SideBar {
 float: left;
 margin-left: 50px;
 width: 200px;
 border:1px solid green;
}

#MainPage {
 border:1px dashed red;
}

Notice the tiny amount of added space between the text "This is the main page." and the floating element. Well, that's picky! And really, it doesn't create much of a problem, until you add a width to the subsequent element:

#MainPage {
 border:1px dashed red;
 width:200px;
}

Now the whole element is pushed over to the right.

 

This may not be a problem either, except in the case of a very tight layout. Those extra three pixels could cause the MainPage element to clear down under the float, which would destroy the design.

Luckily, the fix is easy. Simply set the margin-right of the floating div to -3 pixels as shown below:

<!--[if IE]><style type="text/css">#SideBar { display:inline; margin-right:-3px;}</style><![endif]-->

 

posted @ 2010-12-13 15:14 Buyee 阅读(26) 评论(0) 编辑

The Box Model

All elements are treated as boxes: paragraphs, headings, block quotes, lists, list items, and so on. Even inline elements like <em> and links are treated by CSS as boxes.

According to the W3C definition of a box created using CSS, the width and height define the width and height of only the content area (the area where images and other objects are displayed). The apparent width and height, or the total width and height that the element takes up in the design, include the content area plus the padding and border sizes. So, the apparent width would be calculated in the following way(Margin is unique in that it doesn't affect the size of the box itself per-say, but it affects other content interacting with the box, and thus an important part of the CSS box model.):

width(content) + left padding + right padding + left border + right border = width(apparent)

Unfortunately, Microsoft Internet Explorer through version 5.5 defined width and height values as the apparent width and height (including padding and borders). This effectively subtracts the padding and border sizes to determine the content width and height:

width(apparent) - left padding - right padding - left border - right border = width(content)

So, given the following CSS:

#object1 {
    border: 5px solid #000;
    padding: 10px;
    width: 100px;
}

browsers using the W3C standard (Firefox, Safari, Opera) will set the content area to 100 pixels wide, but will have an apparent width of 130 pixels:

100 + 5 + 5 + 10 + 10 = 130

Whereas Internet Explorer 5.5 and earlier will set a content area 70 pixels wide and an apparent width of 100 pixels:

70 + 5 + 5 +10 + 10 = 100
Recent versions of Internet Explorer (6 and later) fix this problem by following the W3C standards. 
The solution is: Turning Off Quirks Mode.

Quirks Mode

Modern browsers have at least two modes in which they can display content:

  1. Standards-compliant mode
  2. Quirks mode

In standards-compliant mode, the browser does its best to display the page according to the latest CSS standards. In quirks mode, the browser displays the page as if it were an older version of the same browser. The purpose of quirks mode is to make pages that were designed for the old browsers display the same in the newer browsers.

You won't find much difference between quirks mode and standards-compliant mode in Mozilla, Opera or Safari, but you may see big differences in Internet Explorer.

 

Turning Off Quirks Mode

By default, quirks mode is on. It can be turned off by adding the correct DOCTYPE declaration at the top of the HTML page. The following DOCTYPE declarations will turn on standards-compliant mode for all modern browsers:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 

One gotcha in Internet Explorer is that quirks mode is always enabled if you include an XML declaration (<?xml version="1.0"?>), so we recommend you do not include one.

 

posted @ 2010-12-13 14:20 Buyee 阅读(61) 评论(0) 编辑