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!!!
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.
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 : )
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?
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?