tags:

views:

125

answers:

4

Can anyone please help me with this. I am new to css styling. I have a css file where all my styling resides for the asp.net website that I am trying to build. However it works fine on the server that has IE 7 installed on it and when I try to view it using chrome, firefox or IE 8 and above it kinda distorts the look of the website.

I need to change my margin according to the client browser. How do I achieve this? Ive heard that there is an "if" condition solution to this. Means I have to write seperate css files in my project and include the right one according to the browser?? Cant I just change the basic css to detect the browser and select the appropriate styles?

i want to choose different positioning for a division according to client browser. I am working on asp.net 2.0 by the way.

existing code:

#page   
{  
background-image: url('C:/Program Files (x86)/some-path-here/Images/img1.jpg');  
}  

I am trying to do this:

html>/**/body #page   
{  
background-image: url('C:/Program Files (x86)/some-path-here/Images/img1.jpg');  
}  

But the img1 does not load up for IE 8 and firefox. It does for IE 7.

I tried to target just IE 7 for my testing purposes with the following code in my html file:

<!--[if IE 7]> body {
background-color: Red;
} <![endif]-->

However when I test the page on IE 7 it just show me the "background-color: Red;" text. Why is that? Am i missing something here in this conditional statement? It is only taking the text and doing nothing with the background color. Thanks.

+3  A: 

You can code for standards compliant browsers (Chrome, Safari, Firefox etc.) and then add conditional comments for internet explorer.

EDIT:

As a side point, look at Eric Myers' CSS reset. It's an excellent resource.

Moshe
you might want to rephrase that as "code for **web-standards**". The standards-compliant rules can then be adapted for non-standard browsers, like IE.
STW
@STW - Thanks. [Ugh, fillers for a single character.]
Moshe
Firefox uses Gecko not webkit :)
Yassir
@Yassir - what does that matter?
KevinDTimm
Now I have already have the css for IE 7 and below but want to change a few things for firefox and chrome and IE 8. I am using html>/**/body #divid {} for example. I have set the background-image property and it seems to work fine in IE 7 and below but not in other browsers. I want to write a special class for firefox, IE 8 and others for this image and I did that but wihtout success. It just doesnt get the image in the back ground.
VP
@VP - Please post your code in an edit to your question.
Moshe
I just did that Moshe. Thanks
VP
@VP - Dunno, could be the lack of double quotes around your url paths, inside the parenthesis.
Moshe
Ok.That might be. However I am now required to write internet explorer hacks using conditional statements. Since I have never done this,, can you please tell me if the conditional statement are to be written inside the html file ? So basically what I understand now is that I have to build my css for standard browser and then write conditional statement targeted ONLY for IE? Is that right? and if yes then what do I do for IE 8 then?
VP
Never, ever, ever use any version of IE to initially test your markup! We write hacks for IE, not the other far more modern browsers. Always, always use FF, Chrome, Safari, Opera, et al. Test your markup in IE as you go along to see WHEN IE screws it up.
Rob
@VP - you can put anything in a conditional comment, including a link to an external stylesheet. You can also use conditionals to limit specific versions of IE.
Moshe
A: 

CSS Hacks are decreasing in popularity. You should instead start your css by using a CSS Reset. You can read more about reset options in this thread.

Craig
that's far from a firm rule. CSS Resets are also declining in popularity as browsers are becoming more standardized. Assuming a reset is needed can lock you into that reset, so it's an assumption which should be turned into a careful decision.
STW
Now I have already have the css for IE 7 and below but want to change a few things for firefox and chrome and IE 8. I am using html>/**/body #divid {} for example. I have set the background-image property and it seems to work fine in IE 7 and below but not in other browsers. I want to write a special class for firefox, IE 8 and others for this image and I did that but wihtout success. It just doesnt get the image in the back ground
VP
CSS hacks are more for fixing browser bugs while CSS resets are for keeping all browsers consistent in layout.
Rob
*shrug* He's having troubles with a margin. I tend to think that if he was using a reset.css as a basis for the styles that the problem wouldn't exist in the first place. I don't like the idea of just patching this particular problem with a hack, when having a better foundation in place would most likely solve not only this, but other problems in the future as well. Additionally, I have a feeling that there is an IE8 rendering mode component to this problem as well, and addressing that problem is beyond the scope of this question, and this particular issue could be best addressed with a reset.
Craig
A: 

Conditional formatting for IE: http://www.quirksmode.org/css/condcom.html

In terms of Hacks you can write invalid markup for some browsers (such as IE) by using _ in front of the CSS attributes for IE 6/7 but it makes your CSS invalid per specifications - it will still run properly.

nopuck4you
Ok.That might be. However I am now required to write internet explorer hacks using conditional statements. Since I have never done this,, can you please tell me if the conditional statement are to be written inside the html file ? So basically what I understand now is that I have to build my css for standard browser and then write conditional statement targeted ONLY for IE? Is that right? and if yes then what do I do for IE 8 then? Thanks for the help .
VP
A: 

Instead of Conditional Comments and non-standards ompliant CSS hacks, check out the CSS Browser Selector plugin. This jQuery will allow you to write different rules for each browser inside the CSS file itself with no inaccuracies for standards and no Conditional comments. It can even allow you to target different browsers on different OS's.

For IE you'd write:

.ie .myDiv
{
   background-color: #f00;
}

And for webkit browsers:

.webkit .myDiv
{
   background-color: #bar;
}

Then IE (all versions) and Webkit browsers will display different background colors. You have a big choice of browsers to select as well :)

Kyle Sevenoaks
wow. this sounds like a real neat solution here. However the I cannot find the download link on the site you mentioned. Is this a plugin for visual studio as I am developing on visual studio 2010 asp.net ?
VP
It's actually a jQuery plugin, you need to include the jQuery library in your header and then link the script after the body tag in your HTML master page (if you have one).
Kyle Sevenoaks