New CSS techniques with CSS3

AID: 4168
  • Status: Published

2537 points

  • ByZado
  • TypeTips/Tricks
  • Posted on2010-11-29 at 15:17:12
Awards
  • Community Pick
What is CSS3? It's the same CSS you've been using, but updated and improved. Now you can replace image effects or even some javascript code with simple CSS code.

So let's get started – no needless writing or boring descriptions, just clear, simple and easy to understand examples below. 12 of my favorite CSS3 tricks:



1.Rounded corners
Use it in your div, add background color, set your border and done.
<style type="text/css">  
.box {  
  border-radius: 10px;  
  -moz-border-radius: 10px;  
  -webkit-border-radius: 10px;
  background-color: #ccc;  
}  
</style>  

<div class="box">  
  <!--your content-->  
</div>  
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/rounded-border/
http://css-tricks.com/snippets/css/rounded-corners/




2.Box shadow:
Add a drop shadow to your div or image.
<style type="text/css">  
.box {  
  box-shadow: 5px 5px 2px black;  
  -moz-box-shadow: 5px 5px 2px black;  
  -webkit-box-shadow: 5px 5px 2px black;  
}  
</style>  

<div class="box">  
  <!--your content-->  
</div>  
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/box-shadow/
http://css-tricks.com/snippets/css/css-box-shadow/




3.Text shadow:
Add a shadow to your text. Nice when used for headlines on your website.
<style type="text/css">  
.font {  
  font-size: 20px;  
  color: #2178d9;  
}  
.font {  
  text-shadow: 0 1px 0 #000;  
}  
</style>  
  
<span class="font">
  Your fancy text here
</span>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/text-shadow/
http://css-tricks.com/snippets/css/css-text-shadow/




4.Cool font:
Instead of using javascript and cufon (http://cufon.shoqolate.com/generate/), use this simple CSS technique to include your font on your website.
<style type="text/css">  
@font-face {  
  font-family: 'Loyal';  
  src: url('loyal.ttf');  
}  
.font {  
  font-family: Loyal;  
  font-size: 20px;  
}  
</style>  
 
<span class="font">
  Text with your fancy font!
</span>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/web-fonts-with-font-face/
http://css-tricks.com/snippets/css/using-font-face/




5.Gradient:
Use a gradient in your div or body tag. Requires an image to use as the gradient.
<style type="text/css">  
#gradient {
  height: 100px;
  background-color: #1a82f7;
  background: url(images/linear_bg_1.png);
  background: -moz-linear-gradient(100% 100% 180deg, #2F2727, #1a82f7);
  background: -webkit-gradient(linear, left top, right top, from(#1a82f7), to(#2F2727));
}
</style> 

<div id="gradient">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window


Learn more:
http://css-tricks.com/css3-gradients/




6.Opacity:
Simple trick to make your page elements transparent.
<style type="text/css">  
.box {  
  opacity: .6;  
}  
</style>  
   
<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/opacity/
http://css-tricks.com/snippets/css/cross-browser-opacity/




7.RGBA:
Red, green, blue, alpha (transparency). It's fun to experiment with this technique.
<style type="text/css">  
.box {  
  background: rgba(239, 182, 29, .25);  
}  
</style>  
  
<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/rgba/




8.Background size:
Resize your background with pure CSS. Opera 9.5, Safari 3 and Konqueror only.
<style type="text/css">  
.box {  
  background: #ccc url(images/xxx.jpg) no-repeat;  
  -webkit-background-size: 50%; /* Safari */  
  -o-background-size: 50%; /* Opera */  
  -khtml-background-size: 50%; /* Konqueror */  
}  
</style>  
  
<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/background-size/




9.Multiple backgrounds:
Use more than one background in a very easy to implement way. Safari 3 only.
<style type="text/css">  
.box {  
  width: 200px;  
  height: 150px;  
  background: url(images/text.png) center center no-repeat, url(images/bg.png) repeat-x;  
}  
</style>  
   
<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/multiple-backgrounds/
http://css-tricks.com/snippets/css/multiple-backgrounds-syntax/




10.Text columns:
Instead making tables or using javascript, you can now define columns in your stylesheet.
<style type="text/css">  
.columns {  
  -moz-column-count: 2;  
  -webkit-column-count: 2;  
}  
</style>  

<div class="columns">  
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>  
  <p>Lorem ipsum dolor sit amet.</p>  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/multi-column-layout/
http://css-tricks.com/snippets/css/multiple-columns/




11.Image border:
Don't use boring 'border' property, make it fun with borders from an image.
<style type="text/css">  
.box {  
  border-width: 20px;  
  -webkit-border-image: url(images/border.png) 27 round;  
  -moz-border-image: url(images/border.png) 27 round;  
  border-image: url(images/border.png) 27 round;  
}  
</style>  
 
<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window


Learn more:
http://www.css3.info/preview/border-image/




12.Animations:
Believe it or not, you can animate elements with simple and short css code!
<style type="text/css">  
.box {  
  position: relative;  
  -webkit-transition: top 300ms linear;  
}  
.box:hover {  
  top: 20px;  
}  
</style>  

<div class="box">  
  <!--your content-->  
</div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window


Learn more:
http://css-tricks.com/snippets/css/webkit-keyframe-animation-syntax/




Please note, these techniques are new. They can only be used in browsers that support CSS3 and will not work in older browsers.

Search the net for even more sophisticated techniques, tips and tricks for CSS3. This article just scratched the surface of CSS3 possibilities. It's a great tool for every day use if you are a web designer. And it's just plain fun to play with and experiment! I've seen some fantastic websites on the internet made with pure html/css3 (and images obviously). CSS3 is a huge step forward in web design.

Thanks for your time reading this and good luck.
Hope you find it useful :-)


LEARN CSS3:
http://www.css3.info/
http://designshack.co.uk/articles/introduction-to-css3-part-1-what-is-it
DESIGN WITH CSS3:
http://www.themeflash.com/60-excellent-css3-tutorials-and-techniques-you-should-know/
http://www.dzinepress.com/2010/03/50-excellent-tutorials-for-web-development-using-css3/
Asked On
2010-11-29 at 15:17:12ID4168
Tags

css

,

css3

,

stylesheet

,

css tricks

,

css tips

,

cascading style sheet

Topic

Cascading Style Sheets (CSS)

Views
1249

Comments

Expert Comment

by: mreuring on 2011-04-07 at 08:24:29ID: 25594

Where's the piece on transitions? Much better support than animation :)

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top CSS Experts

  1. COBOLdinosaur

    213,892

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  2. LZ1

    169,513

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  3. DaveBaldwin

    108,635

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  4. ChrisStanyon

    101,266

    Master

    0 points yesterday

    Profile
    Rank: Sage
  5. tommyBoy

    65,616

    Master

    0 points yesterday

    Profile
    Rank: Genius
  6. kozaiwaniec

    57,116

    Master

    2,000 points yesterday

    Profile
    Rank: Guru
  7. nap0leon

    53,757

    Master

    0 points yesterday

    Profile
    Rank: Sage
  8. xmediaman

    50,100

    Master

    0 points yesterday

    Profile
    Rank: Guru
  9. jason1178

    49,680

    0 points yesterday

    Profile
    Rank: Genius
  10. SSupreme

    43,018

    0 points yesterday

    Profile
    Rank: Wizard
  11. Kravimir

    42,150

    0 points yesterday

    Profile
    Rank: Genius
  12. s8web

    40,064

    0 points yesterday

    Profile
    Rank: Sage
  13. therealteune

    35,300

    0 points yesterday

    Profile
    Rank: Wizard
  14. webmatrixpune

    30,818

    0 points yesterday

    Profile
    Rank: Guru
  15. HagayMandel

    27,880

    0 points yesterday

    Profile
    Rank: Guru
  16. zappafan2k2

    23,368

    0 points yesterday

    Profile
    Rank: Guru
  17. leakim971

    22,966

    0 points yesterday

    Profile
    Rank: Genius
  18. HainKurt

    20,000

    0 points yesterday

    Profile
    Rank: Genius
  19. jagadishdulal

    19,668

    0 points yesterday

    Profile
    Rank: Guru
  20. Proculopsis

    19,350

    0 points yesterday

    Profile
    Rank: Sage
  21. mplungjan

    19,232

    0 points yesterday

    Profile
    Rank: Savant
  22. basicinstinct

    19,026

    0 points yesterday

    Profile
    Rank: Genius
  23. anuradhay

    19,006

    0 points yesterday

    Profile
    Rank: Guru
  24. jtwcs

    18,808

    0 points yesterday

    Profile
    Rank: Master
  25. gurvinder372

    18,057

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame