7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. HTML & CSS

How to Design and Code a Flexible Website

Scroll to top
Read Time: 32 mins

In this tutorial, we're going to be designing and coding a simple blog-style web-page. We'll pay special attention to making our design flexible and accessible by using clean and simple XHTML and CSS. This tutorial is aimed at beginners, and anyone looking to improve the accessibility of their web designs.

Tutorial Details

  • Difficulty: Easy
  • Estimated Completion Time: 1.5-2hrs

Step 1

So our example web page is going to be based on a simple blog theme, with a WordPress blog-like structure similar to that of the nettuts homepage. It's going to look something like this:

The idea here is not for you to reproduce my example, but for you to be able to follow along and apply the techniques to your own designs, hopefully learning a thing or two about the underlying concepts.

Step 2 - Photoshop

We're going to keep the Photoshop use here to a bare minimum, Usually I mockup an entire design in Photoshop before coding, but here I'm just going to generate a basic layout, and worry about the content later. This is an example of a different workflow, it will make more sense as we go along.

Note: Some readers have asked about using the GIMP. I haven't used it personally, but I'm sure you can accomplish the following steps in GIMP just as easily, because all we're using is basic shapes and gradients.

Page Layout

I decided to make the page 900px wide, so my document is 1000px wide and 1200px long (don't know why I gave myself so little room, make yours wider if you like). Place guides at 50px and 950px to get a 900px wide area. We're going to have a content area and a sidebar, and the content area is going to be 600px wide, so place another guide at 650px.


The header background is just three rectangles for the top links, main header, and navigation area. I used shape layers and added gradients to the layer styles. There are also 1px borders between the top bar and header area, and between the header and navigation area.

The footer background is another gradient, but this time with a 2px border at the top.


Next add a background for the sidebar, I chose #d8ddd1.


Type Tool

Next we Grab the type tool (T) and add in a logo and tagline, and some basic navigation links. Fonts:

Blog Title:

  • Font: Myriad Pro
  • Size: 48pt
  • Color: #ffffff (white)

Blog Description:

  • Font: Myriad Pro
  • Size: 24pt
  • Color: #ffffff

Main Navigation Links:

  • Font: Arial
  • Size: 18pt
  • Color: #2b2b2b

"welcome, guest" and "stay updated":

  • Font: Arial
  • Size: 12pt
  • Color: #fffff

"login, Sign Up" and "subscribe via...":

  • Color: #a5bf8d
  • Style: underline


We're only going to include one sample "post" in our Photoshop mockup, because I find working with type in Photoshop is a real pain, but we'll get into more detail about styling the content section later. The fonts I'm using for the dummy post are:

Post Title:

  • Font: Arial
  • Size: 24pt
  • Color: #3c3f40
  • Style: Bold

Date, category and author info:

  • Size: 11pt


  • Size: 12pt

Okay, we're pretty much done with our mockup. All we need to do now is slice it up and save for the web.

Select the slice tool (C) and cut out skinny slices of each of the background rectangles: the top bar, the header area, the navigation, and the footer. Don't include the borders, we're going to add those in with CSS. Try zooming in really close to make sure you get the right parts. The slices I cut are about 5px wide, but the width isn't terribly important at this point.


Select 'File/Save for web and devices...' Hold down "shift" and click to select each slice. From the dropdown menu "presets" select "jpeg . Uncheck "convert to sRGB"" (I find that the conversion dulls the colors). All other settings should be left at defaults. Click "Save." In the popup window, make sure "selected slices" is displayed in the "slices" dropdown menu, and click save.

GIMP users: I'm not sure if gimp has a tool like slice, but you just need to make rectangular selections, save them to separate documents, crop them down, and save them as optimized jpegs.

For a more in depth look at the slicing and saving process, see My previous tutorial.

Step 3 - HTML Setup

If you're unfamiliar with the process of organizing files and folders for a webpage, again, see my other tutorial linked above.

Open up your favorite code editor, and create a new file called index.html.
We're going to try to use as few div tags as possible to keep our markup meaningful and semantic. That being said, to maintain a flexible, resizable layout, we need to enclose some elements in multiple divs. More on that later.

All elements in our page will be contained within two divs, called "main" and "footer". Within the "main" div, we're going to have a divs for the top bar, the header, and the content area. The footer is going to contain an inner div for the written content.

Top Bar

The background of the blue bar at the top stretches the entire width of the page, but the two text areas need to be within the 900px of the page. To achieve this, we need the content to be contained within another div, which will have a class of "container".
Within our bar at the top, we're going to have two paragraphs, one for the login, one for the subscription options. Since they're going to be floated, each needs to be given a unique id. Actually, if we wanted to be totally semantic, we could code these two paragraphs as an unordered list with two list items. Try it both ways, see if you can make it work.

Step 5 - Header

We face the same issue here as we did with the last step, the background image needs to stretch out indefinitely. To contain the content, we need to place it within another div. Since the header will also be within our centered, 900px wide page, we can re-use the "container" class. The title of the blog will be wrapped in an <h1> tag, and the description/tagline will be a paragraph with a unique id.


Also inside the header is the navigation menu, which will be wrapped in an unordered list with the id of "menu", which will all be wrapped within another div with the id of "navigation".
Because we want the navigation menu to be centered, we can add our "container" class to the ul as well.

We're writing the navigation links in lowercase here, but in our CSS file we will transform them to uppercase. You could write them in capitals here, but my caps lock key is broken, and this way makes for cleaner markup.

Step 6 - Content

The content area doesn't have a background image or color, so we don't need to enclose it in an additional div. To center it, we can give the content div a class of "container" as well. Inside the content div, we have two more divs, one for the blog posts, and one for the sidebar.

When designing a blog, we need to take into account the fact that the content is going to change, and may include any number of elements, including lists, images, quotes, headings and emphasized text. To prepare, we need to style every possible element that might appear., so our sample content needs to include Everything. This is sometimes called the "Sandbox method." To get dummy content, I suggest visiting HTML Ipsum.

We're going to separate our sample content into a couple different posts, with titles wrapped in <h2> tags. Each post will also have standard information about date, author, etc, contained within a <small> tag.

Step 7 - Sidebar

In a typical blog, the sidebar holds various widgets, which display links to other pages or articles, and each widget is usually an unordered list of anchor tags. Here, we're going to have "categories", "recent posts" and "archives" widgets. So our sidebar div will contain three lists, each with a title wrapped in an <h3> tag.

Step 8 - Footer

The footer will work like the top bar, header, and navigation worked, with an outer div to hold a repeating background, and an inner div to centre the content and contain it within the 900px of our page. To do this, we just need to add the "container" class to our "footer content" div.

Our footer is going to have three columns: copyright info, links, and subscription options. Each will have to be contained within its own div.

Okay, we're done our markup! Lets take a look in the browser (I'm using Safari 4, which is awesome, by the way)


It doesn't look like much, but it has all our content laid out in a logical, readable way. It's important that an un-styled web page contains all the information needed for a reader to be able to understand and navigate the page easily. This ensures that anyone browsing with CSS disabled, or using a specialized browser (like a screen reader for visually impaired people), will still be able to access and understand content. Keeping this in mind will also ensure a logical layout, which will be easier to modify later.

Step 9 - CSS

Now comes the fun part: giving our page some style. Prepare yourself - to achieve the layout we want, we're going to have to face some confusing little CSS headaches. I'm going to try to explain the underlying concepts that lead to these problems, so that you not only learn how to solve them, but also gain a better understanding of how CSS works and how to deal with any problems you might come up against. Let's get started!

Create a new document "style.css" and link to it in the head of your html document:

Step 10 - Basic Clean-up

First off, we need to know what we're working with, which means getting rid of default browser styles.

We're going to use a simple css reset to get rid of those pesky margins and styles:

We also have the wrong font for our whole page, lets fix that:

The next step is kinda neat: remember how we added the "container" class to all divs that had text content? It's time to get that container to contain our content! Like I said before, we're making our page 900px wide, and we need out content to be centered.

And just like that, we have a nice 900px wide, centered webpage.

Step 11 - Starting at the Top

We're going to start at the top with the small blueish bar containing the subscription and login links. Okay, the first thing we notice about our two paragraphs at the top is that they're supposed to be beside each other, not on top. To do this, we need to float the elements.

How floats work

When we look at our webpage in the browser, we see a bunch of elements of different widths and heights. The browser, however, sees only a bunch of boxes stacked on top of one another, with each element taking up the entire width of the container. The webpage has a very simple "flow": each element is pushed down the page by the element above it.
To get two elements to sit next to each other, we need to take them out of the normal "flow" of the page.

When an element is floated, a few things happen: it gets stuck to the side of the page (or another element) and it is removed from the normal flow of elements - that is, instead of taking up the entire width of the page, it only takes up the space it directly occupies, allowing the elements below it to move up beside.

Lets try it with our two little paragraphs:

Take a look in the browser, and we have a problem! The h1 logo has moved itself up between the two floated items. When you float elements, it's like you're telling them to "break the rules", the only problem is, when you let some elements break the rules, other elements start doing it too! What we need is a way to tell the browser that these two paragraphs, and these two paragraphs ONLY, are allowed to break the rules, so after the floated elements, the normal flow is restored. To do this, we need to add a rule to the parent div of the two floated elements, which lets the two paragraphs inside it float without affecting the rest of the page.


To do this, we need to add a property to the containing div of "overflow" and set the value to "hidden".

It's not terribly important to understand what's overflowing, or where it's hiding, so long as you understand that the overflow: hidden rule controls the behavior of floats within the div. Now, I don't want to confuse or scare anybody, but this technique doesn't always work in all browsers. It works here, but always test in IE for your own designs. There are a number of other techniques worth noting, all of them have their advantages. Peter-Paul Koch describes a method similar to the one I use here in his article Clearing floats. Steve Smith describes his method of "Floating (Nearly) Anything" in his post Clearing Floats: The FnE Method. Also, it's a bit more of an advanced technique, but Tony Aslett has pioneered a rather ingenious and devious technique, described in the article How To Clear Floats Without Structural Markup. Choose the method that works best for you, and remember to test your browsers!

One more important thing about floats is that you should always specify a width. Your CSS will still validate if you don't (you will get a warning) but it's best practice and you sometimes wind up with unexpected results in some browsers if you don't.

Since we want our layout to be easily resizable, we're not going to use pixel values, we'll use percentages instead. We may as well let each element take up 50% of the width. Because we've given it so much space, the subscribe paragraph has moved to the left. We can get it to stick to the right again by specifying text-align: right.

Styling the paragraphs

Before adding a background image, I like to style the fonts so that I can see how much space I have to deal with.

Font sizes using ems

We want to make our web page as accessible and flexible as possible, to reach the largest possible audience, right? That includes allowing readers to resize text to a comfortable size. Now, different browsers deal with resizing in different ways, but as usual, the problem browser is IE/Win. In Internet Explorer, if your text size is set in pixels, you can't resize it, so your reader is stuck with whatever font size you've specified. That's not very nice, especially for readers with poor vision. To correct this problem, we need to use a different unit - ems.

Ems are a relative size unit - it means the width of the "m" in whatever font-size is specified. The default size set by browsers is 16px, so 1em would equal a font-size of 16px.

To make our web page fully scalable, we can convert all our font sizes to ems. An easy way to do this is with the web application Em Calculator (works best in FireFox).

However, to avoid doing any confusing math, there's an easier way. Since ems are relative to the default font-size of the page, if we change the default, the em values will be different.

To make the math easier, we can give ourselves a base of 10, by setting the default font to 10px instead of 16. To do this, we just specify in our CSS file that we want our fonts to be 62.5% of the default.

Now we just need to divide each font size in pixels by 10, and we have our em value. To start, the font size for our two <p> tags at the top of the page is 12px, which works out to 1.2ems.

Expandable backgrounds

Next, we add in the repeating background image we sliced from our PSD:

We need to add a bit of padding to the top and bottom of the paragraphs to make the image stretch to its full height. To get the right values, we need to go back to our PSD and measure the height of the bar with the ruler tool: mine turns out to be 26px tall. Since our text is 12px tall, the total padding will be 26-12 or 14px. This means we add 7px of padding to the top, and 7px to the bottom. (These values are sometimes off by a pixel or two, but are a good place to start, just keep checking in your browser)

We could also get the div to stretch to its full height by specifying a height of 26px, but you should always avoid specifying a height for your elements as much as possible, to allow for maximum flexibility. If you restrict your element to a specific height, you aren't allowing for larger text or additional content.

Check it out, it looks just like our PSD. But here's where things get tricky: try re-sizing the text in your browser. When we increase the size of the text, the ratios change, we lose our bottom padding, and the text eventually overflows beyond its background. To make a more "indestructible" web site, we need to get the background to stretch as the text grows or as more content is added, so that there is always 7px of padding at the top and bottom, no matter how big the text is. Since our background image is only 26px tall, we need to have something else to stretch out further. What we're going to do is basically put a solid color behind the image, so that when the text gets bigger and the image can't contain it, the color behind it shows through. The color at the bottom of our gradient is #08413c. So lets add that to our background. To specify that we want the image to always stick to the top of the element, so that the color stretches out from the bottom, we add a background-position value after the image url.

Now try resizing the text: the background grows with it, and it looks pretty much the same as it gets bigger. This also lets us know that if we wanted to add a second line of content later on, we wouldn't have to change anything in our css. The ability of an element to expand for more or larger text is often overlooked in web development, and this causes pages to break down when text is resized. Just check out my University's Homepage, try making the text one step bigger, and you lose a navigation link!

One more thing, we just need to add that 1px border to the bottom of our bar:

So here's where we're at so far:


Step 12 - Header

Since we're going to be making the text of our blog name and description white, lets add in the background first. We probably won't need this image to stretch out, but just in case, we'll repeat the same process of adding a background color and position:

Now lets do our font styles:

Alright, so now we need to get our description out beside our logo. This can be achieved by floating, but I tried it and ran into problems with specifying widths, and I managed to get a much better result using absolute positioning. Plus it gives me a chance to explain an important concept!

Absolute Positioning

If we want to position an element outside the "flow" of the page without using floats, we can use absolute positioning, which basically allows you to position an element anywhere within a div regardless of other elements within the div. This means that when you specify a position of, say, left: 10px, it will position the element 10px to the left of the side of the div, whether or not there is another element there.
First, in order to absolutely position an element, we need to set the position of the parent div to relative, because the absolute positioned element is positioned relative to the parent div.

Now we can set the absolute position of our description. When position: absolute is used, we can specify its position in terms of left, right, top and bottom, using pixels, percentages or ems. First, the top - the description is almost exactly 50% from the top of the header, so lets specify that:

Now we need to push it out to the right, by giving it a value for left:
if we use pixels, then when the text is resized, it will get closer to the h1, and eventually overlap it, so scrap that method. We get the same problem with percentages, only not as dramatically. The best thing to do is position it with ems, which as you remember get bigger as text size gets bigger, so the space between the h1 and description will remain when the text is resized.

And it looks great!


Step 13 - Navigation

Lets clean up this navigation menu, and get rid of the list style and text decoration, and add font styles.

To get our links to line up horizontally, we're going to set the list items to float: left, so that each list item is "stuck" to the one to its left.

We of course wind up with the same problem as with all floats, but all we have to do is add in overflow: hidden to the navigation div, and we're good to go.

Each list item is about 45px apart, so lets add 45px of padding to the right of each item.

Note: All these padding values can be written shorthand as:

The shorthand for padding (and margins) is the values for top-right-bottom-left all in one line. I find the best way to remember the order is to think of the compass directions: n-e-s-w.

Next, lets add the background image, employing the same technique as before to ensure our background stretches when text is resized:

To find the padding value, once again just measure the height of the bar (mine is 40px) and subtract the size of the text (18px) to get the total padding, and divide by two: 40-18=22px.

To move our navigation menu down a bit, the easiest thing to do is just add a margin to the bottom of the branding div.

And finally, the 1px border at the top of the bar:

That's it! We're done with the header! Let's take a look:


Step 14 - Content

The first thing we need to do here is to create the two columns - one for the posts, one for the sidebar. By now this should be simple: just float one to the left, one to the right.

And... Nothing happened. At least it looks that way, scroll down and you'll find your sidebar stuck to the right side beneath the posts. Before the sidebar can move in beside the posts, we need to specify how much space the posts div can take up, and specify a width for the sidebar as well. Again, we're going to use percentages instead of pixels, so that if the text is resized, the sidebar stays at the side.

And we need to hide the overflow of our containing div:


Great, we have a nice little two-column layout!

Step 15 - Styling the Posts

Since we used a CSS reset, the content of our post section has no style at all, and we need to go through the tedious task of styling every single possible element. My workflow is a bit different for this than with the rest of the page, as I don't really refer to the Photoshop document. I find font styling in Photoshop to be a real pain, so I usually skip it. To get the styles for my posts, I go through a process of trial and error. I usually start with settings similar to default browser styles, which you can find at This Link, and tweak them to my liking. I'm not going to go through the whole process step by step, but this is what I came up with in the end:

Which should look something like this:


Now there's about 35px at the top of our post section, but our h2 tags already have a margin of 7px at the top, so lets add a margin of 28px to the top of the posts div.

And that's about it for the posts section. On to the sidebar!

Step 16 - Sidebar

First off, lets add in that background color:

And fix the font styles:

We also need to push the sidebar down by 25px (35px - the 10px margin at the top of the h3 tags). This time, however, we can't use the margin-top property, because this will move the entire sidebar, including the background, down the page. We want the background to start right beneath the navigation bar, but the content to start 35px below that, so we need to use the padding-top property. We also need padding on the left, right and bottom, and 25px sounds about right, so we can declare it all in one padding value:

But oh no! Our sidebar is no longer at the side! This is because we added padding to the sides of the sidebar. When you add padding to an element, you are actually making it bigger. We just made our sidebar 50px wider, so now the widths of the floated elements add up to more than 100%. To fix this, lets first convert our 25px of padding to a percentage of 900px. It works out to about 2.7%, but I'm rounding up to 3%.

Note: this is another shorthand value, it means that the top and bottom are both 25px, and that the left and right are both 3%.

Our sidebar is now 33+6% wide, so it's still too wide, but all we need to do now is subtract the 6% from the original 33%

I thought the sidebar looked a bit wide at this point, so I reduced it to 25%.

The important thing to remember is that all the padding, margin, and even border values add to the width of the element, so you need to adjust the width property to compensate every time you add padding, margins or borders.

And there we go, no matter how big you make the text, our sidebar stays at the side, and the ratio between the post area and sidebar remain the same. It's an excessively plain sidebar, but I'm not going to bother styling it any more, just because its not really important to the goals of this tutorial.


Step 17 - Footer

First lets make the font a bit bigger and add some styles:

Next we can add in our background image:

And since we want it to be able to stretch, we can add in the color at the bottom of the gradient to our background value just like we did before:

Next add in that 2px border at the top:

Lets add a margin and some padding to the top and bottom:

Next, we're going to create three columns by floating. Floating three elements works pretty much the same as floating two, we're going to float each column to the left. We need to declare the widths of each element being floated, using percentage values to allow for expansion.

Just remember that we have to set the overflow to hidden in the parent div.

At this point, I decided that it would actually look better if the links and RSS divs came first, and the copyright info was at the far right, so I changed the order of the divs, and set the copyright div to float: right, and aligned the text to the right as well.


And there we have it, we're done coding and styling our page!

Step 18 - Accesibility testing

Througout the tutorial, I've been testing my page in Safari 4. Since Safari 4 is probably the most standards-compliant and modern browser, it displays web pages the most predictably. If only everyone used a standards-compliant browser... Unfortunately, there are still people out there using outdated browsers, and we need to prepare for that.

Let's start with the most problematic browser first: Internet Explorer 6. I use a mac, and have yet to find an effective (free) way to test my web pages in Internet Explorer. If anyone reading this knows of some sort of magical technique, please, let me know. Anyways, time to haul out the ol' family Dell. To install multiple versions of Internet Explorer at once, google "Multiple IE" and download the .zip file.

Surprisingly, it works out just fine in IE6! IE7 is okay too. Text is resizeable! I also tested this page in Firefox, Opera and Camino. I had trouble installing Google Chrome on the PC, so I couldn't tell you if it works, but I think it should, bceause of the simplicity of the markup and style.


That's it! Hopefully you've learned a thing or two about coding flexible websites. Check it out in any browser, make the text bigger, make it smaller, and our layout adapts just fine. Disable CSS, and it still makes sense! I hope you can see how easy it is to make your websites less fallible! This page was very simple, without many complicated challenges. When your layouts get more complicated, it will be a bit tougher to maintain flexibility.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.