the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Multiple classes in Internet Explorer

    Jan 16 2006

    I recently discovered the power of using multiple classes. That is, using more than one class on a single element. The class attribute simply accepts multiple classes separated by a space. For example, you can do something like this:

    <style>
    .box { border: 1px solid black; }
    .small { width: 400px; }
    .large { width: 800px; }
    </style>
    
    <div class="small box">
    <div class="large box">

    This is a great way to organize your CSS. For example, you can have a set of classes to define font styles and another set of classes to define box sizes. Then you can use them together in different combinations.

    The class names "small" and "large" aren't totally clear, since they refer specifically to small and large box sizes. It'd be great if I could write "large title" and have it affect the font size instead of the width. So, I tried to change the definition by combining multiple classes in a single selector:

    .box { border: 1px solid black; }
    .box.small { width: 400px; }
    .box.large { width: 800px; }
    
    .title { color: blue; font-family: Arial; }
    .title.small { font-size: 10px; }
    .title.large { font-size: 20px; }

    When I tried this in Firefox, everything worked great. Unfortunately, Internet Explorer doesn't support this. In fact, Internet Explorer will just look at the last class in the list. So, it will interpret the last example as if we had written this:

    .box { border: 1px solid black; }
    .small { width: 400px; }
    .large { width: 800px; }
    
    .title { color: blue; font-family: Arial; }
    .small { font-size: 10px; }
    .large { font-size: 20px; }

    Small boxes will have small fonts, large boxes will have large fonts, small titles will be 400px wide, large titles will be 800px wide. Very unfortunate.

    Once again, Internet Explorer ruins all the fun. Well, there's an up side to this. When we use "small" to affect the width in one place, and the font size in another place, we make it harder to understand and maintain the CSS. And isn't that supposed to be the point of using CSS?

    Besides, not all is lost. We just have to come up with better names. We can still do this:

    .border { border: 1px solid black; }
    .small-box { width: 400px; }
    .large-box { width: 800px; }
    
    .title { color: blue; font-family: Arial; }
    .small-text { font-size: 10px; }
    .large-text { font-size: 20px; }

    It sure isn't as pretty to write something like class="border small-box". But at least then we can use our "small-box" class in places that don't have borders, or use the "border" class to give a border to something without a fixed width.

    In conclusion, avoid the .class1.class2 syntax altogether. It's not supported by Internet Explorer, and it makes code harder to read and manage. However, using multiple classes is completely supported and will make your CSS cleaner and more reusable.

    Tags: css html tips internetexplorer firefox standards web
    View 1 Comment | Add a comment
  • Comments

    1. Ben Evans at 10:23pm on January 16, 2006

    In your html file, put the following code to reference your stylesheets (i hope your comment box allows the less than / greater than signs)...

    <link href="style.css" type="text/css" rel="stylesheet" media="screen, projection" />
    <!--[if IE]>
    <link href="ie.css" type="text/css" rel="stylesheet" media="screen" />
    <![endif]-->

    The comment block that contains the if IE and the second link tag will be treated as comments by non IE browsers, and IE will load the additional stylesheet.

    In your normal stylesheet, set your styles normally. You will override those styles for IE in the second stylesheet, which you can do like so...

    * html body div#box_feature
    {
    height: 100px;
    }

    * html body div.table_login
    {
    width: 160px;
    }

    As you can see, IE does let you do the parent.child syntax, but you have to do the entire path from the root (*) down the DOM tree to the child you want to set.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites