Crossbrowser CSS Tips

AID: 4179
  • Status: Published

2280 points

  • Byali_kayahan
  • TypeTips/Tricks
  • Posted on2010-12-01 at 09:49:49
Most web designers often experience crossbrowser issues during interface design / development.  On common problem is with Internet Explorer due to its rendering discrepancies between previous versions.  Here are some techniques I use to help avoid crossbrowser interface design problems.

1

Reset CSS

All browsers have different default margin padding values and we don't want to need to specify margin padding each time when we define a new class.  So let's reset default margin and padding.

*{margin:0px;padding:0px;}
                                    
1:

Select allOpen in new window

2

Internet Explorer Tips

We all know that the Internet Explorer series causes trouble.  Here are some particular techniques for detecting IE version and composing a special CSS to hand differences.

<!--[if IE]>
    <link href="ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window



The code above links IE.CSS if the browser is Internet Explorer regardless of its version.  And here is a more specific one for version detection.

<!--[if IE6]>
    <link href="ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window


But I prefer inline separators unless there isn't any major change.   For instance:

<style>
.someClass{
 background:red ;    /*This is for all browsers*/
 .background:blue;  /* This is for internet explorer 7 */
 _background:green ;/* This is just for IE 6 */
 *background:black;  /* This is for all internet explorer except IE 8 */
 width:300px;
 height:300px;
}
</style>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window


So if you run the code above and test it using for instance, BrowserLab, you'll realize that ...

*background:black
                                    
1:

Select allOpen in new window


...makes the box's background black for IE6 and IE7 whilst it is red for rest of the browser sets.
If you remove that line and rerun the test, IE6 will render the box background as green, IE7 will render the box background as blue, while IE8 and other browsers rendering will be red.

Also if you just want to assign attribute value for IE6 you may use !important
For instance:

.someClass{
 background:black ;!important
 background:red ;/*This is for all browsers*/
 width:300px;
 height:300px;
}
                                    
1:
2:
3:
4:
5:
6:

Select allOpen in new window


As the output of the line above, IE6 will render the background as red because it doesn't know the command of
    !important
whilst other newer browsers render it as black and won't override the first value due to that directive.


3

Having trouble with other browsers?

Sometimes we may need to define special handling just for Safari:

html:lang(en)>body  .someClass {background:orange}
                                    
1:

Select allOpen in new window


Or for Chrome:

body:nth-of-type(1) .someClass{
   background:teal;
}
                                    
1:
2:
3:

Select allOpen in new window


If we just append the browser set fixes at the very bottom of our CSS sheet, they will basically override previous definitions for specified browsers.

Asked On
2010-12-01 at 09:49:49ID4179
Tags

crossbrowser css

,

css

,

ie 6 css tips

,

crossbrowser web development

Topic

Cascading Style Sheets (CSS)

Views
1493

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top CSS Experts

  1. COBOLdinosaur

    213,892

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  2. LZ1

    169,513

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  3. DaveBaldwin

    108,635

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  4. ChrisStanyon

    101,266

    Master

    0 points yesterday

    Profile
    Rank: Sage
  5. tommyBoy

    65,616

    Master

    0 points yesterday

    Profile
    Rank: Genius
  6. kozaiwaniec

    57,116

    Master

    2,000 points yesterday

    Profile
    Rank: Guru
  7. nap0leon

    53,757

    Master

    0 points yesterday

    Profile
    Rank: Sage
  8. xmediaman

    50,100

    Master

    0 points yesterday

    Profile
    Rank: Guru
  9. jason1178

    49,680

    0 points yesterday

    Profile
    Rank: Genius
  10. SSupreme

    43,018

    0 points yesterday

    Profile
    Rank: Wizard
  11. Kravimir

    42,150

    0 points yesterday

    Profile
    Rank: Genius
  12. s8web

    40,064

    0 points yesterday

    Profile
    Rank: Sage
  13. therealteune

    35,300

    0 points yesterday

    Profile
    Rank: Wizard
  14. webmatrixpune

    30,818

    0 points yesterday

    Profile
    Rank: Guru
  15. HagayMandel

    27,880

    0 points yesterday

    Profile
    Rank: Guru
  16. zappafan2k2

    23,368

    0 points yesterday

    Profile
    Rank: Guru
  17. leakim971

    22,966

    0 points yesterday

    Profile
    Rank: Genius
  18. HainKurt

    20,000

    0 points yesterday

    Profile
    Rank: Genius
  19. jagadishdulal

    19,668

    0 points yesterday

    Profile
    Rank: Guru
  20. Proculopsis

    19,350

    0 points yesterday

    Profile
    Rank: Sage
  21. mplungjan

    19,232

    0 points yesterday

    Profile
    Rank: Savant
  22. basicinstinct

    19,026

    0 points yesterday

    Profile
    Rank: Genius
  23. anuradhay

    19,006

    0 points yesterday

    Profile
    Rank: Guru
  24. jtwcs

    18,808

    0 points yesterday

    Profile
    Rank: Master
  25. gurvinder372

    18,057

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame