CSS 3 Features

5 Techniques to Acquaint You With CSS 3

CSS is a well-known, widely used language to style websites. With version three in the works, many time-saving features will be implemented. Although only the most modern browsers currently support these effects, it’s still fun to see what’s around the corner! In this tutorial I’ll show you five techniques.

1: The Basic Markup

Rounded Corners Image

Before we start with this tutorial, let’s create the basic markup that we will be working with throughout the tutorial.

For our xHTML, we’ll need the following div elements:

  • #round, to apply rounded corner CSS3 code on.
  • #indie, to apply an individually rounded corner on.
  • #opacity, to show the new ways CSS3 handles with opacity.
  • #shadow, to show you how to create drop shadows, without Photoshop.
  • #resize, to show you a new sort of CSS, to resize elements.

For this, our xHTML will be:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>An Introduction to CSS3; A Nettuts Tutorial</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
 
 
 
 
resizable image
</body> </html>

In our HTML document, we’ve created some div elements, with the IDs stated above.
Let’s quickly create a base CSS file.

body 	{	
background-color: #fff;
}

#wrapper {	
width: 100%;
height: 100%;
}

div {	
width: 300px;
height: 300px; 
margin: 10px;
float: left;
}
        
/*rest of the code will come here, later*/

As you can see, we’ve given all div tags a width and height of 300px each. We also forced them to float to the left, leaving us with a page full of divs to play with.

2: Rounded Corners

Rounded Corners Image

It can be a hassle to create rounded corners. First, you must create the images. Next, you have to create more elements to have the rounded corners set as backgrounds. You know the drill.

This problem can be easily solved with CSS3, with the property called “border-radius”.
We’ll first create a black div element and give it a black border.
The border is there because we need to "execute" the border-radius property.

We do this like so:

#round {	
background-color: #000;
border: 1px solid #000;
}

Once you’ve created the div, refresh the page. You should now see a black, square div with a width and height of approximately 300px. Now, let’s work on the rounded corners. This can be accomplished with only two lines of code.

#round {	
background-color: #000;
border: 1px solid #000;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;

}

As you can see, we’ve added two nearly identical lines, with the only difference being "-moz" and "-webkit". -moz refers to Mozilla Firefox, and Safari/Google Chrome use the -webkit prefix.

Refresh your HTML document and you’ll see a rounded div – nice and smooth. Well, smooth… In Firefox and Safari, yes, but Chrome renders a slightly pixelated edge.

Back to our code, "border-radius" needs a value. This value determines the sharpness of the corner; the bigger the value, the rounder the corner – just as in Photoshop. To the best of my knowledge, there’s no maximum value for this element.

3: Individually Rounded Corners

Rounded Corners Image

Creating traditional rounded corners can drain your day. Luckily, CSS3 solves it!

This property is quite similar to our previous one. It also uses "border-radius", however we’ll slightly modify our code.

We do it like this:

#indie {	
background-color: #000;
border: 1px solid #000;
-moz-border-radius-topright: 10px;
-moz-border-radius-bottomright: 10px;
-webkit-border-top-left-radius: 10px;
-webkit-border-bottom-left-radius: 10px;
 }

We’ve added "topright" and "bottomright" to our code; this refers to the top and bottom right corner of the div element. With these new properties, you can easily implement a “tab-like” feature.

The additions to be used are "topright", "bottomright", "bottomleft" and "topleft".

4: Changing Opacity, The CSS3 Way

Rounded Corners Image

Currently, you need to write several lines of CSS to adjust the opacity of elements (hacks). Once again, CSS 3 simplifies the process.

This line is easy to remember as well, it’s just "opacity: value;". Easy right?
Our code will be:

#opacity {	
background-color: #000;
opacity: 0.3;
}

5: Drop Shadow!

Rounded Corners Image

In Photoshop, it’s a trivial task to create drop shadows. However when implementing them into your web applications, you’d probably save the shadow as an image, and then use it as a background. No More! (At least for the modern browsers.) Unfortunately, only Safari and Chrome support the feature at this time.

The code requires just one line, but it’s quite long and has 4 different values!

-webkit-box-shadow: 3px 5px 10px #ccc;

For the first value, I’ve used 3px. This one determines the horizontal distance between the drop shadow and the div element. Moving along, the second value, 5px, determines the vertical distance between the shadow and the box.

Yet there is another px value, 10px. This is the radius of the shadow – which in plain English means the blur of the shadow, or how "wide" the gradient is.

Finally, the last value determines the color of the shadow. This means the shadow is not only limited to grayscale; we can also use red (#f00) as a value, which renders a bright, red shadow.

For our final code, it comes down to the following:

#shadow {	
background-color: #fff;
border: 1px solid #000;
-webkit-box-shadow: 3px 5px 10px #ccc;
}

As you can see I’ve given the div a white background, a black border and a light grey shadow.

6: Resize It!

Rounded Corners Image

With the most recent version of CSS, it’s possible to resize elements. (It currently only works in Safari.)

With this code, it’s possible to make a tiny triangle appear in the bottom right corner of an element. You may then drag it to resize. The code to follow is yet again very easy, and just requires one line and is supported by some older elements you might already know. These are "max-width", "max-height", "min-width" and "min-height".

The code is as follows:

#resize {	
background-color: #fff;
border: 1px solid #000;
resize: both;
 overflow: auto;
}

In the code I’ve used 2 additional lines to our normal CSS, "resize: both;" and "overflow: auto;". The line with overflow is needed to make the resize work, it can be any sort of "overflow" value, as long as it’s there.

The other line I’ve used is "resize: both;". This line specifies that the element can be resized in both horizontal and vertical directions.

Conclusion

Rounded Corners Image

What should you take from this article? In the next few years, your job will become easier, webpages will load faster, and you’ll be left with more time for your family! Though you can’t rely on these effects to work in all browsers just yet – there’s absolutely nothing wrong with applying a rounded corner to a div in one browser, and not the other. Consider it an upgrade!

For more information on CSS3, you can visit http://www.css3.info.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Daan Weijers is Daan on Codecanyon
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Youssef

    Very coool… i love this article
    thanks

  • http://amk.apokalipsis.cl Francisco Aguilera G.

    Thanks it’s so cool

  • Pingback: Most Useful Web Design Posts From 2008 | Cardeo Design Blog

  • octagon

    What’s the issue here as far as compatibility is concerned, for now build your clean sites with no extra markup for round corners. Make sure the ui looks awesome for old browsers and then apply any “CSS3″ effects you want to boost the appearance for modern browsers.

    If you really are a designer you should be able to see these bits of CSS3 support as a bonus and not just ignore them because MS neglected the Internet for a god damn decade.

  • http://www.craniumdesigns.com Steve

    This tutorial is kinda silly, as it only pertains to Firefox, Safari, and to some degree, Chrome, so right now, only 20-30% of users.

    Most of these effects don’t work in IE or Opera. I prefer not to use browser-specific CSS, as it is not conforming to web standards. Thought these affects are nice to add some extra “wow” factor to things, I wouldn’t rely on them to be major parts of your website.

    However, nice to see what’s coming around the bend. Until IE supports this stuff, I won’t be using any of these methods, as that’s still 60+% of my audience in most cases.

  • http://www.freshclickmedia.com Shane

    OK. Guys. This is not a silly or worthless tutorial.

    Take a look at this article on Progressive Enhancement.

  • http://james.padolsey.com James

    Steve, these effects are NOT browser specific properties, they are all early adoptions of W3C’s CSS3 draft specification! You might consider opacity a “browser specific” property but I bet you use that!

  • http://www.twitter.com/anthonybruno Anthony James Bruno

    It really is a shame to see so many comments that are simply turning down the use of CSS3 due to it not being supported in all browsers. Progressive Enhancement is your friend, and its time we started thinking about the future.

  • Ryan

    To those saying this post is pointless or that you can’t use this code “yet”.

    Check your analytics.

    For some of us, 70% of our visitors are on mozilla, another 10%-20% on webkit and so we’re completely happy to implement this.

    The goons using IE just get the same ol’ boxes, but for me that’s only 10% of my visitors.

    Speaking of IE: IE8 appears to have left out rounded corners AGAIN. So if you’re waiting for IE to support these specs, you’d better get comfortable.

    Personally, I’m using it. If a client wants IE to have corners than that’s extra–IE style support always costs double for my clients.

  • Pingback: Link Roundup: Week 12/29 - 01/04

  • http://wazdesign.wordpress.com Wazdesign

    Ok . tell . is IE 8 supports multiple background and the CSS3 properties.?

  • http://wazdesign.wordpress.com Wasim

    I want to Use it Anyway …

  • http://www.jehzlau-concepts.com/ Jehzeel Laurente

    very useful tut. I’ll ratimark this :)

  • Pingback: 5 techniques pour se familiariser avec les CSS 3… | Fredzone

  • http://www.abhijitdutta.com Abhijit

    Thanks for the article. Firefox 3.1 has added the box-shadow property through -moz-box-shadow. Its still in beta though.

  • http://resnodesigns.com Bryan P

    Sounds like we cant just rest on our previous knowledge of CSS. How does this code degrade in current and previous browsers? And how long will it take for it to become compliant?

  • Pingback: Chipping the web: January 5th -- Chip’s Quips

  • Pingback: 20090106 網摘 - 部落格經濟 - 網絡暴民 Jacky’s Blog

  • Brent

    IE6 just can’t die fast enough…

    • John

      Hahaha! (^_^) that really is sooooo true!!

  • MrDan

    It’s nice to know it can be used for test and training, but using it for production would be completely pointless.

    I’m tired of writing several different instances of code for the exact same result in several browsers, and using -webkit -moz and suchlikes are just a way to end up adding later the same property for yet another browser (not YaB) until it takes you twice more time when you wanna maintain/update your code.

    I’m not against the evolution of css, it’s just that jumping at it too early seems to me like a long term pain in the neck.

  • Pingback: Nick Sumpter » Blog Archive » CSS3 - Simplifying Standards Based Coding … I Hope!

  • http://www.freshclickmedia.com Shane

    @Bryan. Being involved with software/web development doesn’t afford us the luxury of resting; surely that’s one of the pleasures – that there are always new things to learn and use.

    How does it degrade? Well, put simply, it doesn’t work – specifying rounded corners or shadows, or opacity in this way won’t have an affect.

    How long will it take to become compliant? Not an easy one to answer – my guess is it could be a while. The main thing is that we can use these properties in our designs right now.

    Have a look at this site:

    http://dowebsitesneedtolookexactlythesameineverybrowser.com/

  • http://www.pushingbuttons.net Timothy

    What’s the point of learning CSS3 at this point? There is always one browser (currently IE, formerly Netscape) that holds back progress of web development as a whole. For all we know, IE8 still won’t even support CSS2 completely.

    Why would you spend your time rounding edges and assigning multiple borders when you would be leaving IE6, IE7 and probably IE8 users in the dust (who are the majority of users).

    Don’t get me wrong. I am all for progress. CSS3 has some great value. And I hope it is fully released soon, since the earlier it is released the sooner it will be incorporated as a standard. But that’s just the problem. There is no authority forcing it as a standard. Microsoft and some other vendors lack any serious commitment to these standards.

    We should push for progress and standards, but we should continue to enhance our understanding and expertise of earlier technology until adaptation of new technology is granted.

  • Paul

    Someone just needs to start a grass roots campaign to blacklist IE. This won’t work on enterprise or professional business sites, but Everyone running their own blog or personal sites should include javascript to direct IE users to all of the major modern (and free) browser download pages and refuse to show them content. If people stop browsing in IE at home they will request other browsers in the workplace because they will grow increasingly unfamiliar with the poor product and start to judge it based on it’s merits (or lack thereof) instead of using it because it’s familiar. It’s time that we stop consuming Microsoft’s poor products and cut them loose. They have done nothing valuable for the marketplace in nearly 10 years. If I sold Falafel and my sandwiches tasted like dirt, I would go out of business. Why is this not so for Microsoft? We are to blame.

  • Nick

    nice effect… the dropshadow via css…

    I know you can do it in photoshop… but is there a way to add a dropshadow to a div that is populated dynamically so that the height of the div differs according to the content?

    I guess u need to use ‘repeat’?

  • http://www.freshclickmedia.com Shane

    @Timothy – I couldn’t disagree more.

    If we all adopted the lowest common denominator approach, we’d never move forward. If nobody was using CSS3, there would be less reason for browsers to adopt it, so the fact that it is being used is, in my opinion, a driving force.

    As for IE6, yes, we all wish it would it would die. That’s a given.

    Some well known sites, such as apple and facebook are already dropping full support for IE6. If nobody pushes things forward in this way, we’ll never move forwards.

  • Wayne

    Cool tut — thanks, Daan.

  • http://www.alive77.cn z.Yleo77

    cool I like it .

  • http://rebelmaker.wordpress.com Ian Hutchinson

    It’s nice to see some elements that we would usually have to rely on with Javascript to achieve to be built into CSS.

    Not to be a pessimist, but I’m not keeping my hopes up though for a while though, I doubt the market share of browsers will be large enough for CSS3 to actually implement it in a site.

  • Pingback: Tyler’s Blog » The Future of Web Markup

  • darkkoder

    Good read. May be you could mention in the opacity section that the same effect can be produced in IE with this line of code:

    filter:alpha(opacity=30);

  • Pingback: COFFEE CUP||咖啡杯 » 50最佳CSS文章&资源

  • Pingback: Zaipul.Com » 20 Blogs you can’t live without!!

  • Prabin Karmacharya

    Great Article! Keep it up man

  • Pingback: En iyi CSS Makaleleri | MuGi Graphic System

  • Pingback: [Web] 連結分享 « 網站製作學習誌

  • charitha

    nice. but couldn’t work in IE……

  • Pingback: Los textos de qweos.net» Blog Archive » Ponerse al día con los nuevos estándares web

  • http://www.classiccasualgames.com Classic Casual Games

    Great Article, looking forward to all browsers accepting it…

  • http://www.freshclickmedia.com Shane

    @darkkoder – yep, but that ain’t valid CSS.

  • Pingback: Blox.Svbasi » En İyi 50 CSS Makalesi

  • Pingback: 20 Useful Resources for Learning about CSS3 - Six Revisions

  • http://www.iamjdub.com Jason

    These are going to be great once they are widely accepted in all the browsers. I know I spend a considerable amount of time saving images to pull of these effects now.

  • Pingback: Selección de 20 recursos para el aprendizaje de CSS3 | Cosas sencillas

  • Pingback: آقای وبلاگ » 20 مرجع برای یادگیری CSS3

  • Pingback: persianhelp :: پرشين هلپ » Archive » CSS3 بیست مرجع برای یادگیری

  • Pingback: MyInkTrail: Best of the Web, January 2009 | My Ink Blog

  • Pingback: 20个有用的CSS3学习站点推荐! | 胡言乱语

  • Pingback: 20 Useful Resources for Learning about CSS3

  • Pingback: 20 Useful Resources for Learning about CSS3