Less hack background-image for IE8 - css

I want to set different background image for IE8 with Less, as it doesn't support background-size.
I have searched in Google about IE8 hacks and the result is writing code like this height: 300px\9;, ending with \9.
I have tried this, but I find it doesn't support background-image setting.
If I write like this
.backgroundImage(#url) {
background-image: url('#{base_url}#{url}.png');
background-image: e("url('#{base_url}x/#{url}.png')\9");
}
#demo {
.backgroundImage('large_cloud');
}
,
the Less compiler gives an error, and if I set height: 300px\9; like this, the compiler doesn't show any errors.
So, I'm very confusing. How to use Less hack for setting background-image in IE8.

Less is written to be compatible with proper CSS and so it may not always work properly when used for hacks. While some hacks like the height and color (in the thread linked below) may work, it is not a guarantee that all would work. If you need values with hacks to compile properly then it is safe to do CSS escaping for the value like below:
#demo{
background-image: ~"url('myimage-400px.png')\9";
}
or as
#demo{
background-image: e("url('myimage-400px.png')\9");
}
The above solution is purely one for the Less compiler error and I don't have much idea if that actually works in IE8 or not. If that method does not work then you may want to have a look at the options that are suggested in this SO thread.
Note: This question is similar to this one but it is not the same because that is about property name interpolation whereas this one is more about values.

You should avoid hacks altogether. Instead, conditionally serve a stylesheet to IE 8 users that addresses the version-specific issues you're facing.
<link href="styles.css" rel="stylesheet" />
<!--[if IE 8]><link href="ie8.css" rel="stylesheet" /><![endif]-->
Relying upon syntactic quirks often causes unintended side-effects, and generally come with a larger affected scope than advertised. Conditional Comments, on the other hand, are inherently designed to apply version-specific instruction.

Here's the technique I use and the trick to getting it to compile when writen in LESS.
1) write your background css for older browsers that doesn't include the background-size property.
eg: background:transparent url(/img/path-to-image-ie8.png) 0% 0% no-repeat;
2) write your background css for modern browsers that does include the background-size property. eg: background:transparent url(/img/path-to-image.png) 0% 0%/100% auto no-repeat;
Old browsers wont recognise the second line and will ignore it, modern browsers will overwrite the first line with the second.
The trick to getting it to compile in LESS is to write the / like this:
background:transparent url(/img/path-to-image.png) 0% 0%~"/"100% auto no-repeat;

Related

What happens when a CSS style isn't supported by the browser, but the property is?

I'm wondering what happens if a CSS style is supplied for a property which the browser supports, but the style itself isn't supported.
Take for example the following in IE8;
background: url(../path/to/img.png);
background: rgba(0,0,0,0.8);
Does IE8 simply ignore the second style due to it's lack of supported for CSS3 colours?
Thanks :).
Does IE8 simply ignore the second style due to it's lack of supported for CSS3 colours?
The answer is YES, it will completely ignore that value, and hence it won't render any color, it's a common practice to use a fall back with a hex value like
.class_name {
background: #000;
background: rgba(0,0,0,.5);
}
So, when you write the background twice, it's completely valid, the browsers who understand the rgba() will render an opaque background, but the browsers who don't understand rgba() will use #000.
Though, there are various workarounds for that, like, you can use :before or :after, with filter property with a negative z-index, which can be used as an opaque background, or you can do is, use a normal 1x1 px opaque png image only for IE8.
For example
background: url("IMAGE_URL_HERE")\9; /* Targets IE8 and below */

CSS Shorthand Efficiency

What is the difference in efficiency between background-color: #BADA55; and background: #BADA55;? I realize it's quite trivial, but is there a difference in how the browser extracts the values of each. Also, on a slightly related note, on CSS3Please I noticed that for linear-gradients they specified them using background-image. Is there any reason not to simply use background?
The spec makes no mention of how browsers should implement parsing of properties, and in particular, shorthand properties. All there is to it is a grammar, and the grammar says nothing about its implementation. How a browser parses a shorthand declaration then, I suspect, is entirely implementation dependent and not easily answered (unless you have the source code, of course).
In fact, the main reason why we have numerous CSS hacks specifically catered to IE is because of how differently (and often poorly) it understands CSS.
Is there any reason not to simply use background?
The answer lies in your previous sentence:
Also, on a slightly related note, on CSS3Please I noticed that for linear-gradients they specified them using background-image.
Indeed; CSS gradients are considered images for use with backgrounds, and are documented in the Image Values module. The individual background property they apply to is background-image.
If you use the shorthand property to specify either only the color or only a gradient, it will use the initial value for the rest of the values. If this difference in used styles matters, then the difference in performance becomes completely out of the question, because it's no longer a fair comparison.
In this example, the second background shorthand declaration will completely override the first one, leaving you with a solid color and no gradient as the initial value of background-image is none:
background: radial-gradient(white, rgba(255, 255, 255, 0)) /* transparent */;
background: /* none */ green;
The purpose of the shorthand notation is to specify values for multiple related properties in a single declaration, so in order for both the gradient and the color to apply, it should be rewritten as:
background: radial-gradient(black, transparent) green;
You're second example should be: background: #BADA55;, but either way is fine and should not be something you need to worry about.
If you are curious how browser parse CSS I can tell you: it depends (ever worked with IE?). For Chrome you can see the source on GitHub and the Firefox source can be found here.

IE(8 in 7-compat mode) uses "#media print" rule - on the screen

I have this rule which says "use this CSS rule ONLY FOR PRINTING.
#media print {
.yui-dt-bd {
width: 920px !important;
height: 100% !important;
page-break-before: avoid !important;
overflow: visible !important;
position: static !important;
}
}
The link-to-CSS-file with media-specification shows the same result, by the way:
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
However, what do I see when I debug my page with the IE8 dev. tool (F12)? It applies those rules to the page (media=screen)!
I looked long and hard but found nothing on the web, only the "IE ignores this or that", which is the opposite of MY problem.
IE dev. tool window says: "Browser Mode: IE7, Document Mode: IE7 Standards". I don't use any #import statement anywhere (this was an issue in some other IE CSS questions I found so I mention it). All 5 rules (above) are applied (on the screen media).
EDIT: Updated and follow-up question merged into this one.
EDIT: This is not a CSS3 media query, which is of course not supported by IE8 and below. This is media dependent CSS. I did not find anything conclusive, but it seems that THIS SHOULD work, as long as I don't use CSS3 media query features, which seem not to include THIS example.
IE8 and below do not support media queries, so your issue might be related to an unclosed tag or conditional comment somewhere in your css or page. Run a CSS linter or validate your css to see if you catch any tags that might need closing.

declaring background property in css twice

I am reverse engineering a previous employee's work and noticed a number of css classes look like this...
.img-shadow {
float:left;
background: url(../images/shadowAlpha.png) no-repeat bottom right !important;
background: url(../images/shadow.gif) no-repeat bottom right;
}
Can anybody think of a reason for a css class to declare background twice like this (specifically with the !important)?
According to wikipedia, the second background rule is for IE6.
Internet Explorer 6 and below also
have a problem with !important
declarations when the same property of
the same element has another value
specified within the same code block,
without another !important
declaration. This should result in the
second value being overridden by the
first, but IE6 and lower do not honor
this.
It's a cheap PNG fix for IE6. Since IE6 won't recognize the !important tag, it will use the GIF background, while all other browsers will use the PNG.
Older versions of IE will use the last one.
These versions had problems with png transparency.
looks like he's attempting to support browsers that don't handle alpha .png's properly (cough IE6 cough)

Is there any way other than javascript to fix IE 6 bugs?

For IE 6 we have plenty of bugs to bug us as a designer.
incorrect box model etc etc.
i have searched for fixes via JavaScript and found
[link text][1]
IE7.js
IE7 is a JavaScript library to make Microsoft Internet Explorer behave like a standards-compliant browser. It fixes many HTML and CSS issues and makes transparent PNG work correctly under IE5 and IE6.
but do we have real life saver other than javascript via css.
Ways to deal with IE6 bugs with CSS? Sure.
See: http://www.quirksmode.org/css/condcom.html
for conditional comments
There are other ways, such as adding some specific characters in some CSS properties that get ignored in some browsers but not in others.
However, in some cases, web designers should be very cautious when using these.
The alternative is to live within the IE 6 world of bugs and design your pages to look right despite them. You can serve up different css for your IE6 clients, or even different html if necessary, depending on your design. In some cases, you can use one CSS file that will mean different things to IE6 clients, but that technique is problematic with respect to IE7 and 8.
this link is also handy one
How do you deal with Internet Explorer?
I never knew this - thanks svinto
"IE6 doesn't have the incorrect box model unless you have the wrong doctype. – svinto"
There are some simple stylesheet hacks that can modify the presentation in various internet explorer versions to solve your CSS problems. For example these three:
Simplified box model hack for IE4, IE5, IE5.5:
div.values { margin: 10px; m\argin: 20px; }
star html hack for IE4, IE5, IE5.5 and IE6:
* html div.values { margin: 5px; }
star first-child+html hack for IE7:
*:first-child+html div.values { margin: 5px; }
PNG transparancy issues could be solved with solutions like this:
<div style="width:50px;height:50px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/logo/logo.png');">
<img src="/images/logo/logo.png" height="50" width="50" alt="" style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" />
</div>
Great info so far but one thing to note is that IE7.js doesn't fix pngs in all cases (at least last I looked). For instance, you won't be able to tile a background image with transparency.
In the case of DXImageTransform you may find that when this is applied to elements that contain links, those links are no longer 'clickable'. You can sometimes fix this by giving the parent element that has the transform applied to it static positioning and to position the child anchor element e.g.,
h2{
position:static;
zoom:1;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/mypng.png", sizingMethod="scale");
}
h2 a{
position:relative;
}
<h2><a href="" >a link!</a></h2>
If you have to do this sort of garbage put it in a separate stylesheet and control loading with conditional comments. If the design is of any complexity try you best not to support ie6 or <. If you can't avoid doing it, charge more ;). Sometimes that is enough to persuade someone that supporting ie6 isn't "worth their while".
why don't you try FireBug Light for IE? It's not as powerful as FireFox FireBug but can be helpful
Many bugs can be worked around in CSS using conditional comments or CSS selector hacks. But there are some bugs that CSS hacks alone cannot handle such as IE6's .multiple.class.selector.bug
There's another quick and dirty hack for IE6 styles
for e.g.
You can define the CSS as;
.divTitle
{
padding: 5px;
width: 600px;
_width: 590px;
}
All the other browsers picks up 600px as the width value & IE6 overwrites it & take 590px;
I've tested this in IE7 & FF as well.
Also you may want to check this link;
link text

Resources