Posts Tagged ‘CSS3’

The great thing about HTML5 being open source

Monday, September 6th, 2010

I’ve said on here before that one of the things some developers might not like about HTML5 is that others can easily see the code of anything you make. But at the same time, for anyone trying to learn it’s as easy as right-click > view source. I love to take a look at code and figure out what does what and that’s one of the great benefits of technology like HTML5/CSS/JavaScript. It’s all there in the open, so if you see something that interests you, you can very easily take a peak.

I’ve seen some amazing things done with Flash and while I would love to know how they did it, you need a decompiler to take a look at the code and all the assets. Some decompilers will rename library items and variables so it’s a lot harder to figure out what does what. Plus, in some ways, I’ve always felt there was an ethical question when it comes to decompling .swf files.

I love going to a site, like Mr. Doob’s, and being able to see the code so easily. It might be one of the reason’s that I’ve spent so much time lately messing around with HTML5, CSS3 and JavaScript.

I recently reader Hackers: Heroes of the Computer Revolution and one of the main themes throughout the book was the Hacker Ethic, something that said make your hardware or software open source so that others could see what you’ve done and improve it which will make things better for everyone. If HTML5 takes off like it has the potential too, this idea will come back whether developers want it to or not.

Hopefully, developers out there take advantage of this and learn from what others have done. One of the things that gave Flash some bad press was developers making bloated and slow running programs. If a developer has the ability to look at someone else’s organized and efficent code, it might make them a better programmer and users will get better and smoother running programs.

Now if we can just get rid of the phases like “Runs best in Webkit” the future of the web is going to be wide open.

Quick and easy way to make cool buttons using CSS3

Monday, August 16th, 2010

Using some CSS3 and a bit of positioning, you can easily make good looking buttons for your web site. I’ve got two examples you can take a look at here. the first being buttons with a box-shadow.

1
2
3
4
5
6
7
8
9
10
11
.button {
   background:-moz-linear-gradient(-90deg, #273C4F, #205275);
   background:-webkit-gradient(linear, left top, left bottom, from(#273C4F), to(#205275));
   padding:10px 15px;
   -moz-border-radius:5px;
   -webkit-border-radius:5px;
   -moz-box-shadow:3px 3px 5px rgba(0,0,0,0.7);
   -webkit-box-shadow:3px 3px 5px rgba(0,0,0,0.7);
   position:relative;
   text-shadow:1px 1px 1px rgba(0,0,0,0.5);
}

We’ve got a gradient on the background and a box-shadow to give the button some depth. I’ve also added some padding so you can use this on an anchor tag and it will look good no matter what text you have. But what’s with the position:relative? That comes into play on the active state for the button.

1
2
3
4
5
6
.button:active {
   top:1px;
   left:1px;
   -moz-box-shadow:2px 2px 5px rgba(0,0,0,0.7);
   -webkit-box-shadow:2px 2px 5px rgba(0,0,0,0.7);
}

What happens here is that the when the button is clicked, it moves down and left one pixel and the shadow is decrease by one pixel to give it the effect of going down when it’s clicked on.

Now these look like hovering buttons but the same effect can be had on buttons you want to look like they’re going down into the background.

1
2
3
4
5
6
7
8
9
.button2 {
   background:-moz-linear-gradient(-90deg, #1987FF, #0055B2);
   background:-webkit-gradient(linear, left top, left bottom, from(#1987FF), to(#0055B2));
   padding:10px 15px;
   -moz-border-radius:5px;
   -webkit-border-radius:5px;
   position:relative;
   text-shadow:1px 1px 1px rgba(0,0,0,0.5);
}

Pretty much the same thing, but you’ll notice there’s no box-shadow. Now for the active state.

1
2
3
4
5
6
.button2:active {
   top:1px;
   left:1px;
   -moz-box-shadow:inset 2px 2px 5px rgba(0,0,0,0.7);
   -webkit-box-shadow:inset 2px 2px 5px rgba(0,0,0,0.7);
}

Now we add the box-shadow and set it to inset and it looks like the button is being pressed into the background. There’s one unexpected bug I came across with this one, the inset box-shadow doesn’t display right in Chrome on Windows, but it looks great in Firefox and in Safari. Why it doesn’t work in Chrome, I don’t know but hopefully, they’ll fix this bug quick, because I think this is a great looking effect that web designers have a real use for.

There’s HTML5 and then there’s HTML5

Monday, August 16th, 2010

I remember back in school, sometime in my second year, I read an article on A List Apart called A Preview of HTML5 and I was excited about the new tags, like <header> and <footer>. I was pretty sure <video> was going to make life a lot easier. But, really, that was it, it wasn’t a Flash killer or something to build amazing web apps, it was just a new version of HTML that would make my work a little easier. And over the last few years, I’d read articles about it and I was waiting for the day when I could start building sites using it. But then Apple changed everything.

We had a client talk to us about building them a new site and one of their questions was whether or not we knew how to build things using HTML5. It took me a minute, but I realized they didn’t mean HTML5 they way I thought about, they meant it the way Apple had been talking about it, as an all encompassing term that included CSS3 and JavaScript. And over the last six months this has pretty much become the norm, HTML5 means everything that you’d find in something like Apple’s HTML5 showcase or Google’s HTML5Rocks. Maybe it’s just me, but I don’t remember CSS3 transitions being a part of the HTML5 spec.

So it seems that HTML5 has become a term like Ajax, which stands for Asynchronous JavaScript and XML, even though it doesn’t have to include XML. Somebody just thought it sounded cool and started using it to reference a group of technologies that the name didn’t necessarily apply to. In the case of HTML5, it seems like it’s companies like Apple and Google that have spread the use of it as a name for a group of technologies that aren’t really under it’s umbrella.

But it seems that the web design and development community have accept this use and things like HTML5 apps, which use more than just HTML5 to work, are becoming widespread. I don’t have a problem with this but I just find it interesting that HTML5 has become an accepted buzz word so quickly and it’s acceptance is so widespread. And the next time a client brings up the term HTML5, I’ll know what they mean.

CSS3 Transitions on Transforms in Firefox 4

Thursday, July 29th, 2010

In one of my previous posts, I showed how to use SVG as a mask over top of video and I based it on what I saw in a demo video showing off what Firefox 4 can do. With the release of the second Firefox 4 beta, Mozilla has released a few of the demos along with the code. The demo that I think has the most potential for real world use, is the London Project. I can see sites being animated like this really taking off, for better or for worse. And the great thing is, it only takes a few lines of CSS and one line of JavaScript to do it.

Here’s my version of what Paul Rouget made without the video.

How does it work? Like most CSS3 animations, it’s easy once you’ve figured it out. First off, the HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<body class="intro">
<div id="wrapper">
 <div id="intro" onclick="document.body.classList.remove('intro')">
    <h1><a href="#">WELCOME</a></h1>
    <p><a href="#">Click Here</a></p>
 </div>
 <div id="logo">
    <img src="demo_logo.png" />
 </div>
 <div id="picture">
    <img src="robot_slap.jpg" />
 </div>
 <div id="text">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tristique sem id tortor lacinia vel iaculis turpis tincidunt. Sed tincidunt venenatis neque, vel pretium dui suscipit id. Duis sit amet nibh quis ante pretium faucibus nec in quam. In feugiat rhoncus diam, id porta lorem blandit eget. Vivamus et velit sit amet nunc cursus pretium. Mauris in lectus nec tortor lacinia vehicula. Nam sed elit augue. Mauris id magna ipsum. Quisque sagittis aliquam elit at sagittis.</p>
 </div>
</div>
</body>

The only thing here that’s important to the animation is this line: <div id=”intro” onclick=”document.body.classList.remove(‘intro’)”>. This is what triggers the animation when the right box is clicked. It does this by removing the class “intro” that’s assigned to the body tag and then the elements of the page are animated to their new position on the page. Here’s the CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
body {
   margin:0; padding:0;
   background:#dabe25;
 font-family:Arial, sans-serif;
}
#wrapper {
   width:900px;
   height:500px;
   border:10px solid #000;
   margin:50px auto;
   -moz-border-radius:80px 0 80px 0;
 -webkit-border-radius:80px 0 80px 0;
   background:#fff;
   position:relative;
}
#intro {
 width:200px;
 height:100px;
 border:10px solid #000;
 text-align:center;
 position:absolute;
 left:350px;
 top:-200px;
 opacity:0;
}
#intro a {
 text-decoration:none;
 color:#000;
}
#intro h1 {
 margin:10px 0 0 0;
}
#logo {
 position:absolute;
 left:620px;
 top:15px;
 -moz-transition-duration:1s;
 -moz-transition-delay:1s;
}
#picture {
 width:500px;
 height:375px;
 border:10px solid black;
 position:absolute;
 top:60px;
 left:50px;
 -moz-transition-duration:1s;
 -moz-transition-delay:.5s;
}
#text {
 width:250px;
 padding:10px;
 border:10px solid #000;
 position:absolute;
 left:590px;
 top:130px;
 background:#ccc;
 -moz-transition-duration:1s;
 -moz-transition-delay:2s;
}
#text p {
 margin:0;
}
body.intro #intro {
 -moz-transform:translate(0, 380px);
 opacity:1;
}
body.intro #logo {
 -moz-transform:translate(0, -200px);
}
body.intro #picture {
 -moz-transform:translate(0, -800px);
}
body.intro #text {
 -moz-transform:translate(0, -800px);
}

Most of this is styling, which we don’t need to worry about here. We’ll look at just the #logo div because they all behave the same. If you look at the CSS for #logo, you’ll see its final positioning on the page. But there’s also body.intro #logo. Here, we’re using the transform property to animate the positioning of the #logo div. Using translate, we set the initial position to -200px which will take it off the viewable part of the page. And when the #intro div is clicked and the “intro” class is removed from the body tag, the #logo div is animated to its permanent position using the -moz-transition-duration property. All the different elements come in in a staggered effect using the -moz-transition-delay property.

And that’s it. Something that people will use Flash to get the same effect can now be done using just HTML, CSS and the tiniest amount of JavaScript. Right now this only works in Firefox 4, but it’s designed to display properly in all other browsers, just without the animation or, in some cases, the rounded corners.

Poke around in the demo and look at the Mozilla one, which adds SVG masks to the mix. Getting on board with techniques like this as early as possible is going to be the right move, especially since HTML5 is the latest buzzword clients are bringing up when talking about websites. And I’m a fan of adding in extra things like this for people that use newer browsers, as long as it still displays properly in older ones.

jQuery style animated drop down navigation using only CSS3

Tuesday, July 20th, 2010

I’ve been building website navigations that are enhanced with jQuery for a while now and so far I haven’t had many problems with them. But I’m always a little apprehensive about relying on JavaScript for basic functions, like animating drop down menus. It’s not really a big deal, because if you do it right, then it will still display decently if JS is turned off. A great example of how to build a drop down menu and enhance it using jQuery is Jeffery Way’s How to Build and Enhance a 3-Level Navigation Menu.

But we aren’t going to need jQuery for that type of effect much longer, now that Firefox 4 supports CSS3 transitions and, I think, Internet Explorer 9 will too. To get the same kind of effect with CSS3 takes an id or two and a few lines of CSS for each drop down you have. It’s actually a lot simpler with CSS than it is with jQuery!

Here’s a demo of what we’ll be building.

The HTML is pretty much the same as it would be with the usual navigations people have been building:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<nav>
   <ul>
       <a href="#"><li>HOME</li></a>
       <li>
           <ul id="about">
               <a href="#"><li>ABOUT</li></a>
               <a href="#"><li>Me</li></a>
               <a href="#"><li>Education</li></a>
           </ul>
       </li>
       <li>
           <ul id="portfolio">
               <a href="#"><li>PORTFOLIO</li></a>
               <a href="#"><li>Websites</li></a>
               <a href="#"><li>Animation</li></a>
               <a href="#"><li>3D Graphics</li></a>
           </ul>
       </li>
       <a href="#"><li>CONTACT</li></a>
   </ul>
</nav>

The only difference is, for this to work, in the <li>s with a drop down, the top level link is in the child <ul>. It’s nothing major, it’s just something that needs to be done or else the drop downs won’t work.

I’m not going to go through all the styling just the CSS that makes the drop down work.

1
2
3
4
5
6
7
8
9
10
nav li ul {
   position:absolute;
   top:0;
   left:0;
   overflow:hidden;
   height:40px;
   width:120px;
   -webkit-transition:height .2s ease;
   -moz-transition:height .2s ease;
}

The first 3 lines position just make sure that the child <ul> is positioned properly in the parent <li>. The overflow is set to hidden so we can’t see the drop down menu and the height is set to the height of the nav. The transition is set here for when the visitor mouses off of the navigation element.

In my example, I have two drop downs, each with their own height and because of this, they both need their own id. One is “about” and the other “portfolio”, which let’s us set this:

1
2
3
4
5
6
7
8
9
10
#about:hover {
   -webkit-transition:height .4s ease;
   -moz-transition:height .4s ease;
   height:120px;
}
#portfolio:hover {
   -webkit-transition:height .4s ease;
   -moz-transition:height .4s ease;
   height:160px;
}

And here’s what we get.

The transition property is set similar to the one in nav li ul but it’s set to be a little slower because this is where the drop down is actually animated to show itself. And the individual heights for each are set, portfolio has one more item, so it’s 40 pixels bigger than about.

So what are we doing with the transition settings? Like most CSS, this is the short hand for CSS transitions. The first element is the property we are affecting, the second is the transition delay and the third is the timing function which I’ve set to ease to just make the animation a bit smoother. You can find about more here.

And that’s it, that’s all you need to add animation to your drop downs using CSS. It’s a lot less code than the jQuery way and it’s a lot simpler. Plus, I’ve been messing with it all day and it’s impossible to break like it is with jQuery if you don’t have everything set properly. Can you start using this right now? Sure, if you don’t need to have the animation show up in IE or Firefox 3.6 or older. The drop down will still work in IE and older versions of Firefox but there just won’t be any animation, so if you’re just adding it as an extra for Webkit and Firefox 4 users than there shouldn’t be a problem.

This is one of the CSS3 features that web designers should be excited about because it’s going to make it so much easier to add the little things like this that add a lot to a website’s design.

Create the ThinkGeek background effect using CSS3

Wednesday, July 14th, 2010

When ThinkGeek launched their new site design last year, one of the hilights was the background image that switches from robots to zombies when you scroll down to the bottom of the page. It was one of those things that made me, as a web designer, need to know how they did it. How they did it was using a few images and some wrapper divs. It’s not complicated once you figure it out and I think the few extra divs is worth it for the effect. If you want to see how ThinkGeek does it, check out this screencast by Chris Coyier over at CSS-Tricks.

I knew it could be done with CSS3 but was it worth it? The other day I began wondering if you could do it with less HTML and CSS, if you used things like CSS gradients and multiple backgrounds. After some trial and error, I figured out you could do it with a couple of lines of CSS and no extra HTML. Of course, the problem is, it won’t work in any version of IE.

For the CSS, we just need to apply multiple background images to the body tag and this works because CSS3 gradients are technically a background image.

1
2
3
4
5
6
7
8
9
10
11
12
body {
   padding:0;
   margin:0;
   background:url(images/robots2.png) fixed repeat-x -275px bottom, -moz-linear-gradient(#000, #404143 180px, #404143 70%, #000 80%)  no-repeat #404143;
   background:url(images/robots2.png) fixed repeat-x -275px bottom, -webkit-gradient(linear, left top, left bottom, from(#000), color-stop(0.1, #404143), color-stop(0.7, #404143), color-stop(0.8, #000)) no-repeat #404143;
}
#content {
   width:800px;
   min-height:2000px;
   margin:0 auto;
   background:#fff;
}

Check out the demo here.

That’s all the CSS I used to get the same effect. there’s two background properties because there has to be one for Firefox and one for the Webkit browsers, Safari and Chrome. Robots2 is just a image I made that’s the same colors as the ThinkGeek image. Putting it first sets it on top of the gradient background, For the gradients, of course Firefox and Webkit have different syntax but the set up for this is pretty much the same. Black for 180 pixels in Firefox or 10% in Webkit. Then it changes to the gray and stays gray until 70% of the page height and it turns black again at the 80% mark. #content is just a div I put in there because for the effect to work right the page needs to be around 2000 pixels in height, which isn’t that big of a deal.

I think this is great example of what CSS3 is going to let people do. It wasn’t a ton of CSS and HTML to do it with images, but this will load faster because it’s only one image and less code. One thing I noticed while doing this is Chrome doesn’t seem to create gradients as nicely as Safari or Firefox, hopefully Google is working on this because as CSS3 becomes more wide spread, it might make some designs look pretty bad. Another I’ve noticed, if you put a gradient first then multiple backgrounds won’t work. I was fooling around trying to have two gradients and nothing would display at all.

HTML5 is becoming the ultimate PR war

Monday, June 28th, 2010

I actually saw a tweet the other day that said “IE9 is going to be awesome”. Really? I think IE9 is going to be leaps and bounds above IE7 and IE8 but I wouldn’t describe it as awesome. Yes, some of the demos Microsoft as on their IE9 demos are pretty interesting but in a case of some good PR, they’re only showing the stuff that they know their browser will perform as good or better than the others guys with. I will give them credit though, unlike others, they are letting you compare IE9 to the other browsers.

But the problem with these showcases isn’t, in Microsoft’s case they’re just showing things they do well or with Apple’s, forcing you to use Safari. It’s the fact that they’re really just showing what you can do with JavaScript and with CSS3. Sure, the HTML5 canvas tag is what removes the need for something like Flash Player to create most of these things, but is it really an HTML5 demo if you just add a canvas tag and then have hundreds of lines of JS? Isn’t it more of a JavaScript demo?

What’s really going on here is all the different browser makers are saying, “Hey, look what you can do with our browser! Aren’t we awesome? Tell all your clients to use our browser because of the cool stuff you can build in it.” But that’s not going to matter, because when Microsoft shows off IE9 doing something that Chrome and Firefox can’t, then for us in the web community, we just say, well, I can’t build something like that because only IE9 users can see it. Web developers are only going to build what can been seen or used in every browser.

Google is taking the right approach with the HTML5Rocks website, it’s not really a showcase site, it features tutorials and a code playground. One feature I really like is the site shows what browsers the tutorials will work in and I was surprised to see some that worked in all five major browsers.

Simply put, Microsoft’s IE9 Test Drive site is telling the web community that IE isn’t going to suck as bad anymore. And they’re wrapping the message in HTML5. Internet Explorer has been so far behind the other browsers for so long, that I think Microsoft realized their browser was really close to becoming irrelevant and they needed to do something. So they took a page out of Apple’s book and ran with the HTML5 is going to change everything idea. But you have to give Microsoft some props, at least they let you compare their demos in every browser, especially considering they do come up behind some of the others in some tests.

Knight Rider animation made with just CSS3

Wednesday, June 23rd, 2010

Most of the examples people of CSS3 animations people have made have been interesting but pretty useless and this fits that bill perfectly. But the fact is, they’re fun, a challenge and show what CSS3 is capable of. I was bored the other night and was checking out desktop wallpapers at Simple Desktops when I saw one called Hello Michael. For some reason, I got the idea to make the Knight Rider animation using only CSS3. It took a bit to figure out but it worked. It only works in Webkit browsers and seems to work better in Chrome than Safari.

Check it out here

So how does this work? It uses @-webkit-animation and I just had to figure out the timing. The HTML is nothing but 6 divs with ids of block1 through block6 and the CSS for each div looks like this:

1
2
3
4
5
6
7
8
9
#block1 {
  width:100px;
  height:30px;
  background:#f00;
  margin-right:10px;
  float:left;
  -webkit-animation: pulse1 2s linear;
  -webkit-animation-iteration-count: infinite;
}

The only thing really interesting there is -webkit-animation and -webkit-animation-iteration-count. In the first one, I’m telling it the name of the animation, the duration and setting the animation to linear. Next, I’m just telling the browser that the animation is to run forever. You can set it to any number.

1
2
3
4
5
6
@-webkit-keyframes pulse1 {
  0% {opacity:0.5;}
  10% {opacity:1;}
  20% {opacity:0.5;}
  100% {opacity:0.5;}
}

That’s what the animation for block1 looks like. I’m setting the opacity of the div to 0.5 for most of the two seconds of the animation time with it changing to 1 for 10% of it. Block1 and block6 only go to opacity:1; once where as the other 4 have it set to 1 twice. All of the CSS is in the demo page, so if you want to see how I did everything, check out the source.

As I said early, there’s really nothing useful about this, in fact, I really hope we don’t have to see things like this all over web pages. It would be a scary return to the days of animated gifs everywhere. But the challenge was cool and I think it shows that eventually CSS is going to lessen our reliance on JavaScript.

HTML5 lets you wrap an anchor tag around anything

Wednesday, June 16th, 2010

One thing that really annoyed me to no end with XHTML was that I couldn’t wrap a div in an anchor tag and make the whole thing into a link. Well, I could do it, but it wouldn’t validate and I’m not 100% certain that it would work in every browser. But now, with HTML5, you can wrap an anchor tag around anything! This might not sound like that big a deal, but I have to say that being able to create elements, like call to action buttons, using HTML5 and CSS3 instead of images is going to lower page sizes and make life a lot easier for web designers and developers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>HTML5 Anchor Tage Example</title>
<style>
#button {
  width:200px;
  height:100px;
  margin:100px auto 0 auto;
  background:red;
  -moz-border-radius:10px;
  -webkit-border-radius:10px;
  -moz-box-shadow:5px 5px 5px #000;
  -webkit-box-shadow:5px 5px 5px #000;
  background: -moz-linear-gradient(top, #41478b, #7579a5);
  background: -webkit-gradient(linear, left top, left bottom, from(#41478b), to(#7579a5));
}
#button p{
  color:#fff;
  font-family:Arial, sans-serif;
  font-size:40px;
  margin-left:10px;
  padding-top:27px;
}
a {
  text-decoration:none;
}
</style>
</head>
 
<body>
<a href="http://www.atomicrobotdesign.com">
  <div id="button">
    <p>Click This!</p>
  </div>
</a>
</body>
</html>

There is really nothing amazing here, it’s just if this page was made in HTML4 or XHTML, this wouldn’t validate and you’d be left with the choice of making only the text a link or using an image. The only unexpected thing I came across was that in Webkit browsers text is underlined like a regular text link. It’s easy to fix by setting text-decoration to none.

No, this isn’t earth shattering but I think it’s going to make the world of web design a simpler place. You could create the same thing with a paragraph tag, but what if the design calls for more than one line of text or something? This is going to solve some problems I know I’ve had with some of the designs sent my way. Although I’m sure someone’s going to ruin everything and make the entire page a link.

CSS3 calc() is a move in the right direction

Thursday, June 10th, 2010

As far as I’m concerned, any time you can use less JavaScript it’s a good thing. And the CSS3 calc() value is definitely a step in the right direction for CSS. Over at the Mozilla Hacks blog(which every web developer should read), they posted about Firefox 4 supporting the calc() value which will allow developers to size divs without worrying about affecting other divs. They have a great example on their site showing how to prevent an input text field from overlapping it’s parent div:

1
2
3
4
5
6
input {
    padding:2px;
    border:1px solid black;
    display:block;
    width: -moz-calc(100% - 2 * 3px);
}

What does this mean for developers? The first thing I thought of is being able to set the specific width of a wrapper div and then setting the widths of all the child divs using calc() to keep their widths to scale. This would allow me to only need to change the parent div’s width and all the interior divs will scale properly! Now if we can just get variables in CSS.