Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Tuesday, June 22, 2010

CSS Hacks Compiled


A compiled version of mostly used CSS hacks/filters used for Cross Browser Compatibility issues. (Ahh and yes the issues are always because of IE, more specifically a poor level of Standard Support in IE  :P )


In the list below first 4 points are preferred. By preferred it doesn't mean to always use these. These hacks are only to be used when needed. For some of hacks it will not be a Valid CSS. So better use these with caution. 


1. Conditional

SYNTAX:::
Positive
<!--[if condition]> HTML <![endif]-->
Negative
<!--[if !condition]><![IGNORE[--><![IGNORE[]]> HTML <!--<![endif]-->
condition is one of the following:

IE
For ALL versions of IE
lt IE version
IE versions less than version
lte IE version
IE versions less than or equal to version
IE version
Only specified version of IE
gte IE version
IE versions greater than or equal to version
gt IE version
IE versions greater than version

version is the version of Internet Explorer 5, 5.5, 6, or 7

NOTE: Versions of IE older than IE 5 don't support conditional comments, you may get unexpected results in those browsers.
NOTE: Versions of IE for MAC don't support conditional comments.

<head>
<title>Test</title>
<link href="allBrowsers.css" rel="stylesheet" type="text/css">
<!--[if IE]> <link href="onlyIE.css" rel="stylesheet" type="text/css"> <![endif]-->
<!--[if lt IE 7]> <link href="belowIE6.css" rel="stylesheet" type="text/css"> <![endif]-->
<!--[if !lt IE 7]><![IGNORE[--><![IGNORE[]]> <link href="new.css" rel="stylesheet" type="text/css"> <!--<![endif]-->
<!--[if !IE]>--> <link href="gudBrowsers_notIE.css" rel="stylesheet" type="text/css"> <!--<![endif]-->
</head>

2. !important

background: green !important; /* Major browsers other than IE 6 and below respect the importance immediately */
background: red; /* IE 6 and below use this value instead, even though the above was marked as important */

3. @import

@import "styles.css" all; imports the stylesheet in all major browsers except IE 7 and below. It may or may not work in future versions of IE.

4. body[class|="page-body"] {} selects the body element with the class page-body in IE 7 and all modern browsers except Opera 9 and below. It may or may not work in future versions.

5. _property: value and -property: value apply the property value in IE 6 and below. Warning: this uses invalid CSS.

6. *property: value applies the property value in IE 7 and below. It may or may not work in future versions. Warning: this uses invalid CSS

7. body:empty {} selects the body element in Firefox 1.5 and 2.0 only. It may or may not work in future versions. Warning: this uses invalid CSS 2.x but valid CSS 3 according to recent drafts.

8. >body {} selects the body element in IE 7 only. It may or may not work in future versions. Warning: this uses invalid CSS!

9. html* {} selects all descendants of the html element in IE 7 and below. It may or may not work in future versions. Warning: this uses invalid CSS!

10. The !ie identifier allows the property to be applied in IE 7 and below. It may or may not work in future versions. Warning: this uses invalid CSS!

11. The !important! identifier allows the property to be applied with importance in IE 7 and below and the property is not applied in other browsers. It may or may not work in future versions. Warning: this uses invalid CSS!

Thanks to http://www.webdevout.net/css-hacks. Please do visit it for details. I have changed a few things for easy understanding and quick reference.

Friday, December 11, 2009

Rounded Corners Compatible with All Browsers


Example text


To have the above effect, we will use some CSS and Nested Div. For this we will also require images of corners only, like the square image of top left corner etc..

We will start with our CSS.

First all we will need a class which will have a border. It will be bounding box of rounded block.
.rounded {
border: 1px solid #ffffff; /* border in white color */
background-color: #ffffff; /* background color */
zoom:1; /* for IE to work properly */
}
As our initial step, we will have the following stuff.
<div class="rounded" style="width: 200px;">  <div style="padding: 20px;">Example text</div> </div>
This will display a box with straight border in white with white background. Our surrounding is black. Now for corners, we will make a separate class which will have its respective image as background in CSS and positioned at its respective place.
.tl {background: url(tl.gif) no-repeat top left;
margin:-1px; } /* the margin -1 aligns borders with corner images*/
.tr {background: url(tr.gif) no-repeat top right;}
.br {background: url(br.gif) no-repeat bottom right;}
.bl {background: url(bl.gif) no-repeat bottom left;}
For each corner we have a class ready with respective corner image. Now we will just create divs for each corner and apply its class to it as follows:
<div class="tl"><div class="tr"><div class="br"><div class="bl">
     <div style="padding: 20px;"> Example text </div>
</div> </div> </div> </div> </div>
This way we will have a rounded cornered block. It will work in all browsers.

Complete code is as follow::::<style type="text/css"> 
.rounded {  
border: 1px solid #ffffff;  
background-color: #ffffff;  
color: #000000;  
zoom:1; /* causes IE to behave properly */  
}  

.tl {background: url(tl.gif) no-repeat top left;  
margin:-1px; } /* the margin -1 aligns borders with corner images*/  
.tr {background: url(tr.gif) no-repeat top right;}  
.br {background: url(br.gif) no-repeat bottom right;}  
.bl {background: url(bl.gif) no-repeat bottom left;}  

.tr, .tl, .bl, .br {zoom:1;position:relative;} /* for relative positioning of div */

</style>  

<div class="rounded" style="width: 200px;"> 
<div class="tl"><div class="tr"><div class="br"><div class="bl"> 
<div style="padding: 20px;"> Example text </div> 
</div> </div> </div> </div> 
</div>

Developer Instincts