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.

Demo 1

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.

Demo 2

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.

Demo 3

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;
}

Demo 6

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.

Demo 7

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*/
}

Demo 9

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.

Demo 10

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.

Demo 12

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*/
}

Demo 13

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
  • 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:

    • More on the Star HTML Bug
    • Explanation of the star HTML bug
    • 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*/
      }

      Demo 15

      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?


Comments

Leave a Comment
  1. Very informative. Thanks!

    Bookmarked.

  2. this is a very informative compilation. every coders need this!

  3. 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

  4. 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.

  5. @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

  6. Tips 14 & 15 seem to be indented to the left. *Using Opera 9.62.*

  7. Very good informative compilation, thank you for this CSS tricks !

  8. Wow! Lots of useful info, definitely bookmarking this for reference later

  9. 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.

  10. thanks
    helpful tipps and tricks

  11. RE: #8
    “Google’s IE 7.js” is more accurately “Dean Edwards’ IE7.js hosted by Google Code”

  12. Gravatar

    Rajiv Vishwa

    Neat and Simple!

  13. Really helpful and bookmarked.

    Cheers

  14. … sorry, but borring. 99% of this “hints” are standard for every web developer!

  15. Nice work.

  16. nothing new here

    also “7. Using CSS to Fight Spam” has major accessibility drawbacks, screen-readers would not be able to read the captcha

  17. #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

  18. “One of the greatest tricks come together” article :)

    Thank you very much

  19. 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! :)

  20. Great post. Already use a bunch of these but still a very nice refresher

  21. And here I thought I was a geek. Well done!

  22. 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.

  23. Interesting ;)

  24. Very good tutorial. The points about margins and padding were really informative.

  25. 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.

  26. Thanks for this list. Very useful.

  27. One of the better css trick list in a long time. Thanks!

  28. Really very useful one for all… Keep this…

  29. 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

  30. eh.. old, basic stuff

    fyi - the layout for your site is broken in mac os x/firefox 3.. the content is below the navigation.

  31. This is ’standard’, but forms a great list of questions to ask at an interview, imho.

  32. Thanks !

  33. 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

  34. Great List! very useful thanks.

  35. 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. ;)

  36. Very nice for us nonhardcore css developers

  37. very useful, thanks a lot…

  38. Great Article.! just learned some great css techniques !

  39. Great tips. I’ve learned a lot of these in just the past few months and they’ve really helped cut my coding time.

  40. I love how you present everything, very clean and understandable. Keep at it!

  41. too good article

  42. very good article !!!

    especially,
    “10. Use CSS Image Sprites” is great informative :)

  43. Damn good article. Thanks!

  44. good work - thanks

  45. 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.

  46. Thanks very much for all the positive encouragement guys, its what keep me going :)

  47. Nice article. Bookmarked. :)

  48. First paragraph says “20″ yet title says 15 hints. Just a typo I guess. Figured i’d let you know.

  49. Anyone notice the outline onpress when using text-indent for block anchors. Try this next time -moz-outline: none;

  50. @Alex, yup just a typo :) Thanks for letting me know, I’ll see if Jeff can change it.

  51. cooool :)

  52. 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

  53. Nice post with a good collection of items that come up frequently.

  54. Gravatar

    Eberhard Doerr

    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!

  55. Awesome, thanks guys!
    Always hard to keep up with all the new CSS stuff and stylings. Your page is bookmarked!

    Jim

  56. @ #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).

  57. 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.

  58. 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!

  59. Gravatar

    David Hucklesby

    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.

  60. 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.

  61. 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.

  62. Sometimes some jquery codes could b better for a plain html code.

  63. Nice.
    Isn’t there a problem with IE6 and auto margins?

  64. just Awesome ,
    Thanks

  65. Thanks for the tips!

  66. 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.

  67. Very well put up! This will sure help every css starter…

  68. Gravatar

    Saurabh Shah

    very useful tips … thanks for sharing

  69. very good, but known list.

  70. Sprites are always good. And thanks for refreshing me on the absolute / relative positioning.

  71. Great
    Thank you

  72. thank you

  73. There is another solution to use transparent PNG in IE 6:
    http://www.sitepoint.com/blogs/2007/09/18/png8-the-clear-winner/

  74. thank you and bookmark!

  75. thanks…………………….

  76. very good, thanks

  77. that is hell of a good work here! Very useful for beginner like me. And to ALEX for mentiong the List Apart png technique :-)

  78. of course Site Point… oops (well that is an obvious tip to go to sleep ;)

  79. a good web design resource

  80. Oh… very good post! I translate this on my language and pos in my blog ;)
    THX!

  81. 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…”

  82. Wow. Nice put together. You should also write some # tricks to fight IE ;)

  83. Nice summary and some great tips - bookmarked for reference!

    Cheers!

  84. Great tricks.
    I will use some in the website i am making.

    Wish to see more css tutorials.

  85. very helpful tips…nice compilation

  86. very useful tricks…thanks!

  87. nice set of posts. very handy to look back upon..

  88. 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…

  89. nice article

    visit my bloghttp://technofreak-cooldude.blogspot.com

  90. nice article :-)

  91. Great tricks in one article. Keep up the good job!

  92. 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!

  93. 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).

  94. 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.

  95. 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.

  96. Great work..!!! These are the things which we need to know in our development.

    Thanks. ..

    Zeeshan

Add a Comment

Name Email Website

Note: The avatars shown next to comments are Gravatars. You can get a Gravatar account for free and any other site that supports it will show your avatar too!

 

Trackbacks

Leave a Trackback