IE hack Problem

Discussion in 'CSS' started by mikee.jk, Mar 26, 2008.

  1. #1
    hi,

    I had a question in my Interview.

    "write a css for an element(image, text...) and the top margin for that element should be 20px in firefox and 50px in IE...hack it!"

    I could not answer it!@#$%.

    but i am interested to know the answer for it, can someone help me.

    Thank you in advance!!!
     
    mikee.jk, Mar 26, 2008 IP
  2. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #2
    Use conditional comments for IE:
    <style type="text/css">
    p{margin-top:20px}
    <!--[if IE]>
    p{margin-top:50px}
    <![endif]-->
    </style>

    The conditional comment MUST come after the first <p>, otherwise it will just get reset back to 20px.
     
    drhowarddrfine, Mar 26, 2008 IP
  3. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There're a few other ways to hack it. There are CSS hacks like * html {element} and *+html {element}... (just as ugly as conditional comments, but Microsoft recommends them over any hacks).
    There's also a float issue where, if you have something floated left, and then want some text underneath with x-amount of top margin, you'll see IE start at the bottom of the floated thing (instead of from the top of the container like FF would do)... this is until your top margin is greater than the height of the floated thing, anyway.

    I had just gotten done reading that... and wish I'd read if before running into the problem myself : )
     
    Stomme poes, Mar 26, 2008 IP
  4. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Chances are the hack isn't even needed. What does your full complete HTML and CSS code (as the browser would see it) look like?
     
    Dan Schulz, Mar 29, 2008 IP
  5. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #5
    it was a test question, Dan. I don't think there was any HTML or CSS.
     
    Stomme poes, Apr 4, 2008 IP
  6. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would have probably asked why they would want the page to look different in IE than in FireFox. Then I probably would have let them know that generally hacks are a bad idea, so I code my pages to look the same without using them if I can. Thus I am not familiar with all of the various hacks out there because many of them are unnecessary. :)

    How is that for a BS answer when you don't know the actual answer?
     
    ChaosFoo, Apr 4, 2008 IP