Posts Tagged ‘css’

I made my own HTML/CSS template and you should too

Wednesday, August 11th, 2010

In the last couple of days, a couple of HTML5 templates have been released, HTML5 Reset and HTML5 Boilerplate. I’ve looked them over and I have to say, especially in the case of HTML5 Boilerplate, they’re pretty solid. But they’re not for me. Why? Well, as soon as I looked at the index file, I went through it thinking “I’d take out that and that and that.” It’s the same reaction I had when I looked at the 960 Grid template and a few others. It’s nothing against them or people that use them, it’s just they’re not for me.

Why not? Over time I’ve just made my own. It’s pretty simple and lean and made for the way I build websites. I’m sure I could take someone else’s more comprehensive template and alter it to fit my needs, but I think that would be more work. I’d rather start from zero and build my way up than to edit one down to fit my needs.

This works for me because I’ve been doing this for a while and when it comes to coding HTML/CSS and even JavaScript or ActionScript, I’m fast and I think a template like HTML5 Boilerplate is just going to slow me down. Maybe if I put in the time to sit down and start using one of these on the next few sites I build, I’d get used to it and it might end up working for me. But I know that the way I do it now works and I’m comfortable with it.

I think every web designer/developer should build their own template. If you want to modify one that’s out there, but change it to fit your style. I’m a huge believer that everything should be about making coding comfortable, from the languages you use to the editor you work in and setting up a template is no different.

Be the Batman of web design

Tuesday, August 3rd, 2010

I’m a huge comic fan and as long as I can remember, Superman has been my favorite hero. But there’s a problem with Superman, he’s doesn’t make a very good role model. Why? Because Superman showed up on Earth and was immediately better than you at everything. That’s not really something you can aspire to unless, of course, you are planning on going to a planet where no one is as good as you at web design!

Batman is the one superhero that people can look to for inspiration because he’s one of the few that’s just a man. But in order to become Batman, he worked and worked until he became the best at all the things he needed to master to accomplish what he set out to. It’s been changed a bit over the years, but Batman became an expert at martial arts, an escape artist, a detective and a scientist. And, over the years, it’s become apparent that Batman will always be the last guy standing.

So what does this have to do with web design? Well, from what I’ve learned over the years, no one just shows up and is great at everything that it takes to be a great web designer. There are no Supermen out there, there are only people that have worked their way to becoming Batman. To be a web design Batman, you need to know design, HTML/CSS, JavaScript and technologies like Flash or PHP. And you need to be good at all of them.

Of course, not everyone can be Batman but trying to get there will make you better at what you do. Although, you could be like the Flash and be really, really good at one thing but I don’t think anyone is going to say the Flash is cooler than Batman.

In some browsers <textarea> is resizable

Sunday, July 25th, 2010

I was building a contact form a few weeks ago and since I use Firefox 3.6 to do my testing while I build a site, I didn’t noticed until I checked the site in Chrome that you can now resize text areas. If there’s a couple of diagonal lines in the bottom right corner of the text area, you can click and drag to resize it. Checking around, I’ve found this is only available in Chrome, Safari and Firefox 4 right now, but I’m sure that when Internet Explorer 9 comes out, it will be there too. Is this a big deal? Maybe not, but when you resize the text area if can affect the design around it, moving text and pictures around. And I know from experience, that’s the kind of thing that can become a major issue for some of the clients I’ve worked it, if they happened to discover you can do that.

But this is an easy problem to solve. I usually set the height and width of form elements in my CSS. But there’s a couple of tricky things you should know. If you just set this:

1
2
3
4
textarea {
  width:400px;
  height:300px;
}

It will set the height and width of the text area but it will let the visitor make it bigger although not smaller. You have to set the max-height and max-width as well. If you just set the max-height or max-width, the visitor can resize the text area from the default width to the max-width you’ve set and the same with height. So both need to be set in order to prevent any resizing:

1
2
3
4
5
6
textarea {
  width:400px;
  height:300px;
  max-width:400px;
  max-height:300px;
}

So far, I haven’t been able to find any info on preventing resizing with a setting inside the actual HTML tag, but since CSS is used nearly 100% time, it’s not a big deal to do it this way.

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.

SVG Masks, HTML5 video and Firefox 4

Saturday, July 17th, 2010

Earlier this month, Paul Rouget, who’s a evangelist for Mozilla posted a video where he demonstrated some of the new features that are coming with Firefox 4. And I have to say, the stuff he shows off is for the first time, a real example of how HTML5 and CSS3 can replace things like basic Flash animation. A lot of the stuff he shows off is really amazing and one that stuck out at me was around the 4 minute mark, where the text Firefox acts as a mask for a video. Now, this was definitely something that could only be accomplished in Flash, so I really wanted to know how to do it and unfortunately there was nothing out there to tell me how. After some trial and error though, I managed to figured it out, mainly because, thanks to Firefox 4, it’s not at all that hard.

Here’s what what I made. It only works in Firefox 4, so it’s not something that you can throw up on a site right now, but it is a great example of what we are going to be able to do in the next few years.

Check out the demo here. (This only works in Firefox 4)

How is it done? Using Firefox 4’s inline SVG, HTML5, some CSS and not a lot of any of it. Here’s all the HTML I used:

1
2
3
4
5
6
<video id="video_target" src="oceans-clip.ogg" type="video/ogg" autoplay></video>
<svg>
   <mask id="video_mask" maskUnits="userSpaceOnUse">
       <text x="300" y="190">VIDEO</text>
   </mask>
</svg>

The video is a clip from Disney’s Oceans that I got from Video JS. Since this only works in Firefox 4, I went with the ogg video. In HTML5 the video tag is just like any other tag so you can control it like you would an image tag. What Firefox 4 allows us to do is create some SVG text and then use it as a mask without the need to use an xml doctype like SVG needed before. Another plus is that the actual <svg> tag can be placed anywhere in the HTML and will be assigned to the targeted tag, in our case here the <video> tag. To make this work, we wrap the SVG <text> inside a <mask> tag, giving <mask> the id of video_mask, which we need to assign the mask in the CSS and we need to set the maskUnits to “userSpaceOnUse”. I’ll be honest with you, I don’t really understand this part, but in everything I found this was the setting for text and maskUnits=”objectBoundingBox” maskContentUnits=”objectBoundingBox” is used for shape masks. The video tag has the x and y positions set to center the text.

For the CSS:

1
2
3
4
5
6
7
8
9
10
text {
   font-family:Arial Black, sans-serif;
   font-size:167px;
   text-anchor: middle;
   letter-spacing:1px;
   fill:#fff;
}
#video_target {
   mask: url(#video_mask);
}

First off, we’re styling the text, nothing different here except we need to set the fill to white. If the fill is set to black, nothing shows through the mask and the closer the fill color gets to white, the more that shows through. Next, we set video_target’s mask to url(#video_mask). And that’s it.

And, this is what we get. (Remember, it only works in Firefox 4)

Something that for years you could only do in Flash you can now do with 6 lines of HTML and about 10 lines of CSS. No JavaScript or anything like that to rely on. This is something that should be exciting the web design community, especially if the other major browsers implement this.

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.

Don’t build a site that relies on JavaScript

Monday, July 12th, 2010

I’ve been spending a lot of my time learning jQuery, mainly because the direction the web is going, web designers and front-end developers are going to need to know it to fulfill their clients needs. jQuery is great and can add a lot to a website, including increasing the functionality of a site. But I’m not a big fan of things not working if the visitor has turned JavaScript off. I’ve said here and other places that I feel if they user turns off JavaScript or Flash or Java then they know that they are going to be missing certain elements of a site and I feel, since they’ve made their decision, they should be prepared to live with the consequences. At the same time, I don’t think that a site’s main functionality should rely on something like JavaScript in order to work. A site that needs JavaScript to provide it’s main function, such as e-commerce, is built wrong.

99% of people have JS turned on, so what’s the big deal? I was taught and I still follow it to this day, your site should work if the CSS is turned off. A visitor might come to your site and the CSS file doesn’t load, so instead of refreshing, they assume the site looks this way. You want them to be able to still get around and be able to do what they wanted to do on your site. And I believe this has to apply to JavaScript as well. Some browsers suck at running JavaScript and we’ve been on sites where it takes a few minutes to load because it’s hanging up on the JS. How many times has a site half loaded on you because it was waiting for the JS, so you just clicked the back button?

So what do you do? Build your main functionality with HTML and a language like PHP. Just like CSS is for styling and positioning, JavaScipt and libraries like jQuery should be used to enhance a web site. One thing I’ve learned over the years is that a lot of these decisions come down to who is the sites audience. Your audience will almost always determine the technology you use and if you think you can get away with using JS to provide an import functionality, then do it. But always be aware that there might be someone out there that can’t use the site the way you intended.

2 CSS Positioning Tips Every Web Designer Needs to Know

Thursday, July 1st, 2010

In my fight to stop people from using tables for layout in web design, there’s a couple of arguments I come across a fair amount. One is about pinning a div, like a footer div, to the bottom of another div and it stays there regardless of the parent div’s height. And the other one is positioning a div inside a parent div that’s been center on the page. Both of these things are easy to do when you know how to do them and for some reason the solution isn’t as wide spread as you would think it is. In fact, the way you position div inside another div is something that I only found out about a couple months ago. In fact I had a work around that worked pretty good, but used margins, which isn’t always the best way to go.

Let’s say you have this HTML:

1
2
3
<div id="wrapper">
    <div id="right_block"></div>
</div>

And you wanted to position “right_block” over to the right side of the “wrapper” div. Here’s how I used to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#wrapper {
  width:800px;
  height:500px;
  background:#ccc;
  margin:auto;
}
#right_block {
  width:100px;
  height:200px;
  background:#222;
  position:absolute;
  margin-left:600px;
  top:50px;
}

If you set left to 600px then “right_block” will stay 600px from the left edge of the browser and 50px from the top no matter the width and height of the browser window. If you change left to margin-left, then “right_block” will be 600px from the left edge of the “wrapper” div and 50px from the top of it. If this works, then why not do it this way? Well, sometimes the margin can interfere with elements above it and this really isn’t the proper way to do it. But the solution is really simple. Just add position:relative; to “wrapper” in the CSS and then you can just position “right_block” the usual way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#wrapper {
  width:800px;
  height:500px;
  background:#ccc;
  margin:auto;
  position:relative;
}
#right_block {
  width:100px;
  height:200px;
  background:#222;
  position:absolute;
  left:600px;
  top:50px;
}

That’s all there is to it. This is one of those things that really needs to be taught to web designers just starting out. And setting the wrapper div’s position to relative is also part of the solution to pinning the footer div to the bottom. Here’s our HTML:

1
2
3
<div id="wrapper">
  <div id="footer"></div>
</div>

Now we want to pin “footer” to the bottom of “wrapper” and maybe the page height varies from page to page on our site and we don’t want to have to set it’s position on every page. It’s really easy to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#wrapper {
  width:800px;
  height:500px;
  background:#ccc;
  margin:auto;
  position:relative;
}
#footer {
  width:800px;
  height:60px;
  background:#555;
  position:absolute;
  bottom:0;
}

“Footer” will be pinned to the bottom because we’ve set the “wrapper” div’s position to relative and “footer” to absolute. But what positions it at the bottom is bottom:0; That’s all it takes. Now the “footer” div will be at the bottom no matter what the parent div’s height is.

CSS positioning can be frustrating when you’re just starting out. But once you know tricks like this, it makes the temptation to use tables for positioning a lot less. With CSS3 looking like it’s going to be supported in Internet Explorer 9, tables for layout is going to limit the features you can use in your design. And when it’s this easy to solve some of the problems people seem to have with CSS positioning, there’s really no reason to ever use tables to layout your website.

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.

The only person that cares how you did it is you

Tuesday, June 22nd, 2010

I work with five designers and a couple have basic HTML knowledge. One can kinda sorta do key framing in Flash. Everything web is my responsibility. And I have a thing where I like to challenge myself and do something new on nearly every project I work on. Sometimes I do something that I just learned and sometimes it’s something I’ve been working a while on and I’m excited to finally have figured it out. After a while, seeing all the glazed over eyes as I tried to explain the amazing thing I did, I realized that A. most people don’t understand what web designers and developers really do and B. they don’t care. I’m a person that’s loves learning how they did things in movies and when I see a website that does something I’ve never seen before or does it a new way, I enjoy picking it apart, figuring out what does what. And it’s taken me a while to realize that this isn’t how most people think.

When the average Internet user comes to the site, they don’t care about anything that’s going on behind the scenes. Only a very small group of us do and I don’t even think that includes all of the people out there that call themselves web designers. There are people out there building websites the same way they did 15 years ago. I’ve started building every new site in HTML5 and using CSS3 as much as I can instead of using images. Why do I do this? I can get the same result building a site the way I did 3 years ago and my bosses wouldn’t be any less happy with it. But I have a need to deliver a website that is built with the latest technology. I have to challenge myself or else the job will get boring. I don’t build sites in HTML5 for the clients, 90% of them haven’t heard of HTML, much less HTML5. I did it because I want the people that know to look under the hood of my sites and know it was built by someone who knows what their doing. And I did this stuff for fun before I was being paid for it and I want to keep it fun.

If you’re the only one that’s going to know the amazing things you did to make your site, why bother going to that extra effort? For me, as I’ve said before it’s the challenge but I think there’s also some pride in there. I want to build the best site I can. I want to impress those web design gurus out there that will probably never visit those sites. Most nights at home, I’m learning, messing around and building things that have no practical function, I’m building them to see if I can. I think the CSS Fail Whale is a good example of something that was built just to see if it could be built by someone that enjoys they’re work. I’ve spent time looking at the CSS and I’m sure people like me have, but does this interest any non-web people? I’m pretty sure they’d ask, “Why didn’t you just use a picture?”

So everyday I go to work and try to build something new, using things like HTML5 and CSS3. Because I doing something I love and I want to be great at what I do. I’m just lucky enough that I get paid to do it and now I’ve finally learned it doesn’t matter to me if anyone else gets what I’m doing.