title besides title

 
Showing posts with label CSS - Hacks and Workarounds. Show all posts
Showing posts with label CSS - Hacks and Workarounds. Show all posts

Sunday, December 2, 2012

CSS : Hacks and Workarounds - [9] Introduction

When designing for the Web, developers historically have used hacks and workarounds to achieve certain effects. The mid-1990s saw a proliferation of such workarounds, among them single-pixel GIFs, font tags, and nested tables, to name just a few.
In this new millennium, CSS has allowed web designers to free themselves from these old workarounds. But although CSS 2 became a recommendation back in May 1998, only relatively recently have browser vendors fully implemented the standard in their products. To overcome the bugs in the browsers that have poor CSS support, web designers have once again resorted to using hacks and workarounds to achieve their designs.
There are many reasons why old browsers are still in use. Unlike web developers, most people don't automatically upgrade their browsers each time a new one is available. They tend to stick with the browser that's on their computer because it works fine and will get a new browser only when they purchase a new computer. Also, IT departments in many companies lock down the systems and prevent individuals from upgrading software applications on their own.
So even though problems might be solved by using newer versions of browsers, web developers still need to use hacks or workarounds to deliver the appropriate presentation to their audience that is unwilling or unable to upgrade.
This chapter covers techniques on how to deal with browsers that have spotty CSS support. Included in this chapter are methods to hide advanced style sheets from Netscape Navigator 4, deal with Internet Explorer 5.x for Window's unique interpretation of the box model, and more.

CSS : Hacks and Workarounds - [9.1] Hiding Certain Styles from Netscape Navigator 4.x

Problem

You want to keep Netscape Navigator 4.x from using certain CSS rules. For example, Navigator 4.x doesn't correctly inherit styles like font-family and color set for the body to elements like table, div, and p.

Solution

In a separate style sheet, place the CSS rules that you don't want the Netscape Navigator 4.x browser to use. Then use the @import method to associate the "advanced" CSS rules (making sure that the advanced style sheet comes after the basic to override styles from the basic style sheet):
<link rel="stylesheet" type="text/css" media="all" title="Basic CSS" href="/basic.

css" />

<style type="text/css" media="all">

 @import "/css/advanced.css";

</style>

Discussion

Netscape Navigator 4 was the first Netscape browser to contain support for CSS. Unfortunately, Netscape was developing the browser while CSS was being finalized. Also, Netscape was supporting its own proposal, JavaScript Style Sheets, known as JSSS, and was basing Navigator 4 on that technology. So, when the W3C went with CSS instead, the Netscape engineers had to do some quick jury-rigging to fix their implementation. This is why you can turn off CSS support in Navigator 4 just by turning off JavaScript in the program's preferences.
Because Navigator's CSS implementation was essentially a remapping to its JSSS engine, actual CSS support for the implementation of such things as the @import method of associated styles to a web page was woefully incomplete. And whatever CSS styles Navigator did include were implemented improperly. As newer browsers offered stronger and more robust support for CSS, a method for hiding certain CSS rules from Navigator 4 became a necessity if web developers were to embrace CSS-enabled designs.
Although the @import method works, you need to write the CSS rules in two separate files: one for Navigator 4 and another one for other browsers capable of handling the @import method. Another way of hiding styles from Navigator 4 and keeping the styles in a single style sheet is through a CSS comment workaround known as the Caio hack, named after the person who developed it, Caio Chassot.
In the following code example, styles are hidden from Navigator 4 through the hack:
.p1 { 

 font-size: 200%; 

 text-decoration: underline;

}

/*/*/

.p2  {

 font-size: 200%; 

 text-decoration: underline;

}

/* */

.p3 { 

 font-size: 200%; 

 text-decoration: underline;

}

Here is the HTML code that is used in Figure 9-1:
<h2>Netscape Navigator 4 test</h2>

<p class="p1">This text is large and underlined.</p>

<p class="p2">This text is <em>neither</em> large nor underlined.</p>

<p class="p3">This text is large and underlined.</p>

Figure 9-1. Netscape Navigator's comment parser problem used to hide certain styles

Navigator 4 interprets the comment snippet /*/*/ as an open comment, meaning that anything after it is hidden from the browser. Other browsers see the snippet as open and close comment tags. To close the hack to let Navigator 4 see the rest of the styles, add another pair of open and close comment tags, this time with a space between the asterisks:
/* */

You also can include the hack with inline styles:
<p style="/*/*/ color: font-size: 200%; text-decoration: 

underline;">This inline-styled p is neither large nor underlined 

in Navigator 4.</p>

Along with the comment parsing problem, Navigator 4 won't pull in style sheets when the media attribute equals all:
<link rel="stylesheet" type="text/css" href="/css/advanced.css" 

media="all" >

<style type="text/css" media="all" >

.p2 {

 font-size: 200%; 

 text-decoration: underline;

}

</style>

Navigator 4 won't interpret style sheets when there is more than one value for the media attribute. So, if you use a combination of values for the media attributes, Navigator 4 ignores the style sheet:
<link rel="stylesheet" type="text/css" href="/css/advanced.css" 

media="screen, print" >

You also can hide styles from Navigator 4 by using descendant selectors—for instance, by placing the html element as a selector before the next selector (since Navigator 4 doesn't include the html element in the parsed document). In the following example, the text size and decoration won't appear in Navigator 4:
html .p2 {

 font-size: 200%;

 text-decoration: underline;

}

CSS : Hacks and Workarounds - [9.2] Delivering Alternative Values to Internet Explorer 5.x for Windows

Problem

You want to apply different CSS property values to the Internet Explorer 5.x for Windows browser, such as the value of the width property, to work around implementation of the Microsoft box model.

Solution

Put in the declaration you want Internet Explorer 5.x for Windows to handle, and then use what's called the box model hack to put in the corrected values you want other browsers to interpret:
div#content   {

 /* WinIE value first, then the desired value the next 2 times */

 background-color: red;

 voice-family: "\"}\"";

 voice-family: inherit;

 background-color: green; 

}

html>div#content

 background-color: green; 

}

Discussion

Tantek Çelik, Microsoft's diplomat to the World Wide Web Consortium (W3C) CSS and HTML working groups, originally demonstrated how the box model hack could be used to fix Internet Explorer 5.x for Windows' approach to the box mode. This fix also applies to Internet Explorer 6 for Windows in quirks mode since it also uses the Microsoft box model.
CSS specifies that the width property defines the width of the content area of a box, and that any margin, border, or padding space should draw outside of that space. For example, in the following bit of code, the width of the element (as it is stated) is 500 pixels:
div#content {

 width: 500px;

 padding: 33px;

 margin: 50px;

 background-color: #666;

}

As seen in Figure 9-2, the box appears to be 566 pixels wide. The 66 "extra" pixels are from the padding being added outside the 500 pixels.
Figure 9-2. The box model correctly implemented in Mozilla

In Internet Explorer 5.x for Windows, the width isn't the stated value in the CSS. Instead, Microsoft's box model draws the box with the border and padding inside the specified width. To calculate the width of the content area for Internet Explorer 5.x for Windows, subtract the padding and borders from the stated width:
width property
- left border - left padding
- right padding - right border
= Microsoft's box model
In the previous CSS example, the width determined by Internet Explorer 5.x for Windows is 434 pixels (see Figure 9-3):
500px - 33px - 33px = 434px

CSS : Hacks and Workarounds - [9.3] Removing Web Page Flicker in Internet Explorer 5.x for Windows

Problem

You want to remove the initial flicker, or flash, of unstyled content before Internet Explorer 5.x for Windows applies your CSS style sheet.

Solution

Add a link or script element as the child of the head element in your web document:
<head> 

 <title>christopher.org</title> 

 <link rel="stylesheet" type="text/css" media="print" href="print.css"> 

 <style type="text/css" media="screen">@import "advanced.css";</style> 

</head>

Discussion

If a web page contains a style sheet associated by only the @import method, Internet Explorer 5.x for Windows' browsers first show the contents of the web page without any of the styles applied to the markup. After a split second, the browser redraws the web page with styles applied. Adding a link or script element in the head before the @import rule forces the browser to load the styles when it initially draws the page in the viewport.
This rendering phenomenon isn't a problem with the browser itself. The CSS specification doesn't specify whether this behavior is acceptable or not, so the browser is compliant with the specification. You or your audience might perceive this flicker as a bug or annoyance, though, so you should prevent it from occurring.

CSS : Hacks and Workarounds - [9.4] Keeping Background Images Stationary in Internet Explorer 6 for Windows

Problem

You want to have a fixed background image in Internet Explorer 6 for Windows.

Solution

Use the following JavaScript hack to force the effect. First copy the following code to call up the JavaScript code in your web page:
<head>

<script type="text/javascript" src="fixed.js"></script>

</head>

Then in the fixed.js file place the JavaScript code for the workaround, which can be found at this book's online sample archive http://www.oreilly.com/catalog/cssckbk/ or from Andrew Clover's site at http://doxdesk.com/software/js/fixed.html.

Discussion

According to the CSS 2 specification, when a background image is fixed using the background-attachment property, it shouldn't move when the user scrolls the web page. In all versions of Internet Explorer for Windows, this property doesn't work at all.
However, this stunning JavaScript workaround developed by Andrew Clover fixes this problem by simply adding the JavaScript link to the web page. The JavaScript works by dynamically recalculating the position of the viewport as a user scrolls, and then it adjusts the background image accordingly.

CSS : Hacks and Workarounds - [9.5] Keeping CSS Rules from Internet Explorer 5 for Macintosh

Problem

You want to hide certain rules from Internet Explorer 5 for Macintosh.

Solution

To hide CSS rules from Internet Explorer 5 for Macintosh, insert a backslash in front of the closing comment with the characters */:
/* \*/

h1 { 

 font-size: 200%; 

 text-transform: uppercase;

 background-color: #666;

 }

After the rules pertaining to Internet Explorer 5 for Macintosh, insert another comment line:
/* */

p { 

 text-transform: uppercase;

}

Discussion

This method exploits a simple comment-parsing problem found in Internet Explorer 5 for Macintosh. The backslash before the closing comment causes the browser to think the comment actually has not closed; any valid CSS rules are hidden, allowing entire rule sets to be hidden from the browser until the next closing comment marker is hidden.