Modern browsers have support for RGBA colours, allowing you to have semi-transparent background colours. Unfortunately, this only works in awesome browsers (everything except IE 8 and below). However, IE does support a custom gradient filter. Whilst it's commonly used to render gradients (obviously), it supports alpha transparency. If you set the start and end colours to be the same, this has the same effect as setting an alpha value on the colour.
This involves quite a lot of CSS for each alpha colour you want to use. We can automate this tedious code generation through a LESS mixin. If you're still using 'pure' CSS, I'd highly suggest looking into LESS and SASS, they're extremely handy. In any case, I use a mixin similar to the following:
.rgba(@colour, @alpha) { @alphaColour: hsla(hue(@colour), saturation(@colour), lightness(@colour), @alpha); @ieAlphaColour: argb(@alphaColour); background-color: @colour; // Fallback for older browsers background-color: @alphaColour; // IE hacks zoom: 1; // hasLayout background-color: transparent\9; -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=@{ieAlphaColour}, endColorstr=@{ieAlphaColour})"; // IE 8+ filter: ~"progid:DXImageTransform.Microsoft.gradient(startColorstr=@{ieAlphaColour}, endColorstr=@{ieAlphaColour})"; // IE 6 & 7 }
Note that IE requires the element to have layout in order to apply filters to it (hence the zoom: 1 hack), and IE 8 changed the filter syntax to use -ms-filter instead. This LESS mixin can be used as follows:
#blah { .rgba(black, 0.5); }
This will set the element to a 50% black background. This mixin could be converted to SASS quite easily, too. Ideally I would have liked to apply the IE styles in a better way (like using conditional comments to set classes on the element) but I couldn't get this approach working with LESS.
Hope this helps someone!
Until next time,
— Daniel
Comments
It's just helped me.
Thanks a lot.
Best regards from PAris, France
Awesome, glad it helped you :)
Could you check this ? On my IE9 .rgba() on <h2> crashes my browser.. no problem on a <div>
It crashes the browser? Wow, that's very odd. Do you get an error message of any sort?
I read like 5-6 pages until I've arrived here.
Thank you, this saved me a lot of time.
No worries, glad it helped :)
thanks this saved me a few hours!
Great stuff! Helps a lot. Tip: refer to the doc how to use mixins (http://lesscss.org/) to make it even easier for beginners. Regards from Germany
Excellent ! Thank you !
Thanks for this mate, saved my bacon. IE10, however, doesent like the way the styles are ordered, ignoring them all. Reordering them like this, works. https://gist.github.com/416...
Thanks for the info! Does the reordering work in older IE versions as well?
Does anyone know of a SASS equivalent?
Hi nottRobin,
a mixin in SASS/SCSS could look like
https://gist.github.com/cas...
Thank you so much =)
Glad to help!
This is awesome
Thanks for useful mixin. But I need your help, how can I apply this code without affect child element?