As web designers and developers, we have all come to learn many css tricks and techniques that help us achieve our layout goals. The list of these techniques is an ever expanding one, however, there are certain tricks that are essential to achieve your goal. Today, we will review 20 excellent css techniques to keep in mind when developing your theme.
1. Absolute positioning inside a relative positioned element.
Putting an absolutely positioned element inside a relatively positioned element will result in the position being calculated on its nearest positioned ancestor. This is an excellent technique for getting an element to “stick” in a certain spot where you need it, for instance, a header badge.

Read more about positioning:
2. Z-Index and positioning.
z-index can be somewhat of a mystery to developers. Often, you will find designers putting a very large z-index value on a div or element in order to try and get it to overlap another element. What we need to keep in mind, is that z-index only applies to elements that are given a “position” value. If you find an element will not adhere to a z-index rule you’ve applied, add “position:relative” or “position:absolute” to the troublesome div.

Read more about z-index:
3. Margin Auto
Using margin auto in a theme is a fantastic way to get an element centered on the screen without worrying about the users screen size. However, “margin: auto” will only work when a width is declared for the element. This also means to apply margin: auto to inline elements, the display first must be changed to block.

Read more about margin auto:
4. Use Padding Carefully and Appropriately
One mistake I often made when starting off with css was using padding without knowing all the effects and the CSS Box Model. Keep in mind that according to the box model, padding adds to the overall width of the element. This can cause a lot of frustration with elements shifting out of place. For example:
#div {
width:200px;
padding: 30px;
border:2px solid #000;
}
Equals a total width of 264px (200 + 30 + 30 + 2 + 2). In addition, remember that padding, unlike margins, cannot contain negative values.
Read more about padding:
5. Hiding text using text-indent
Lets say you have an image you are using for your websites logo. This image will be inside an h1 tag, which is good for SEO, however, we also want to have our text title written in the h1 tags so the search engines can read it easily. Some may be tempted to use “display:none” on an element. Unfortunately, we would have to separate the image logo from the h1 tag if we used this technique. Using text-indent and negative values, we can get passed this like so.
h1 {
text-indent:-9999px;/*Hide Text, keep for SEO*/
margin:0 auto;
width:948px;
background:transparent url("images/header.jpg") no-repeat scroll;
}
This will ensure that all text is not visible on any resolution while allowing it stay inside the h1 element containing the logo. This also will not hide the text from screen readers as display none will.
Read more about using text-indent to hide text:
6. IE Double Float Margin Bugs
I’m sure we have all dealt with this one, as this is one of the most common css “hacks” we need to use. If you haven’t seen this bug before, basically, a floated element with a given margin suddenly has doubled the margin in IE 6 and has dropped out of position! Luckily, the fix is super simple. We just change the display of the floated element to “inline” as seen below.
.yourClass {
float: left;
width: 350px;
margin: 20px 0 15px 100px;
display: inline;
}

This change will have no effect on any browsers since it is a float element, but for some reason in IE it fixes the double margin issue.
Read more about IE’s margin bug:
7. Using CSS to Fight Spam
This would be something you could include to really spice up your theme description. Alen Grakalic of CSS-Globe.com wrote a fantastic post on how to use css as a kind of CAPTCHA technique. A form is declared like so:
<label for=”Name”>Name:</label>
<input type=”text” name=”name” />
<label class=”captcha” for=”captcha”>Answer?</label>
<input type=”text” name=”captcha” id=”captcha” />
For the id “captcha”, we use a background image via css. This would require the spam scripts to find your html element, scan your css, compare selectors, find the certain selector and background image, and then read that background image. Its pretty safe to say most wont be able to do this. The downside is if someone is not surfing with css enabled, they wont know what to do.

Read more about using css to fight spam:
8. PNG in IE 6 Fixes
I’m sure we could all agree dealing with transparent png’s in IE 6 is a real headache. The fixes range from complex Javascript techniques to just using a Microsoft proprietary filter, to using conditional comments to swap them out for a .jpg. Keep in mind, all of these but the conditional comments rely on the Microsoft AlphaImageLoader.
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(...);
Read more on how to fix IE 6 PNG transparency:
9. CSS Cross Browser Transparency
Believe it or not, it is pretty simple to get decent cross browser transparency using css. We can cover, IE, Firefox, Safari, Opera, and old browsers like Netscape Navigator. Chris Coyier recently came to our rescue again demonstrating these techniques.
.yourClass {
filter:alpha(opacity=50);/*Needed for IE*/
-moz-opacity:0.5;/*Older mozilla broswers like NN*/
-khtml-opacity: 0.5;/*Old versions of Safari and "KHTML" browser engines*/
opacity: 0.5;/*FF, Safari, and Opera*/
}

This wont validate, but its not really an issue and the ThemeForest staff is pretty understanding when it comes to techniques like this.
Read more about CSS Opacity
10. Use CSS Image Sprites
CSS Image sprites are a fantastic way to load many of your css images at one time, in addition to reducing http requests and the file size of your theme. In addition, you wont have to deal with any images “flickering” on hover.
CSS Image sprites are achieved by putting many of your image elements all into one image. We then use css to adjust the background position, width, and height to get the image where we want it.

Resources for image sprites:
11. Use Conditional Comments to support IE 6
Far too often, web developers are forced to introduce new css rules and declarations that only apply to certain versions of IE. If your not familiar with a conditional comment, the code below would only link to a style sheet if the users browser is less than or equal to IE 7:
This code would go in the head section of your html file. If the css does not seem to be taking place in IE after you have linked to your specific style sheet, try getting more specific with your css selections to override default styles.
Read more on conditional comments:
12. CSS Specificity
As mentioned above, CSS styles follow an order of specificity and point values to determine when styles override one another or take precedence. Nettuts recently had a nice article in which the point values for css were explained. They are like so:
- Elements - 1 points
- Classes - 10 points
- Identifiers - 100 points
- Inline Styling - 1000 points
When in doubt, get more specific with your style declarations. You can also use the !important declaration for debugging purposes if needed.

Read more about css specificity:
13.Achieving a minimum height in all browsers.
When developing, we often realize we need an element to have at least a certain height, and then stretch to accommodate more content if needed. Unfortunately, IE doest recognize the min-height property correctly. Fortunately, we have what is known as the min-height fast hack, that goes like so:
#yourId {
min-height:300px;
height:auto !important;
height:300px;/*Needs to match the min height pixels above*/
}

Simple, effective, and it validates just fine. This is also one of the few cases when the !important feature comes in great handy.
Read more about the min height hack:
- Using the min height fast hack
- More on the Star HTML Bug
- Explanation of the star HTML bug
- Subscribe to the ThemeForest RSS Feed for more daily web development tuts and articles.
14. The * HTML hack
If you need or wish to avoid linking to IE specific style sheets, one can use the * html hack. In a perfect world, the HTML element will always be the root element, so any * before html would not apply. Nevertheless, IE treats this as a perfectly legitimate declaration. So if we needed to target a certain element in IE, we could do this:
* html body div#sideBar {
display:inline;
}
Read more about * html hack:
15. Sliding Doors Technique
One major problem with using images for navigation buttons, is that you run the risk of the clients text being too long and extending past the button, or being cut off. Using two images and the css sliding doors technique, we can create buttons that will expand to fit the text inside. The idea behind this technique, is using two images for each button, and applying the images via a background declaration in CSS. For example:
HTML Markup:
Your Title
CSS:
a.myButton {
background: transparent url('right.png') no-repeat scroll top right;
display: block;
float: left;
height: 32px; /* Image height */
margin-right: 6px;
padding-right: 20px;/*Image Width*/
/*Other Styles*/
}
a.myButton span {
background: transparent url('button_left.png') no-repeat;
display: block;
line-height: 22px; /* Image Height */
padding: /*Change to how you see fit*/
}

Read more about sliding doors technique:
And there you have it, a list of 15 css techniques to help you when developing a theme. CSS is great for designers as it allows us to be creative with code and use our own techniques to accomplish a job. That said, what are some of the techniques the developers of ThemeForest use? What would you add to the list?




Jhay
November 28th, 2008
Very informative. Thanks!
Bookmarked.
abdusfauzi
November 28th, 2008
this is a very informative compilation. every coders need this!
Marco
November 28th, 2008
Great article! I personally use “text-ident:-900%”, but that’s just a matter of taste (”In case there are screens with a screenresolution with over 9000 px”).
Here’s another fun way to fight spam with CSS:
http://www.marcofolio.net/css/hiding_email_addresses_from_spambots_using_css.html
Kevin
November 28th, 2008
This is a great article - but I did want to add that the text-indent trick only works on block display elements, in case you wanted to use this in other places on your site.
Drew
November 28th, 2008
@Kevin-Thanks for pointing that out, I forgot to mention that but your absolutely correct
Thanks for the kind words everyone, I appreciate it.
Drew
Steve
November 29th, 2008
Tips 14 & 15 seem to be indented to the left. *Using Opera 9.62.*
WonkaStudio
November 29th, 2008
Very good informative compilation, thank you for this CSS tricks !
Abethebabe
November 29th, 2008
Wow! Lots of useful info, definitely bookmarking this for reference later
kiplog
November 29th, 2008
Yea, as Steve says. Close your UL tags after the “Using the min height fast hack” and * hack bullets.
Also, careful when explaining specificity as points - 10 elements do not equal one class and 10 classes won’t equal one ID. See the Star Wars and Poker links at the Smashing Mag link for brilliant explanations.
Markus
November 30th, 2008
thanks
helpful tipps and tricks
Andy Ford
November 30th, 2008
RE: #8
“Google’s IE 7.js” is more accurately “Dean Edwards’ IE7.js hosted by Google Code”
Rajiv Vishwa
November 30th, 2008
Neat and Simple!
Chriscandy
November 30th, 2008
Really helpful and bookmarked.
Cheers
Joe
November 30th, 2008
… sorry, but borring. 99% of this “hints” are standard for every web developer!
Goofydg1
November 30th, 2008
Nice work.
Mel
November 30th, 2008
nothing new here
also “7. Using CSS to Fight Spam” has major accessibility drawbacks, screen-readers would not be able to read the captcha
cwmonkey
November 30th, 2008
#12. I believe what you said about specificity isn’t exactly true. There are no point values, just tiers of specificity. I experimented with putting 101 elements to see if it would clobber a class, and it didn’t.
#5. The text indent trick won’t work in IE on button elements, you can use line-height: 1000px to hide it.
#14. Here are some more * html style hacks for you:
IE6:
* html #ie6 { background-color: red; }
IE7:
*:first-child+html #ie7 { background-color: blue; }
Safari2:
body:last-child:not(:root:root) #safari2 { background-color: yellow; }
“Modern” browsers:
html>body #modern { background-color: pink; }
Safari 3:
html*:first-of-type #safari3 { background-color: orange; }
html*#id_of_body:first-of-type to target the body
Safari 2/3:
html* #safari2and3 { background-color: gray; }
html*#id_of_body to target the body
Barbaros Alp
November 30th, 2008
“One of the greatest tricks come together” article
Thank you very much
Jarryd
November 30th, 2008
I found that the conditional comments weren’t appearing in #11, anyone else found this?
Also with #5, text-indent may not be the best option. For example, when a user has images turned off, a blank space will be left.
Adding a span tag inside your header tags and throwing in your image replacement and positioning it above the text will solve this.
Nevertheless, a great reminder article!
Melvn Walls
November 30th, 2008
Great post. Already use a bunch of these but still a very nice refresher
Blog Bloke
November 30th, 2008
And here I thought I was a geek. Well done!
Scott
November 30th, 2008
You really ought to rename the article, “15 CSS Tricks for The Complete CSS Noob.” Valuable information, but it’s all VERY basic stuff for anyone who has more than 12 months of experience styling content with CSS.
FYI - As @Steve mentioned, tips 14 and 15 are both indented in Firefox 3 as well.
Matías
November 30th, 2008
Interesting
Veera
November 30th, 2008
Very good tutorial. The points about margins and padding were really informative.
jason
November 30th, 2008
if you use the text replacement method as described here and the user is browsing without images then you’re missing an important part of your design. I believe you should nest a span inside your h1 tag and apply the image to that.
core
December 1st, 2008
Thanks for this list. Very useful.
Ben van de Sande
December 1st, 2008
One of the better css trick list in a long time. Thanks!
Selvam
December 1st, 2008
Really very useful one for all… Keep this…
sarimarton
December 1st, 2008
Sliding door technique is a bullshit. It’s a waste using two images for that.
http://vacskamati.blogspot.com/2007/07/making-custom-tabs-in-css.html
lowell
December 1st, 2008
eh.. old, basic stuff
fyi - the layout for your site is broken in mac os x/firefox 3.. the content is below the navigation.
devolute
December 1st, 2008
This is ’standard’, but forms a great list of questions to ask at an interview, imho.
Attilio Viscido
December 1st, 2008
Thanks !
Michael Stum
December 1st, 2008
Some really nice tricks (some maybe old, but #1 alone was worth it for me). I just want to comment on using the AlphaImageLoader for Transparent PNG in IE6: It can lock up your browser. We just had to remove our use of it after half of the clients Browsers locked up.
There is a blog post about this issue here:
http://blogs.cozi.com/tech/2008/03/transparent-pngs-can-deadlock-ie6.html
Aaron Irizarry
December 1st, 2008
Great List! very useful thanks.
frank
December 1st, 2008
Very helpful. Thanks.
@lowell - Not everyone is at your expert level of design. Sorry to disappoint you. Maybe you should write your own expert blog.
GeorgeM
December 1st, 2008
Very nice for us nonhardcore css developers
joyoge designers' bookmark
December 1st, 2008
very useful, thanks a lot…
Ivan
December 2nd, 2008
Great Article.! just learned some great css techniques !
Amber Weinberg
December 2nd, 2008
Great tips. I’ve learned a lot of these in just the past few months and they’ve really helped cut my coding time.
Ilya Radchenko
December 2nd, 2008
I love how you present everything, very clean and understandable. Keep at it!
amitesh
December 3rd, 2008
too good article
omon
December 3rd, 2008
very good article !!!
especially,
“10. Use CSS Image Sprites” is great informative
Jason
December 3rd, 2008
Damn good article. Thanks!
wallpapers
December 3rd, 2008
good work - thanks
Joe Didday
December 3rd, 2008
Excellent article!!!
Sliding door technique is awesome!!!
I think most of the comments saying “old, basic, or boring” are just trying to toot their own horns and stroke their big EGO.
PLEASE keep up the good work.
Drew
December 3rd, 2008
Thanks very much for all the positive encouragement guys, its what keep me going
John
December 4th, 2008
Nice article. Bookmarked.
Alex
December 4th, 2008
First paragraph says “20″ yet title says 15 hints. Just a typo I guess. Figured i’d let you know.
Garrett
December 4th, 2008
Anyone notice the outline onpress when using text-indent for block anchors. Try this next time -moz-outline: none;
Drew
December 4th, 2008
@Alex, yup just a typo
Thanks for letting me know, I’ll see if Jeff can change it.
jj
December 8th, 2008
cooool
Mark
December 9th, 2008
Nice list.
I realise that most of the “precious” amongst us know all this stuff and are happy to tell everyone that, but i appreciate the effort that went it to this and I’m sure at some point it will make good reference.
Thanks again
Joseph R. B. Taylor
December 9th, 2008
Nice post with a good collection of items that come up frequently.
Eberhard Doerr
December 9th, 2008
These are mostly _hacks_ - they work but they leave a simple rule in the dust:
Code should be self-explanatory.
“text-indent:-9999px” .. great ..
Maybe the idea here is sth else:
Code should keep me in business (because no one else understands it).
Happy coding!
Carter Creative Design
December 9th, 2008
Awesome, thanks guys!
Always hard to keep up with all the new CSS stuff and stylings. Your page is bookmarked!
Jim
John Faulds
December 11th, 2008
@ #5 - probably the image-replacement method that I’d least recommend because, as others have pointed out, if images are turned off (e.g. if you’re browsing on a handheld device), you get nothing.
Also if the content of the image is important and you want it to appear when printing a page, background-images can’t be printed by default, so again, it won’t appear. Better to put the image in the HTML and use the alt attribute to describe the text contained in the image (ie, if the image says ’sign up’, that’s what the alt attribute should say).
Dave
December 12th, 2008
Overall, this is a great article for people who just need a refresher, but I can’t recommend it to newbies yet because of a few things:
12 - cwmonkey touched on this, but tried to cushion the blow. I won’t. The 1/10/100/1000 way of explaining this concept is FALSE and MISLEADING. It’s like teaching tables for layout — technically, it will probably work, but it’s bad practice and has the potential to cause more problems down the road.
4 - if you’re going to give specific workarounds for IE6 (as you do in Tricks 6 and 8), then you really should comment in Trick 4 about how IE6 does not correctly implement the box model and how to avoid the problem without hacks (basically, don’t put width and padding on the same block element).
That said, you really have identified the most important, core, re-usable concepts for using CSS in the real world and illustrated them in a really understandable way.
Steve
December 12th, 2008
Wow; #1 helped me right away! All I had to do was declare the parent container as being relative and my absolute position worked like a charm.
Thanks so much!
David Hucklesby
December 12th, 2008
Re: Tip #3 - margin-left/right: auto is useful for more than just centering. viz:
margin: 0 auto 0 0; /* align left */
margin: 0 0 0 auto; /* align right */
Nice set of tips, and unusually useful. Thanks.
Indranil
December 15th, 2008
Re: Tip#3
If you don’t know the width of the element, just use display: table.
Re: Tip#5
You might also want to add font-size: 1px; so that the text doesn’t go into the next line.
zerolux
December 22nd, 2008
while most is beginners stuff, it’s still awesome for the millions of people who are just starting to get into CSS. good stuff.
oh and the reason your layout looks weird for #14 and #15 is because you forgot to close your tags for numbers #13 and #14.
Hasan Tayyar BEŞİK
December 23rd, 2008
Sometimes some jquery codes could b better for a plain html code.
Rasmus
January 17th, 2009
Nice.
Isn’t there a problem with IE6 and auto margins?
Banhawi
January 17th, 2009
just Awesome ,
Thanks
Sam Doyle
January 22nd, 2009
Thanks for the tips!
Jamie Favreau
January 25th, 2009
Very informative I hope that it helps me with my background on Twitter and I can understand what the heck is going on with my Blog page.
Navdeep
January 25th, 2009
Very well put up! This will sure help every css starter…
Saurabh Shah
January 25th, 2009
very useful tips … thanks for sharing
Marvin
January 26th, 2009
very good, but known list.
Timothy
January 26th, 2009
Sprites are always good. And thanks for refreshing me on the absolute / relative positioning.
sivas
January 29th, 2009
Great
Thank you
masal
January 29th, 2009
thank you
Alex
February 8th, 2009
There is another solution to use transparent PNG in IE 6:
http://www.sitepoint.com/blogs/2007/09/18/png8-the-clear-winner/
didi
February 8th, 2009
thank you and bookmark!
D_E_R_M_A_N
February 14th, 2009
thanks…………………….
sivas
February 15th, 2009
very good, thanks
mojitopl
February 17th, 2009
that is hell of a good work here! Very useful for beginner like me. And to ALEX for mentiong the List Apart png technique
mojitopl
February 17th, 2009
of course Site Point… oops (well that is an obvious tip to go to sleep
omega2
March 6th, 2009
a good web design resource
MegaFill
March 10th, 2009
Oh… very good post! I translate this on my language and pos in my blog
THX!
MegaFill
March 10th, 2009
Oh… sorry for second message…
Drew Douglass, I’m going to tranlate into russian, can you add to the bottom of the page link “Russian version…”
Mahbub
April 19th, 2009
Wow. Nice put together. You should also write some # tricks to fight IE
MorayWeb
April 23rd, 2009
Nice summary and some great tips - bookmarked for reference!
Cheers!
bruno
May 1st, 2009
Great tricks.
I will use some in the website i am making.
Wish to see more css tutorials.
Sebq
May 1st, 2009
very helpful tips…nice compilation
anoop
May 1st, 2009
very useful tricks…thanks!
rik
May 1st, 2009
nice set of posts. very handy to look back upon..
Brian
May 3rd, 2009
Great set of tricks you’ve posted. However, I wish everyone would stop trying to support IE 6. Only then will it finally fade away…
puneet
May 11th, 2009
nice article
visit my bloghttp://technofreak-cooldude.blogspot.com
Rebecca
May 14th, 2009
nice article
Kate Mag
May 24th, 2009
Great tricks in one article. Keep up the good job!
Jack McDade
May 25th, 2009
Good list for beginners, all are pretty much *Must Know* techniques if you want to go anywhere. A few things on the post though, tips 14 and 15 got nested in their list, and the HTML markup is missing in Tip 15. Just thought you should know!
Mark Bos
June 18th, 2009
A great article and like many have already said, most is basic stuff. Still even average people would be able to find something useful here (like the min-height thing for me).
e11world
June 18th, 2009
Wow, I haven’t tried any of the 3 IE6 png fixes listed here. I guess I just didn’t bother since the last 15 different fixes/scripts I tried didn’t work in at least one way (always had limitations). I hope these ones don’t have any limitations too and actually work properly.
Dave
June 19th, 2009
I just had to write in , by the way thanks for the Article. I am getting sick of posts like Joe’s ( … sorry, but borring. 99% of this “hints” are standard for every web developer!) , Scott’s and Lowell’s. They are obviously ignorant to how things work in the software industry. I have been in the industry for 15 years and many of these tricks are not known by developers who have been building complex enterprise websites for 10 + years. Most of us really appreciate the time it takes you to help people and write these posts but some people should keep their ignorant mouths shut.
zeeshan
June 24th, 2009
Great work..!!! These are the things which we need to know in our development.
Thanks. ..
Zeeshan