5 useful CSS snippets and tricks

Here are 5 useful CSS tricks that I find myself using often, so I thought of sharing them here. Feel free to suggest others in the comments below.

#1 The quick reset

While there are a lot of lengthy and more comprehensive css resets available online, I’ve found that the following piece of code works just fine and provides pretty good reset capability.

* {margin: 0;outline: 0;padding: 0}
body, div, span, p, a, img, ul, ol, li, table, th, tr, td, form, fieldset, legend, dl, dt, dd, blockquote, applet, object {border: 0}
ul, ol {list-style-type:none}
hr {display: none}

#2 Clearfix

After years of testing and deployment, this method is arguably the best way to clear away floats. Assign the parent container (the container containing floated elements) a class of ‘clearfix’ and you are done.

/* float clearing for IE6 */
* html .clearfix{
  height: 1%;
  overflow: visible;

/* float clearing for IE7 */
*+html .clearfix{
  min-height: 1%;

/* float clearing for everyone else */
  clear: both;
  content: ".";
  display: block;
  height: 0;
  visibility: hidden;
  font-size: 0;

#3 Horizontal navigation bar

The best way to represent a navigation on a webpage is by using lists.
Suppose your navigation list looks something like this:

You can easily turn the above list into a horizontal navigation menu by the following css :

#nav {margin:0;padding:0;height:34px}
#nav li{
font:14px Tahoma,'sans-serif';
#nav a {
padding:10px 14px;

This is just skeletal code, throw in some colors or gradients and you are good to go.

#4 Text-shadow

The text-shadow attribute of css is now being supported by most major browsers. Correct use of text-shadow can really fire up your typography.
For example, for text with a dull silver/greyish background, adding a white text-shadow results in some interesting bevel-like effect. Use it like this:

.someElement {
text-shadow:#fff 1px 1px;

Change the hex value to a color of your choice, usually shades of gray work best.

#5 Hiding text

If you are using images for certain headings or slogans on a site but do not want to compromise with the search engine’s indexing of your text, this trick can come in handy. Here is how:

Suppose your markup is:

Kittens are cute, save them!

and you want to give a background to the h2 that contains the slogan already, then you can just offset the text by a few thousand pixel values, like:

h2#slogan {

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2018 All rights reserved | Powered by WordPress
Back to top
Theme by dynamicguru.com