28

th

Dec

Top 10 CSS Snippets

Top 10 CSS Snippets

The top 10 most useful css snippets of today.

I have noticed myself to use a few different code snippets on a daily basis so I thought some of you might find them useful. So here we go.

     
  1.    
    center     
    Centering a website (or other elements)
       
    The HTML
      
       
    <!-- end wrap -->

    The CSS

     

      .wrap { width:960px; margin:0 auto;}
     

     

    This is an oldie, but apperantly it is not as obvious as you would think.

     

  2.  


  3.    
    stickyfooter

       
    Sticky Footer (make footer always be on bottom without absolute positioning)

     
    The HTML

     

     

    The CSS

     

      * { margin:0; padding:0; } 
    
      html, body, #wrap { height: 100%; }
    
      body > #wrap {height: auto; min-height: 100%;}
    
      #main { padding-bottom: 150px; }  /* must be same height as the footer */
    
      #footer {
              position: relative;
       margin-top: -150px; /* negative value of footer height */
       height: 150px;
       clear:both;} 
    
      /* CLEAR FIX*/
      .clearfix:after {content: ".";
       display: block;
       height: 0;
       clear: both;
       visibility: hidden;}
      .clearfix {display: inline-block;}
      /* Hides from IE-mac */
      * html .clearfix { height: 1%;}
      .clearfix {display: block;}
      /* End hide from IE-mac */
     

     

    As of recently i have had to use this over 50 times… i think this is one of the most important tricks you will learn today.

     

  4.  


  5.    
    min-height

       
    Cross-Browser Min Height

     
    The CSS

     

      .element { min-height:600px; height:auto !important; height:600px; }
     

     

    You can replace the min-height and heigth with 12em or another value that works for you.

     

  6.  


  7.    
    box-shadow

       
    Box Shadow (CSS3)

     
    The CSS

     

      .box { box-shadow: 5px 5px 5px #666;  -moz-box-shadow: 5px 5px 5px #666;  -webkit-box-shadow: 5px 5px 5px #666; }
     

     

    As you can see since this is a CSS3 property you will need all the three commands to make it cross browser(except for ie of course). The first 2 measurements are for horizontal offset and the vertical offset. The third number is for the blur radius. And the last is the color of the shadow.

     

  8.  


  9.    
    text-shadow

       
    Text Shadow (CSS3) with IE hack

     
    The CSS

     

      .text { text-shadow: 1px 1px 1px #666; filter: Shadow(Color=#666666, Direction=135, Strength=5); }
     

     

    This technique is great for all modern browsers, the IE fix works but it is not amazing quality.

     

  10.  


  11.    
    opac

       
    Cross Browser CSS Opacity

     
    The CSS

     

      .transparent {
        
        /* Modern Browsers */ opacity: 0.7;
      
        /* IE 8 */ -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)";
    
        /* IE 5-7 */ filter: alpha(opacity=70);
    
        /* Netscape */ -moz-opacity: 0.7;
    
        /* Safari 1 */ -khtml-opacity: 0.7;
        
      }
     

     

    Opacity can be used for plenty of elements, it adds a certain new age style we all strive for. Notice that in some browsers transperancy is inherited by all child elements!

     

  12.  


  13.    
    reset

       
    The Famous Meyer Reset

     
    The CSS

     

      html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, font, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td {
     margin: 0;
     padding: 0;
     border: 0;
     outline: 0;
     font-weight: inherit;
     font-style: inherit;
     font-size: 100%;
     font-family: inherit;
     vertical-align: baseline;
    }
    /* remember to define focus styles! */
    :focus {
     outline: 0;
    }
    body {
     line-height: 1;
     color: black;
     background: white;
    }
    ol, ul {
     list-style: none;
    }
    /* tables still need 'cellspacing="0"' in the markup */
    table {
     border-collapse: separate;
     border-spacing: 0;
    }
    caption, th, td {
     text-align: left;
     font-weight: normal;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
     content: "";
    }
    blockquote, q {
     quotes: "" "";
    }
     

     

    This is the most useful css reset out there, i use it for almost everything I work on (even the 52framework, coming soon).

     

  14.  


  15.    
    dotted

       
    Removing the dotted outlines

     
    The CSS

     

      a, a:active { outline: none; }
     

     

    I find myself getting very annoyed with the dotted outline (especially for text-indented elements that are off the page).

     

  16.  


  17.    
    rounded

       
    Rounded Corners (non ie)

     
    The CSS

     

      .element {
       -moz-border-radius: 5px;
       -webkit-border-radius: 5px;
       border-radius: 5px; /* future proofing */
      }
      .element-top-left-corner {
       -moz-border-radius-topleft: 5px;
       -webkit-border-top-left-radius: 5px;
      }
     

     

    Love rounded corners? Don’t want to slave over images and elements to get the effect? This is your solution, and elements still look fine in ie.

     

  18.  


  19.    
    import

       
    Override Inline Styles

     
    The CSS

     

      .override {
       font-size: 14px !important;
      }
     

     

    This comes in handy plenty of times, I personally use it way too much smile everytime something doesn’t work I test if its just not being applied because of some other style.

     

Related Articles

Comments

By Mantish on Tue - Dec 29th, 2009

Good article!
Is it better to use the 2nd snippet over the absolute positioning? why?

By pol on Tue - Dec 29th, 2009

thanks for sharing, but they are “classics”, any more advanced snippets you could share?

By af on Tue - Dec 29th, 2009

1st one: you have to use the text-align fix to use this one on internet explorer..

By Angel Grablev on Tue - Dec 29th, 2009

@Mantish - Absolute positioning should be evaded at all costs, it can cause cross browser issues, also when the content fluctuates you will find yourself having problems

@pol I am sorry that you didn’t find these as advanced techniques, as an educator i try to cover basics rather than techniques people wouldn’t know where and when to use, I am sure I will write a new article one of these days covering more advanced techniques as i encountered them

@af sorry but i have NEVER had problems with the margin:0 auto in any ie version…

By Pikadude No. 1 on Tue - Dec 29th, 2009

I hate it when sites use #8. Makes the browser look unresponsive when I click on a link, and also makes it nearly impossible to navigate using the tab key.

Also, according to https://developer.mozilla.org/en/CSS/opacity , you don’t need that progid thing for #6; “alpha(opacity=xx)” (including quotes) is sufficient.

By Pikadude No. 1 on Tue - Dec 29th, 2009

Speaking of things I hate, I also get annoyed when blogs auto-substitute straight quotes for curly quotes. I can see how it can be nice in some situations, but for an article about code…

By Rizky on Tue - Dec 29th, 2009

for outlines i prefer using overflow:hidden. that way u still have the outlines without it expanding off the page.

By agrublev on Tue - Dec 29th, 2009

@Pikaduded No. 1 I suppose #8 is an opinion that i cant argue with smile don’t use the code. And as for #6 it works smile, and the curly quotes look better :D

By SMiGL on Wed - Dec 30th, 2009

Good collection css tips. Thanks

By Selvam on Wed - Dec 30th, 2009

amazing really cool fix for IE 8 too…many thanks….i’m bookmarking this.. smile

By Blogger Den on Wed - Dec 30th, 2009

You’ve got some great tips here, thanks for sharing! I’ll be back to your blog soon to check up on your future posts

By zur4ik on Thu - Dec 31st, 2009

very usefull snippets. Thanks.

By Diego on Sat - Jan 02nd, 2010

Very useful snippets, thank you.

By Akbar Shah on Sat - Jan 02nd, 2010

Hey… really that’s a cool collection of snippets..

By Andrej on Sun - Jan 03rd, 2010

Just a couple of notes:

#8 - Outline should not be readily removed because it’s an accessibility issue. And if you’re having problems with outlines on elements that you’re using image replacement with - you’re doing it wrong. The image replacement technique should include overflow:hidden; and will no longer present this problem. Better to do that than to eliminate an element of accessibility that is often used…

#10 - Just plain wrong; !important does not override inline styles and should be used sparingly otherwise. Specificity states that inline styles have the highest value. Proper hierarchy should always be observed with selectors and more often than not !important is not even needed.

By Jan on Thu - Jan 07th, 2010

Great snippets - have to try your fixed footer solution soon. Made my own, but it sometimes fails if the page content is too low. Love to see box-shadow in action.

By Lina on Sat - Jan 09th, 2010

Very good tips & roundup Angel.

Especially he cross-browser opacity & rounded corner tip.

Good, elegant code, with good explanations.
Thanks.

By Mia26Ug on Mon - Feb 15th, 2010

I think that students have to ease their troubled minds, because the custom writing services will help to accomplish the comparison essay writing of supreme quality.

By Steroids Buy on Thu - Feb 25th, 2010

Well worth the read. Thanks for sharing this information. I got a chance to know about this.

By Pariuri Sportive Online on Thu - Feb 25th, 2010

Not that I’m totally impressed, but this is a lot more than I expected for when I stumbled upon a link on Stumble Upon telling that the info is quite decent. Thanks.

By Wordpress Gaming Themes on Fri - Feb 26th, 2010

Some great CSS spinets for beginners…Now if only IE could stop being a pain in the rear and conform to CSS3 standards.

By JensenTanya on Sun - Mar 07th, 2010

Different people in the world get the credit loans in various creditors, just because that is simple.

Have something to share?

Remember my personal information

Notify me of follow-up comments?

Please enter the word you see in the image below: