Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How can you give different instructions based on browser in a CSS sheet?

  1. #1
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can you give different instructions based on browser in a CSS sheet?

    Any ideas on how I can asign different instructions based on browser with in a style sheet without using JS.

    Looking to tidy up a few issues between ff and ie in my style sheets.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There are various css hacks. But, one must be careful! A hack, by definition, isn't supported in any browser. It simply takes advantage of a bug in a given version or versions of a given browser or browsers.

    One is the !important hack. In IE 6 and less, it doesn't always mean what it is supposed to. If you have:

    width:30px!important;
    width:27px;

    All browsers are supposed to ignore the second width because it isn't !important. But, IE 6 and less will use 27px, all others (including IE 7) will use 30px.

    But, with this and other hacks, one must remember what each does and in which browser(s). There is generally a better way, conditional comments. These are specifically supported in IE 5 and up. With a conditional comment, one may add additional stylesheets for certain or all IE versions, ex:

    Code:
    <!--[if IE]>
    <link rel="stylesheet" type="text/css" href="ie_only.css">
    <![endif]-->
    Now, this should come after the default stylesheet declaration in the head of your document. It doesn't replace it for IE but, anything in it will override the defaults for IE only. To IE, it is simply another stylesheet. To all others, ignored.

    Specific versions can be targeted as well as ranges of versions. Google IE conditional comments for details.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    or you can use

    Code:
    @import(path.css);
    as IE6 and before doesn't support the import functionality


    what you do is create your css file and just comment the "IE specific" areas, then link your "overrides" in the @import file.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's another example of a hack, yes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can use the downlevel conditional (here, literally - if less than IE 5):

    Code:
    <![if lt IE 5]>
    <link rel="stylesheet" type="text/css" href="all_others.css">
    <![endif]>
    To target all browsers except those IE versions that support conditionals. This is not a hack. The important difference in a downlevel conditional is the absence of dashes. Without those, other browsers do not see it as a comment. IE 5 and up see it as not a comment either but the condition excludes them.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's not valid HTML though. Instead, you need to do something like:
    Code:
    <!--[if !IE]><!-- -->
      <link rel="stylesheet" type="text/css" href="all_others.css">
    <!--<![endif]--><!-- -->
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    That's not valid HTML though. Instead, you need to do something like:
    Code:
    <!--[if !IE]><!-- -->
      <link rel="stylesheet" type="text/css" href="all_others.css">
    <!--<![endif]--><!-- -->
    That isn't either. A comment declaration begins with "<!" (MDO) and ends with ">" (MDC). Comments within it are delimited by pairs of "--" (COM). Only white space can separate comments, therefore the text "<![endif]" is invalid, as is the end because the final COM is unpaired.

    Mike

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    That's not valid HTML though. Instead, you need to do something like:
    Code:
    <!--[if !IE]><!-- -->
      <link rel="stylesheet" type="text/css" href="all_others.css">
    <!--<![endif]--><!-- -->
    That creates an unwanted text node:

    <!--[if !IE]>

    in FF. This is simpler, avoids that and validates while still doing the job:

    Code:
    <!--[if !IE]>-->
    <link rel="stylesheet" type="text/css" href="all_others.css">
    <!--<![endif]-->
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh! Stupid me. I was thinking that would cause the first --> and the final <!-- to go unpaired in IE.
    A comment declaration begins with "<!" (MDO) and ends with ">" (MDC). Comments within it are delimited by pairs of "--" (COM). Only white space can separate comments, therefore the text "<![endif]" is invalid, as is the end because the final COM is unpaired.
    Ah. Is there any way around that (to hide text only from certain IE versions, for example)?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Is there any way around that (to hide text only from certain IE versions, for example)?
    If I understand your question, the easiest way is to break it out and duplicate:

    HTML Code:
    <div>
    <!--[if !IE 7]>
    This Isn't IE 7<br>
    <![endif]-->
    <!--[if !IE]>-->
    This Isn't IE 7<br>
    <!--<![endif]-->
    This is all browsers
    <!--[if IE]>
    <br>IE Only
    <![endif]-->
    </div>
    The first "This Isn't IE 7<br>" will be seen only by IE versions less than 7, the second one only by non-IE browsers. IE 7 is the only browser that won't see either of them and no browser will see more than one of them.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •