Style Sheets
  Home arrow Style Sheets arrow Page 5 - Cross Browser Issues: CSS Hacks Explained,...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes
By: Jennifer Sullivan Cassidy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2006-01-25

    Table of Contents:
  • Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes
  • The Use of JavaScript and CSS
  • Hiding CSS from Buggy Browsers
  • Workarounds for Internet Explorer
  • More Workarounds for Internet Explorer

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes - More Workarounds for Internet Explorer


    (Page 5 of 5 )

    Min-Width and Max-Width Hacks (all versions of IE)

    IE is the only browser that doesn’t support the CSS2 properties min-width and max-width, which have been a standard since 1997. These are useful for controlling larger areas of type, particularly when setting line length limits to aid in readability.

    IE treats width as if it were min-width. By exploiting the fact that IE doesn’t support child selectors, we can create some CSS code that doesn’t allow a browser window to shrink below a certain size:

    body {

     width: 800px;

    }

    html>body {

     width: auto;

     min-width: 800px;

    }

    The max-width hack does require using some JavaScript in the CSS, which will cause your CSS not to validate. 

        width:expression(some javascript goes here);

    Here’s an example of its use:

    border:1px solid red;
    width:expression( 
        document.body.clientWidth > (500/12) * 
        parseInt(document.body.currentStyle.fontSize)?
            "30em":
            "auto" );

    The key is using Internet Explorer's relatively little known expression() property.  What this does is make IE check whether a value is over a certain threshold. If it is, then it makes IE use 30em as the box width; if not, auto is used, making the box shrink.  Fortunately, Mozilla, Firefox, Opera, Netscape 6 and other compatible browsers just ignore this because, when a browser that isn’t IE comes across something it doesn’t understand, it simply disregards it.

    Utilizing the Asterisk HTML Selector Bug (all versions of IE)

    The following selectors are all incorrectly interpreted by Internet Explorer as if the first universal selector (asterisk) does not exist:

    * html      /* basic form */

    * * body    /* white space between asterisks is significant */

    * html body /* higher specificity than either of above */

    IE treats the above as if they were, correspondingly:

    html

    * body

    html body

    In valid HTML and XHMTL documents, html is always the root element and body is always a child of the root element, and never a grandchild (or great grandchild). Therefore, the first three CSS selectors above should not match any element. Nonetheless, they are valid selectors.  By using any of the above erroneous selectors we can specify CSS rules that are meant only for IE.

    Line-Height and Replaced Element Hack (all versions of IE)

    In IE, line-height is incorrectly rendered if the line contains a replaced element. If the default line-height is changed to a larger value, the line spacing above and below any text line that contains a replaced element, such as a small image, will be half of the spacing seen elsewhere in the text.  This bug is triggered just by the simple presence of any replaced element: IMG, INPUT, TEXTAREA, SELECT and OBJECT.

    Line-height in IE:

    Line-height in Firefox:

    By calculating and applying a top and bottom margin to the replaced element, you can force the line-height to be what was intended.  Utilize a hack that gives specific instructions to IE, like the asterisk hack above, and then apply your top and bottom margin fixes.  You will need to know the height of the replaced element, and the height of all the replaced elements of the same type needs to be almost equal if you want to make use of the same CSS classes, otherwise, you'll have to create new classes for each replaced element.

    * html img {

     margin: 45px 0;

     vertical-align: middle;

    The vertical-align property is needed to position it correctly.  NOTE: This IE bug is supposed to be fixed in IE 7.

    Workarounds for Mozilla, Firefox, Netscape and Opera

    Generally, you aren’t going to see too many hacks for these types of browsers, unless you are talking about older versions of Netscape and Opera, for the sheer fact that they support CSS very well; why write hacks for all of the browsers instead of the one that’s erring?  But you have a lot of people that use IE, and the school of thought here is to try and look good to the majority of the public when the cross-browser compatibility issues emerge, then go back later and put in the hack to “fix” the other minority browsers.

    I don’t really feel I need to go into any fixes for the compliant browsers, just so that your code in IE works in everything else; I believe this is the absolute incorrect approach.  It not only makes things more difficult, but the likelihood of your code breaking in later versions of IE is extremely high.

    Writing compliant CSS code is always a good idea; the simpler the better.  Once in a while, you will want to use a workaround to get your design to look right in IE, and hopefully you’ll be able to use these for those times.  There is no excuse for writing sloppy code, but then again, there really is no excuse for IE not to be compliant with the CSS standards.  A great philosopher once said, “You can’t always get want you want; but sometimes you can get what you need.”  Workarounds might not be what you want, but you can sometimes get what you need.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · I have also seen this usage: @import url("url");It validates in XHTML...
     

    STYLE SHEETS ARTICLES

    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...
    - Pulling Web Page Elements with the Blueprint...
    - Pushing Web Page Columns with the Blueprint ...
    - Controlling Column Padding with the Blueprin...
    - Prepending Classes in the Blueprint CSS Fram...
    - Appending Grid Units with the Blueprint CSS ...
    - Changing Grid Units in the Blueprint CSS Fra...
    - The Blueprint CSS Framework
    - Building a Liquid Design with Evened Margins...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT