Archive for the Category CSS Tutorials

 
 

Styling Forms with CSS

When you don’t quite get it, styling forms with CSS can be a real pain. But once you get your head around it you will see that there’s nothing really too difficult about it.

In this tutorial you will see how to create this contact form and style it so that it looks like this. In the future I am going to make some further guides on how to take the information entered and send it to yourself using PHP or ASP, but that’s for another day.

Right, so first of all we’re going to need our form. I am going to use this one. Your free to use this code if you don’t have a form of your own. Here’s the code for the form:

<form action=”” method=”post”>
        <label for="user_name">Your Name</label>
        <input type="text" name="user_name" class="inputbox"/>
        <label for="user_email">Your Email</label>
        <input type="text" name="user_email" class="inputbox"/>
        <label for="user_message">Your Message</label>
        <textarea name="user_message"></textarea>
        <input type="submit" value="submit" name="submit" class="submit"/>
</form>
 

Now as you can see the form is very simple and doesn’t look very good at all just yet. We’re going to fix that though.

First of all, we’re going to give the form a css class so that we can style it and all the elements within it. To do this we simply change:

<form action=”” method=”post”>
 

To:

<form action=”” method=”post" class="webform">
 

You will notice I added a class to our form rather than an id. This is simply because you might want to use more than one form on your page and using a class will keep the page valid whether you have one form or ten.

Simple, eh? Now lets start styling.

In our CSS document, we’re going to add a style for our form. So we add:

.webform{}
 

All of our styling will go between the { and the }. Now, I dont want my form to be too wide so I’ll add a width to my CSS.

.webform{
        width:400px;}
 

Next, we’ll add a font, a font colour and a background colour.

.webform{
        width:400px;
        font:11px Arial, Helvetica, sans-serif;
        color:#777;
        background-color:#fff;}
 

Now that the basic styling for our form is done, we can add some styling to the labels and inputs. First of all we will tackle the labels with the following CSS.

.webform label{
        display:block;
        width:300px;
        font-weight:bold;}
 

All the CSS above is pretty simple, we used the “display:block;” so that all the labels will start on a new line. This means we dont have to add any breaks (
tags) after every line. After we added the styling to our labels you can see that the form is starting to look a lot better. Now for the inputs.

.webform .inputbox{
        height:18px;
        width:250px;
        padding:4px 3px 2px 3px;
        margin:2px 0 10px 3px;
        border:1px solid #ccc;}
 

Again, I haven’t used anything to difficult to style the input boxes but they look a lot better! You might have noticed the submit button is a bit screwed up, dont worry, we’ll be fixing that later. But first we will make the textarea look better with the following CSS.

.webform textarea{
        height:80px;
        width:250px;
        padding:4px 3px 2px 3px;
        margin:2px 0 10px 3px;
        border:1px solid #ccc;}
 

So now the form is looking fine and dandy, apart from that pesky submit button. Lets add a class to it.

Change:

<input type="submit" value="submit" name="submit"/>
 

To:

<input type="submit" value="submit" name="submit" class="submit"/>
 

Now our submit button has a class of submit. Now for the CSS.

.submit{       
        margin:2px 0 0 3px;
        background-color:#eee;
        height:30px;
        width:80px;
        padding:0;
        border:1px solid #ccc;
        display:block;
        color:#666;}
 

Again, we have used the “display:block;” style to put the button on a new line without using a break. I have also added a background colour, padding, a border and margin. All of these are pretty simple CSS styles. If you need help with the different CSS styles check out the beginners guide to CSS tutorial.

As you can see, our form is looking very nice now and it has all been done with some simple CSS and minimal extra HTML. You can see the finished CSS form here.

CSS Fonts

One of the most important but overlooked aspects of a web design is the typography. The fonts you use and how you present those fonts can really make or break your website design. Thankfully you can use CSS to really make your fonts look great!

The first thing to learn when it comes to CSS fonts are the selectors and what each of them do. Here are the main font selectors and what they do:

font-family
This selects which font you want to use for your text. You can specify more than one font family, then if the browser cant find the font you use first it will move on to the next one and so on until it finds one it recognises.

font-size
As you might of guessed this defines the size of the font. You can define this as a pixel size (e.g. 12px), as a general size (e.g. large, small etc.).

font-style
The font style attribute defines extra styles to your font such as italic.

font-weight
The font weight sets the weight of the font. To define the weight of your font you can use words (such as normal, bold or bolder) or you can use values (e.g 100, 200, 300 etc.) the higher the values get the bolder the font will get.

These are the main font properties you will use. Now lets have a look at defining a font in CSS. We will add the font to an id called content. Basically this means that everything in the content div will be given the font style we define whilst everything outside the div will have a seperate style.

#content{
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        font-weight: normal;
        font-style: normal;}
 

In the example above we first define the font family, you might notice I slected three fonts all seperated by a comma. This is (as described above) so the browser can find another font to use if it cant find arial. Next we define the font size which is pretty self explanitory. Then we define the font weight and font style. As the default for these two values is normal if you didn’t want to have italic or bold text then you wouldn’t have to define them so your css style would look like:

#content{
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;}
 

Now we will move on to writing the above style in shorthand. Writing in shorthand has the same effects as writing it the long way, it just saves some space.

The way the style should be layed out is:

#content{
        font: font-style font-weight font-size font-family}
 

So an example of a font style would be:

#content{
        font: italic bold 12px Arial, Helvetica, sans-serif;}
 

And that’s all you need to know for the basics of text styling with CSS.

Cheat Sheets

Cheat sheets can be very handy from time to time. Although you probably wont learn much from them, I find them excellent for refreshing my memory. For example if I’ve been working on an ASP project for a while and then have to do some CSS or even worse PHP I often take a while to get my head around the changes in language and format.

I find cheat sheets are great for helping me aclimatise quicker! So here’s a collection of my fevourites:

CSS Cheat Sheets

Now I code something using CSS at least once a day but I still have the need every once in a while to look up some obscure property, so having this CSS cheat sheet lying around comes in handy.

HTML Cheat Sheets

HTML cheat sheets can always come in handy from time to time, bookmark it or print it out so you know where it is when you need it!

jQuery cheat sheets

I’ve been doing a lot of work with jQuery recently and although my javascript isn’t very good this jQuery cheat sheet (and another one from ColorCharge that I cant find on their website anymore) has helped a lot.

ASP Cheat Sheet

PHP Cheat Sheet

MySQL Cheat Sheet

SQL Cheat Sheet

Regular Expressions Cheat Sheet

MOD_REWRITE Cheat Sheet

HTML Characters Chart

Now I realise a lot of these cheat sheets are from the same website, this is just because they’re the best ones I’ve came across. If you know of anoher good cheat sheet that I’ve missed, let me know and I’ll get it up.

I’ll update this post as and when I find new cheat sheets thatI think are worth telling people about.

Horizontal CSS List Menu

This tutorial will show you how to make a horizontal CSS list menu. No images, no javascript. Just a simple, cross browser compatible CSS list menu! If you want to see the finished product or you just want the code, look here.

First of all we’ll need the HTML for our list menu, this is just a normal unordered list.

<div class="menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
<br style="clear: left" />
</div>
 

It’s just a simple html page with an unordered list menu on. You might notice I’ve added a div called menu around the list, I like to do this as it helps me remember what part of the code does what if I ever have to go back to it.

Next we’re going to add the CSS code for the div called menu that surrounds the list.

.menu{
        width: 100%;
        background-color: #333; }
 

Again this is nice and simple. The menu div will be the full width of the div it’s inside and will have a dark grey background.

Next we add the syling for the list and the list items.

.menu ul{
        margin: 0; padding: 0;
        float: left;}

.menu ul li{
        display: inline;}

.menu ul li a{
        float: left; text-decoration: none;
        color: white;
        padding: 10.5px 11px;
        background-color: #333; }

.menu ul li a:visited{
        color: white;}

.menu ul li a:hover, .menu ul li .current{
        color: #fff;
        background-color:#0b75b2;}
 

Again it’s all pretty simple, you should be able to understand the code with a little bit of CSS knowledge. Have a tinker with the code if you’re unsure about something.

You might also want to sign up for the RSS Feed or view some of my other CSS Tutorials

Weird margin problem in IE with floating div’s

Here’s a rather quick css tutorial, but in my opinion, a rather important one. Have you ever noticed when you code a website that uses floating div’s that IE6 does some really weird things with the margin widths?

I’ve came across this before, normally you can get away with a few extra pixels without having to worry, but when you’re coding sites that have large margins on a floating div it can really get messy. Anyway I decided enough was enough, I wasn’t going to leave it, I fiddled around for an hour or two with my CSS file and finally I came up with the reason IE6 doubles the margin on a floating div and that reason is…..(drum roll..)….there is no reason, it shouldn’t do it, it can only be described as a bug!

The Fix

Now to the important part, how to fix the weird margin problem in IE6, and the fix is just as weird as the problem you simply add:

display: inline;

Into the CSS code for the floating element.

Again there’s no reason why this should happen, it’s just one of those things!

I find if you use this technique with the essential css code from my other tutorial I can quickly create layouts that are very, very cross browser compatible.