if IE only works for stylesheets? - css

So I am forced to use conditional style for IEs via the [if IE] comment. I have been trying to fit it in my code but I found some inconsistency that I cant explain.
If I use it like this
<!--[if IE]>
<style type="text/css">
#SomeName {
width: 100em;
}
</style>
<![endif]-->
it will just not work.
However, if I put the style in a css sheet and add a link to it, it works.
<!--[if IE]>
<link rel="stylesheet" type="text/css" href=".../sheetName.css"/>
<![endif]-->
Is there a way for me to put the style inside the [if IE] tags rather than linking it through the tag? it just looks wrong.
Thank in adavance

Nothing wrong with your code.
Make sure that #SomeName is as such in the html (beacuse css rules are case-sensitive), and make sure that you include your code after the normal css include (to avoid having the rule overriden by what is in the normal css)

The first code snippet should work just fine if you are using the correct CSS selector. Make sure you are using the exact selector you need to adjust. However the selector is being used in your normal stylesheet, just copy and paste it into the conditional tag and change whatever you need to change in regards to width or whatever.

Related

conditional comments IE 9

I have one line of CSS to change on a couple of classes on a Wordpress site to make it backward compatible to IE9 (it is currently on a localhost site in development).
The CSS i need to turn off is just one line and where I'll change the opacity from 0 to 1 so the headings show in older IE versions - the transforms etc won't be recognised so these won't be an issue.
If I use a conditional comment, because it's only one line of CSS - can I use the following:
<!--[if IE 9]>
<style>
span, .animate3, .animate4 {opacity: 1!important;}
</style>
<![endif]-->
I can't seem to find any info about using the style tag after a conditional comment. It would seem easier than setting up a stylesheet for one line of code?
Any help ideas would be awesome
Paul.
Yes, the way you set it up is correct and can be implemented in the <head> of the document.
As mentioned in the MSDN Compatibility documents about conditional comments:
<!--[if expression]> HTML <![endif]-->
is the way to write it. Any HTML element inside can be written, so <style> is valid to use.
<!--[if IE 9]><style>body { background-color: red; }</style> <![endif]-->
Read more about conditional comments at MSDN or at Quirksmode (with some better examples).

Is it possible to use different css for IE(any version of ie) and chrome

Is it possible to use different css selector for IE(any version of ie) and chrome? Its a normal top property which appears differently in both browser and needs to explicitly adjusted according to the browser
You cannot do this in CSS alone. You need what are called "conditional comments" like the following:
<!--[if IE 8]>
<p>This is IE 8</p>
<![endif]-->
These are added to your HTML and can be used in many ways. Two primary ways that I have used them are:
To link to a wholly different CSS style sheet
To change the class on the <html> or some other parent tag and use CSS rules to select any children of it
I realize that second description may sound a bit complex but it's actually pretty simple so here's an example:
<!DOCTYPE HTML>
<!--[if IE 8]>
<html lang="en-US" class="ie8">
<![endif]-->
<![if !IE]>
<html lang="en-US">
<![endif]>
...
<body>
<div class="someClass"></div>
</body>
...
Then, in your CSS, use a selector like: .ie8 .someClass
Welcome to the club! Anyways, although you can try to set browser specific css on elements, actually you cannot guarantee that it'll work exactly like you aimed. Because it depends on how those browsers handles these css classes, and there is nothing you can do about that. You may try to set different css classes for IE like this:
<!--[if lt IE 9]>
<html class="ie">
<![endif]-->
<!--[if (!IE) | (IE 9)]><!-->
<html>
<!--<![endif]-->
Notice that these are actually comment lines, but ie reads these lines and set the user-defined css class "ie" to html element (you may notice that Chrome and Firefox ignores these statements). you can then use this css, for example;
html.ie div{
top: 0;
}
It's really annoying to deal with these cross-browser ie bs, I know. hope this helps
What you want to achieve?
If you want to compensate browsers all differences you can use for eg. modernizr
If you want to add special css file for IE you can use Conditional comments They look like this:
< !--[if IE 9]>
< link rel="stylesheet" type="text/css" th:href="ie9.csss"/>
< ![endif]-->"
If you want to fix something in css selector you can use hack(HACK! means not recommended, avoid but if you really have to and you have gun next to your head etc...) which will make properties or css class understandable only for specific browser (google this there is to many of them) eg. http://code.tutsplus.com/tutorials/quick-tip-how-to-target-ie6-ie7-and-ie8-uniquely-with-4-characters--net-10575
And last option learn CSS and find where you made mistake because probably some element is diffrent size and that caused 1-2 px difference with position top

ie7 conditional override is not working

<!--[if lt IE 9]><link rel="stylesheet" type="text/css" href="/a/s/major-market/non-reponsive.css" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" type="text/css" href="/a/s/major-market/ie7.css"><![endif]-->
I have conditionally included this two statements. Now when I try to ovveride a similar css in ie7.css it doesnot work. for example
In non-reponsive.css:
.header .features{
width:25%;
}
In ie7.css:
.header .features{
width:20%!important;
}
This is not working for some reason. please help me figure it out.
Ensure that the IE 7 conditional include is located below all other CSS declarations/includes within the page otherwise it will get overruled.
I am quite sure ie7.css is not loaded at all. !important would override anything in other stylesheets. Actually, if ie7.css comes after non-responsive.css, !important is unnecessary anyways.
That's the most I can tell by looking at the information you have provided.

CSS Browser Selectors

In laying out a page with absolute positioning, I realized that it rendered differently from browser to browser. I've been looking online about css selectors to see if there was some way to change the positioning based on which browser the user was using, but I haven't been able to find anything very helpful. Any ideas?
That looks like a bad strategy to me... I wouldn't want to switch from absolute to, say, relative or some other kind of positioning based on the kind of browser. They all should implement positioning fairly similar. You'd probably need to add some extra styles to make a particular element behave properly on certain browser, but changing the global positioning method based on browser is not a good idea, IMO.
UPDATE:
On CSS alone, there isn't any standard mechanism to detect a browser. You can use tricks like the one below to detect a particular version of IE, for example:
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="iespecific.css" />
<![endif]-->
or something like:
<!--[if gte IE 6]>
<link rel="stylesheet" type="text/css" href="iespecific.css" />
<![endif]-->
For a more complete list of options see here.
Another alternative is to use jQuery (if it's an option for you) and adjust the element's style using javascript depending on the browser. Something like:
if ( $.browser.msie ) {
$("#div ul li").css( "display","inline" );
} else {
$("#div ul li").css( "display","inline-table" );
}
More examples here.
A word of caution: none of the above methods is infallible. One, for example, can make Firefox identify itself as Internet Explorer.

IE9 CSS hack for background-position?

I need an IE9 CSS hack to be able to set the background-position property differently for IE9.
I have tried with several different ones that didn't work and from what I read somewhere, the background property is not possible to "hack" at least the same way as the other.
I basically need this to only apply to IE9:
#ABB_ABContent .subnav li.selected { background-position: center 17px; }
Any suggestions?
If you can't find anything else, there's always conditional comments:
<!--[if IE 9]>
IE9-specific code goes here
<![endif]-->
This would have to live in your HTML code, rather than your stylesheet, but you could use it to include an additional CSS file for IE9.
Alternatively, you may want to look up the Modernizr library. This is a small Javascript tool which you add to your site, which detects what features your browser supports, and allows you to write your CSS to target specific features (or their absence). If there's something about IE9 that it doesn't support which you're trying to work around, this may be the best solution.
I'm still puzzled as to what problem you're trying to solve though.
<!--[if IE 9]>
<link rel="stylesheet" type="text/css" href="your path" />
<![endif]-->

Resources