Handpicked Hacks for CSS

by Admin on March 31, 2009

If you are front end coder you must know how important is to make cross browses, valid CSS and xHTML code. And also you must know how much time we are spending in all those hacks and fixes for various browsers. I’ve written about some of them earlier on PNG transparency issues, Yellow fields in web form, Vertical align div etc..

Here is the list picked CSS hacks and tricks which can help you in your CSS code and also save some time.

1. Vertical align div

One of current CSS left outs is vertical align div. And before CSS 3 comes we have to use some tricks to solve this problem. I looked over for some solutions and it all comes up to defining it as table and using vertical align. The second one appears a lot and it?s a nice solution, but it has two faults, I?ll tell you later about them, here?s the code first.

Idea is to place absolute div 50% left and top and then to move margin left and top half if it?s size.



Code:
.wrapper {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}

Problem?

This works only with defined height and when div height is less then browser height so you can?t have scroll. Now you?ll ask why do anyone want vertical align when there?s a vertical scroll? Well for example you have container height 820px and you have it vertically centered in all larger resolutions, simple.

Solution

In this problem (when browser height is less than div height) at smaller resolutions 1/3 of div is not visible, it?s in negative top margin. So idea is to place some relative div that will prevent div to go into negative margin. Here is the code.

Code:

Content
html, body { height: 100%; margin: 0; padding: 0; } * { margin:0px auto; padding:0; } div#shim { visibility: hidden; width: 100%; height: 50%; margin-top: -200px; float: left; } div#wrapper { width: 1000px; height: 400px; clear: both; background:red; position: relative; top: -200px; /* IE4ever Hack: Hide from IE4 **/ position: static; /** end hack */ } /* Hide from IE5mac \*//*/ div#shim { display: none; } html, body { height: auto; } /* end hack */ /* ]]> */

2. Min-Height

Code:
selector {
min-height:500px;
height:auto; !important
height:500px;
}

3. PNG transparency

If you are CSS coder, you?ve probably ran in to png transparency problems many times. Even Microsoft is having problems with it http://runonce.msn.com/runonce2.aspx open in IE6. So I?ll tell you few problems and solutions I?ve handled so far.

First one is if you need just simple transparent image, without some special needs for example backgrounds etc. The solution would be png fix. I?ve used it many times but it has lots of faults, for example with padding, margins and absolute positioning. Sometimes i can mess up the rest of your JavaScript files, but anyway very useful script. You can download it here http://homepage.ntlworld.com/bobosola/pngfix.js and just include it in your tag



Code:


The second one can partly handle issues from first and it?s pure CSS solution. I mostly use this for backgrounds, because if your path for a background is from CSS file then png fix can?t handle it.


Code:
.someelement {
background-image: url(images/image.png);
}
* html .someelemen {
background-color: #333;
back\ground-color: transparent;
background-image: url(images/blank.gif);
filter: progid:DXImageTransform.Microsoft.
AlphaImageLoader(src=?images/image.png?, sizingMethod=?scale?);
}

I found this very useful, and also when you add some hover effects for example some color or other image. You can see live example here

NOTE: This simple hover is just example and it doesn?t work in IE6

Code:
 .someelement:hover {
background: #333;
}

Code:
.someelement:hover {
background-image: url(images/image2.gif); 
}

Only problem I had with this was with tag, link just don?t work (when it?s in div with this kind of background) and i don?t know why (talking again for IE6). And only solution i could think of was to place another absolute div over it for the links, content etc? Yes i know it?s not elegant but it works if content is not dynamic.

Luckily more and more people are starting to use IE7, and i must say i have recently switched too, i had to have IE6 because of testing my code and when i found out for
Multiple IE program I switched immediately

4. Autoclear

Code:
.container:after {
content: ?.?;
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.container {display: inline-table;}
/* Hides from IE-mac \*/
* html .container {height: 1%;}
.container {display: block;}
/* End hide from IE-mac */

5. Reset CSS

Code:
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,
fieldset,input,p,blockquote,th,td {
margin:0; padding:0;
}
table {border-collapse:collapse;border-spacing:0;}
fieldset,img {border:0;}
address,caption,cite,code,dfn,em,strong,th,var {
font-style:normal;font-weight:normal;
}
ol,ul {list-style:none;}
caption,th {text-align:left;}
h1,h2,h3,h4,h5,h6 {font-size:100%;}
q:before,q:after {content:?;}

6. Scrolling Render IE

Code:
html {
background : url(null) fixed no-repeat;
}

7. Opacity

Code:
#transdiv {
filter:alpha(opacity=75);
-moz-opacity:.75;
opacity:.75;
}

8. PRE Tag

Code:
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}

9. Li Background Repeat IE

Code:




10. Good to know

Code:
@charset ?UTF-8?;
/* ???????????????????????-
WinIE7
???????????????????????- */
*:first-child+html selector{property:value;}
/* ???????????????????????-
WinIE6 & Mac IE
???????????????????????- */
* html selector{property:value;}
/* ???????????????????????-
WinIE6
???????????????????????- */
/*\*/
* html selector{property:value;}
/**/
/* ???????????????????????-
MacIE
???????????????????????- */
/*\*//*/
selector{property:value;}
/**/ 

__________________

Popularity: 1% [?]

Leave a Comment

Previous post: Buying high PR links from blogs – Dangerous ?

Next post: Can Google Adsense Change Payee Name ?