Thursday, September 10, 2009

CSS Tricks

CSS3? What's that?!

I'm sure you've heard of CSS in general. CSS3 isn't that much different, in terms of syntax; however, the power of CSS3 is much greater. As you'll see in these eleven techniques, you can have multiple backgrounds, dynamically resize those backgrounds, border radiuses, text shadows, and more!

Here's what the official (or at least, what I consider official) website of CSS3, css3.info, has to say about CSS3:

CSS3 is the new kid in the stylesheet family. It offers exciting new possibilities to create an impact with your designs, allows you to use more diverse style sheets for a variety of occasions and lots more.

What We'll Be Covering

Here are the 11 techniques that I'll be showing you how to recreate with CSS3. I'll show you how to create them using CSS2 (or JavaScript), and then with CSS3 properties. Remember - these effects will only work in modern browsers that implement these CSS3 features. Your best option is to view these with Safari 4.

  1. Rounded Corners
  2. Box Shadow
  3. Text Shadow
  4. Fancy Font
  5. Opacity
  6. RGBA
  7. Background Size
  8. Multiple Backgrounds
  9. Columns
  10. Border Image
  11. Animations

1. Rounded Corners

Rounded Corners

Probably my favorite on of this list, rounded corners provide a developer with so many options. You can create a rounded corner button in seconds. My favorite thing to do is set a gradient background to repeat along the x-axis and then apply rounded corners to make a very nice looking Web 2.0 button.

You can simulate rounded corners by using four extra divs or by using JavaScript. Though, a user doesn't see these smooth corners if they have JavaScript disabled, I think that's fine as long as the website still functions as it should. You can read up on this method if you would prefer to use pure CSS in making rounded corners.

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="js/jquery.corners.js"></script>  
  3. <script type="text/javascript">  
  4. $(function(){  
  5.     $('.box').corners('10px');  
  6. });  
  7. </script>  
  8. <div class="box">  
  9.     <!--CONTENT-->  
  10. </div>  

The classical method uses jQuery along with a JavaScript plugin called Corners.

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. border-radius: 10px;  
  4. -moz-border-radius: 10px;  
  5. -webkit-border-radius: 10px;  
  6. }  
  7. </style>  
  8. <div class="box">  
  9.     <!--CONTENT-->  
  10. </div>  

As you can see, all that you need to do is specify three CSS3 properties. Eventually, it will only be one; you have to use three now because different browsers use different property names.

View the demo.

2. Box Shadow

Box Shadow

Box shadows provide you with a very powerful tool. 99% of the time when I'm desigining, I find myself using drop shadows for something. Once again, I'll be using a jQuery plugin to take care of the classic way because honestly, trying to figure it out with just CSS is confusing. Why should I waste my time fiddling with negative margins when there's already something written that does the job for me? As long as my design looks fine when people have JavaScript disabled, I'm perfectly content using a jQuery plugin.

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="js/jquery.dropShadow.js"></script>  
  3. <script type="text/javascript">  
  4. $(function(){  
  5.     $('.box').dropShadow({  
  6.         left: 5,  
  7.         top: 5,  
  8.         opacity: 1.0,  
  9.         color: 'black'  
  10.     });  
  11. });  
  12. </script>  
  13. <div class="box">  
  14.     <!--CONTENT-->  
  15. </div>  

I'm using the dropShadow plugin. Simple and what I wanted; though I would prefer to use just CSS ;).

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. box-shadow: 5px 5px 2px black;  
  4. -moz-box-shadow: 5px 5px 2px black;  
  5. -webkit-box-shadow: 5px 5px 2px black;  
  6. }  
  7. </style>  
  8. <div class="box">  
  9.     <!--CONTENT-->  
  10. </div>  

The values for the box shadow properties are: x-offset y-offset blur color. Much easier than importing two JavaScript files, and A LOT easier than messing around with background images and negative margins.

View the demo.

3. Text Shadow

Text Shadow

If you've ever read a tutorial about how to make the letterpress effect in Photoshop, you'll know that the drop shadow effect is used. It's really simple to create letterpressed text, just follow this tutorial at Line25 by Chris Spooner.

Classic Way

  1. <style type="text/css">  
  2. .font {  
  3. font-size: 20px;  
  4. color: #2178d9;  
  5. }  
  6. .fonts {  
  7. position: relative;  
  8. }  
  9. .fonts .font {  
  10. position: absolute;  
  11. z-index: 200;  
  12. }  
  13. .fonts .second {  
  14. top: 1px;  
  15. color: #000;  
  16. z-index: 100;  
  17. }  
  18. </style>  
  19. <div class="fonts">  
  20.     <span class="font">The quick brown fox jumps over the lazy dog.</span>  
  21.     <span class="font second">The quick brown fox jumps over the lazy dog.</span>  
  22. </div>  

Instead of using a jQuery plugin this time, I just used some simple CSS tricks to absolutely position the text behind the other copy of text. The only bad thing about not using CSS3 for this situation is that you will get two copies of the text if CSS is disabled.

CSS3 Way

  1. <style type="text/css">  
  2. .font {  
  3. font-size: 20px;  
  4. color: #2178d9;  
  5. }  
  6. .font {  
  7. text-shadow: 0 1px 0 #000;  
  8. }  
  9. </style>  
  10. <span class="font">The quick brown fox jumps over the lazy dog.</span>  

If you're planning on using text shadows that are blurred (the third "option" in the text-shadow property), I don't know how you would accomplish that with pure CSS, and no images.

View the demo.

4. Fancy Font

Fancy Font

We've dreamed about it for a long time now, but you can finally display "fancy fonts" on the web without relying on JavaScript. Of course this causes some issues with allowing paid fonts to be distributed over the internet. Anyhow, I present you with @font-face.

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="js/cufon.js"></script>  
  3. <script type="text/javascript" src="js/loyal.js"></script>  
  4. <script type="text/javascript">  
  5. $(function(){  
  6.     Cufon.replace('.classic .font');  
  7. });  
  8. </script>  
  9. <style type="text/css">  
  10. .font {  
  11. font-size: 20px;  
  12. }  
  13. </style>  
  14. <span class="font">The quick brown fox jumps over the lazy dog.</span>  

I decided to use Cufón to replace the text. I'm not going to explain how to use it because Jeffrey has an awesome screencast already.

CSS3 Way

  1. <style type="text/css">  
  2. @font-face {  
  3. font-family: 'Loyal';  
  4. src: url('loyal.ttf');  
  5. }  
  6. .font {  
  7. font-family: Loyal;  
  8. font-size: 20px;  
  9. }  
  10. </style>  
  11. <span class="font">The quick brown fox jumps over the lazy dog.</span>  

We create a font family with @font-face and then use it as a value for font-family. Michael Owens wrote an article here about a month ago which explains @font-face quite well.

View the demo.

5. Opacity

Opacity

If you've visited the Envato website redesign lately, you might have noticed that there are a lot of transparent elements. Though this is achieved with transparent PNGs, you can achieve a similar effect by using the opacity property. Now, the opacity property has been around for a while, but our beloved IE has its own properties.

Classic Way

  1. <style type="text/css">  
  2. .box {  
  3. opacity: .6; // all modern browsers (this is CSS3)  
  4. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; // IE 8  
  5. filter: alpha(opacity=60); // IE 5-7  
  6. }  
  7. </style>  
  8. <div class="box">  
  9.     <!--CONTENT-->  
  10. </div>  

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. opacity: .6;  
  4. }  
  5. </style>  
  6. <div class="box">  
  7.     <!--CONTENT-->  
  8. </div>  

With CSS3, we just eliminated two properties that were specific to IE, isn't that great?!

View the demo.

6. RGBA

RGBA

Everyone knows what RGB stands for (red, green, blue), but what does the A stand for? It stands for alpha, which refers to the transparency.

Other than rounded corners, RGBA is my next most used CSS3 property. Sometimes I just want to add a few light white/black background to navigation links when a user hover overs them. It's much easier than creating a new image for each color; however, with a little PHP it makes things a lot easier.

Classic Way

Seeing as how this isn't a PHP article, I won't be going over the PHP. Just save this file as rgba.php and when you need a certain RGBA color, make the background "color" as url(rgba.php?r=R&g=G&b=B&a=A).

  1. <?php  
  2. $image = imagecreatetruecolor(11);  
  3. $r = (int)$_GET['r'];  
  4. $g = (int)$_GET['g'];  
  5. $b = (int)$_GET['b'];  
  6. $a = (float)$_GET['a'];  
  7. $white = imagecolorallocate($image, 255255255);  
  8. $color = imagecolorallocatealpha($image, $r, $g, $b, 127*(1-$a));  
  9. imagefill($image, 00, $white);  
  10. imagefilledrectangle($image, 0011, $color);  
  11. header('Content-type: image/png');  
  12. imagepng($image);  
  13. ?>  

Now just apply that to a div...

  1. <style type="text/css">  
  2. .box {  
  3. background: url(rgba.php?r=239&g=182&b=29&a=.25);  
  4. }  
  5. </style>  
  6. <div class="box">  
  7.     <!--CONTENT-->  
  8. </div>  

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. background: rgba(23918229, .25);  
  4. }  
  5. </style>  
  6. <div class="box">  
  7.     <!--CONTENT-->  
  8. </div>  

View the demo.

7. Background Size

Background Size

The background size property is an amazing thing to have on your tool belt when you're creating a liquid layout. An example of this could be when you have a background image for a container that is for the sidebar. The classic way of doing this would require tweaking to have the image repeat along the y-axis, but with CSS3 it's like adding another background property.

Classic Way

  1. <style type="text/css">  
  2. .box {  
  3. position: relative;  
  4. overflow: hidden;  
  5. }  
  6.     .box img {  
  7.     position: absolute;  
  8.     top: 0;  
  9.     left: 0;  
  10.     width: 50%;  
  11.     height: 50%;  
  12.     z-index: 100;  
  13.     }  
  14.     .box .content {  
  15.     position: absolute;  
  16.     z-index: 200;  
  17.     }  
  18. </style>  
  19. <div class="box">  
  20.     <div class="content">  
  21.         <!--CONTENT-->  
  22.     </div>  
  23.     <img src="http://nettuts.s3.amazonaws.com/423_screenr/200x200.jpg" alt="" />  
  24. </div>  

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. background: #ccc url(http://nettuts.s3.amazonaws.com/423_screenr/200x200.jpg) no-repeat;  
  4. -webkit-background-size: 50%; /* Safari */  
  5. -o-background-size: 50%; /* Opera */  
  6. -khtml-background-size: 50%; /* Konqueror */  
  7. }  
  8. </style>  
  9. <div class="box">  
  10.     <!--CONTENT-->  
  11. </div>  

Unfortunately, this property isn't implemented into Firefox (V3.5.2) at the time of this writing.

View the demo.

8. Multiple Backgrounds

Multiple Backgrounds

Ah, multiple backgrounds. Now this give developers a very powerful tool. I can think of so many things that require multiple divs just to have more than one background. The most common place I can see this being helpful is in a header section of the website, but that's just the first thing I thought of.

Classic Way

  1. <style type="text/css">  
  2. .box {  
  3. width: 200px;  
  4. height: 150px;  
  5. background: url(images/bg.png) repeat-x;  
  6. }  
  7.     .box2 {  
  8.     width: 100%;  
  9.     height: 100%;  
  10.     background: url(images/text.png) center center no-repeat;  
  11.     }  
  12. </style>  
  13. <div class="box">  
  14.     <div class="box2">  
  15.         <!--CONTENT-->  
  16.     </div>  
  17. </div>  

The classic method is pretty obvious, just wrap the div with another div and so on for each background you need. Produces lovely looking code, doesn't it?

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. width: 200px;  
  4. height: 150px;  
  5. background: url(images/text.png) center center no-repeat, url(images/bg.png) repeat-x;  
  6. }  
  7. </style>  
  8. <div class="box">  
  9.     <!--CONTENT-->  
  10. </div>  

The syntax is really easy to pick up on this one. All you do for the multiple backgrounds is add a comma in between each one! However, once again, this is a limited property and is only in Safari.

View the demo.

9. Columns

Columns

This is the most interesting CSS3 property I've come by. It's not something you see a lot in web design. Myself, I've only seen newspaper-like columns once or twice; however, I love how the effect looks when done correctly. Normally you would separate the content into divs and float those divs, but I found a jQuery plugin which dynamically renders the columns.

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="js/jquery.columnize.js"></script>  
  3. <script type="text/javascript">  
  4. $(function(){  
  5.     $('.columns').columnize({  
  6.         columns: 2  
  7.     });  
  8. });  
  9. </script>  
  10. <style type="text/css">  
  11. .column {  
  12. padding-right: 10px;  
  13. }  
  14. .column.last {  
  15. padding: 0;  
  16. }  
  17. </style>  
  18. <div class="columns">  
  19.     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.</p>  
  20.     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.</p>  
  21. </div>  

I've added a little padding to the columns just so the text isn't smashed up against each other.

CSS3 Way

  1. <style type="text/css">  
  2. .columns {  
  3. -moz-column-count: 2;  
  4. -webkit-column-count: 2;  
  5. }  
  6. </style>  
  7. <div class="columns">  
  8.     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.</p>  
  9.     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.</p>  
  10. </div>  

There are a handful of other CSS3 column properties that you can apply, but for demonstrative purposes, I only specified the number of columns. If you'd like to learn more about these other properties, check out the multi-column page at CSS3.info.

View the demo.

10. Border Image

Border Image

I had no clue about the border image property until I upgraded to Firefox 3.5 and went to Chris Spooner's website and saw that his post images had image borders. I personally don't have any interest in this property, but that's not going to stop me from explaining how to achieve it.

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="js/jquery.borderImage.js"></script>  
  3. <script type="text/javascript">  
  4. $(function(){  
  5.     $('.classic .box').borderImage('url(images/border.png) 27 27 27 27 round round');  
  6. });  
  7. </script>  
  8. <style type="text/css">  
  9. .box {  
  10. border-width: 20px;  
  11. }  
  12. </style>  
  13. <div class="box">  
  14.     <!--CONTENT-->  
  15. </div>  

Instead of spending the time creating multiple divs and repeating the background image around the div, I found a jQuery plugin that does the work for me. It's called borderImage and works just like it should.

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. border-width: 20px;  
  4. -webkit-border-image: url(images/border.png) 27 round;  
  5. -moz-border-image: url(images/border.png) 27 round;  
  6. border-image: url(images/border.png) 27 round;  
  7. }  
  8. </style>  
  9. <div class="box">  
  10.     <!--CONTENT-->  
  11. </div>  

As you can see, the border image property is a bit odd. W3 explains how it gets calculated MUCH better.

View the demo.

11. Animations

Animations

Alright, who doesn't love to see some element gently slide to the left or fade in when you hover over something? Animations are great to increase user interface, but make sure that you don't go over board! The only browsers that support CSS3 animations are Webkit based browsers. The only other way to display animations is to use JavaScript. I'll be using jQuery because it's my favorite JavaScript library (if you haven't guessed by now).

Classic Way

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
  2. <script type="text/javascript">  
  3. $(function(){  
  4.     $('.box').hover(function(){  
  5.         $(this).stop().animate({  
  6.             top: '20px'  
  7.         }, 300);  
  8.     }, function(){  
  9.         $(this).stop().animate({  
  10.             top: '0'  
  11.         }, 300);  
  12.     });  
  13. });  
  14. </script>  
  15. <style type="text/css">  
  16. .box {  
  17. position: relative;  
  18. }  
  19. </style>  
  20. <div class="box">  
  21.     <!--CONTENT-->  
  22. </div>  

CSS3 Way

  1. <style type="text/css">  
  2. .box {  
  3. position: relative;  
  4. -webkit-transition: top 300ms linear;  
  5. }  
  6. .box:hover {  
  7. top: 20px;  
  8. }  
  9. </style>  
  10. <div class="box">  
  11.     <!--CONTENT-->  
  12. </div>  

Both of these code snippets do the same thing: slide the div 20 pixels down over the course of 300 ms. Remember, the CSS3 code only works in Webkit browsers!

View the demo.

Article from http://net.tutsplus.com/

1 comments:

SweetMAANU said...

ആഡ്വളരെ നന്ദി
ഇതുപോലുള്ളവ ഇനിയും പ്രതീക്ഷിക്കുന്നു

Post a Comment