Slice a Template and Code it Using CSS
41,914 ViewsHTML Tutorials, CSS Tutorials April 1st, 2007
This tutorial is going to show you how to take a template (not create it) and slice it up in Photoshop and code it in complete CSS. No use of that tables crap, CSS is much faster then using tables, your pages will load a lot faster. Plus you have a lot more control over your templates, and your code will be a lot neater.
This is the template that I will be slicing up and code -
You can steal that template if you want, use it on your own website, use it for this tutorial. Claim it as your own. I don't care, I made it in 45 minutes and it's up for grabs!
As you can see, it's not the prettiest template ever made, but it will work perfect for this tutorial, you can take all the techniques you learned in this tutorial and just transfer them over to any other template that you want to code. Once you get the basics done you will see it's actually pretty easy to do.
Throughout this tutorial, I will be switching back and forth between slicing small parts of our template and the actual CSS coding.
So let's start off by defining our HTML document.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
-
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
-
<meta name="author" content="TutorialCode.com" />
-
<title>Slicing a Template</title>
-
<style type="text/css" media="screen" title="Main Stylesheet">@import "style.css";</style>
-
</head>
This just defines your doc-type, does all your fun header stuff. Now, let's start our CSS file, this is also where we will be looking at our template.
-
body {
-
background-color: #585858;
-
text-align: center;
-
margin: 0px auto;
-
font-family: tahoma;
-
font-size: 11px;
-
}
This is where we define our body. In simple terms, this is where we say what we want all the global things to look like. The fonts on the page, size, background color.
For the background color, I just went into photoshop, used the color picker tool and got the color of the background. Plop that hex code into your background-color, and your set.
You may also see that I have text-align: center & margin: 0px auto; - these are 2 small hacks that let you center your layout in the browser, automatically. The margin: 0px auto is what centers your layout in Firefox, now this doesn't work in IE (Internet Explorer), but what does thoguh? So we used text-align: center. Now you may be thinking : "I don't want all my text centered though!". This is ok, we will be setting it back to normal after. Now, more CSS, YAY!
-
#container {
-
width: 700px;
-
margin: 0px auto;
-
text-align: left;
-
}
Add that below your last CSS statement, this will be our container, our container is invisible but holds ALL of our template inside of it. Think of it, as a tupperware and all of our template is...peas. I got the width: 700px thing by just measuring the total width (left to right) of our template. Usually when you making a template, you will know how wide you are making it, if not then take your marquee tool, select from 1 side of the template to the other, then in your info pallette (usually grouped with your Navigator) will show the width of your SELECTION (it's the W : ###).
Then, we want our container to be centered, don't worry this is the last time we have to do this, since everything is inside our container, it is centered too. Then this is where we set our text back to normal, and it will be aligned to the left again! Instead of doing our HTML, I'm gonna write some more CSS and get it out of the way :P. So let's create our header -
-
#header {
-
width: 660px;
-
background: url(images/header-bg.jpg) repeat-x;
-
height: 78px;
-
padding: 20px;
-
}
-
#header a {
-
font-size: 20px;
-
font-family: arial;
-
color: #000;
-
text-decoration: none;
-
}
-
#header a:hover {
-
text-decoration: underline;
-
}
This whole chunk of CSS code is how we make our header, now when dealing with CSS you have to realize, when you use padding, you have to adjust the height and width accordingly.
So, the actual width of our header is 700px, but I am using a 20px padding, and since that means there is padding on the left AND right, that is a total of 40px of padding. So I subtract 40 from the total width of our header, and do the same with the height.
Now for the most important part, our header background. As you can see, it is just the gradient from our picture, except the only difference is we are only using a 1px wide image.
Here, I will show you a picture of what I'm doing in Photoshop -
So as you can see, all I'm doing is making a 1px wide image, of 1 part of our gradient, I then set it the background image of our header to this image, (the 1px wide part of our gradient) and use repeat-x;. This will repeat this small part of our gradient horizontally so that it seems like its 1 big image. Save this image in an /images directory and save all your images there. I save my image name as header-bg - this fits well seeing as how, it is the background for the header :).
The rest of the header mumbo-jumbo is just some styling for our header text, I style our links, and then style our link when your cursor is hovering over it, pretty basic stuff and very straight forward. Now, more CSS!
-
#hnav {
-
width: 700px;
-
background: url(images/navigation-bg.jpg) repeat-x;
-
height: 25px;
-
padding-top: 7px;
-
}
-
#hnav a {
-
padding-left: 60px;
-
padding-right: 60px;
-
color: #fff;
-
font-weight: bold;
-
text-decoration: none;
-
}
-
#hnav a:hover {
-
text-decoration: underline;
-
}
Alright, this is pretty much the same CSS coding, as we used for our header except for the fact that the height is different, and we are using a different image.
So we get our background image the same way that we got the background image for our header -
Now, we just do the same thing we did with our header background, if you compare the 2 codes there pretty much the same, except this time we only put padding on the top so our links can be more aligned in the middle of our navigation bar.
Then the rest of our code just styles our links, I have made it so that it fits perfectly for 4 links, if you were using MORE then 4 links for example, with this exact template, you would have to lower the left and right padding for the link, this is that the links are all evenly spaced out, and the more links you add you will have to lower the padding.
Now for some more CSS, we are going to make our sub-navigation box, use some new techniques as well!
-
#snav {
-
width: 155px;
-
border: 1px solid #484848;
-
text-align: center;
-
padding-top: 20px;
-
padding-bottom: 20px;
-
float: left;
-
background-color: #747474;
-
}
-
#snav ul {
-
list-style: none;
-
padding: 0px;
-
margin: 0px;
-
}
-
#snav a {
-
color: #71fd2a;
-
text-decoration: none;
-
}
-
#snav a:hover {
-
text-decoration: underline;
-
}
There is quite a bit of CSS here, some of it new. Some of it not. This box, our sub-navigation doesn't have a background image, we are just using a solid color. So I just use the color picker in Photoshop, get the color of the background in our sub-navigation and pop that into our background-color: in our CSS. You have seen everything there, except for the float: left; - This is how we are gonna get our sub-navigation and our content, if you didn't put that your sub-navigation and content would be on different lines (or sections). Content underneath your sub-navigation.
Then we say that all ul tags in our sub-navigation will have no list-style, so no little dot next to them. This is a good method for making vertical navigation lists, using <li> tags.
Then we just style our links and thats that for our sub-navigation. Next onto our content box, this one is a bit trickier because we have our news post box, inside of our content box. Phew, slicing a template is tedious! I hope your still with me
-
#content {
-
width: 537px;
-
background-color: #747474;
-
border: 1px solid #484848;
-
float: left;
-
margin-left: 4px;
-
}
Alright, that is just the beginning of our content area, we need to the little box inside of the content area next, this just makes a simple box, it just mimics our sub-navigation box except the width is wider obviously.
-
#content .header {
-
background: url(images/content-header-bg.jpg) repeat-x;
-
height: 19px;
-
width: 483px;
-
border: 1px solid #454545;
-
padding-top: 4px;
-
padding-left: 15px;
-
font-weight: bold;
-
-
}
-
#content .text {
-
background-color: #a2a2a2;
-
border-left: 1px solid #454545;
-
border-right: 1px solid #454545;
-
border-bottom: 1px solid #454545;
-
width: 484px;
-
padding: 7px;
-
}
This is where we create the little header image part, so in other words a title for our posts. And then the area where we input our content, or text per say. We take a 1px wide image for the background of our title bar, (you have seen this many times before) and then we do the same thing we always do, then I go through the exact same thing as we have done for our other boxes, and navigation. Then I make the text area for our post, this is extremely simple, just a box with borders on the left, right and bottom. Why none on the top? Because our title has a border ALL the way around it, so it makes up the border for the bottom of the title, and the top of the text area, we don't want a double border!
Almost done! Next we just do our footer, then I show you some of the HTML quickly and were done!
-
#footer {
-
width: 690px;
-
background: url(images/footer-bg.jpg) repeat-x;
-
height: 32px;
-
padding: 5px;
-
margin-top: 5px;
-
color: #cbcbcb;
-
font-size: 10px;
-
}
-
.end {
-
clear: both;
-
}
Alright, this is our footer, we make it just as wide as the rest of our template, give it a background image (which we have done numerous times before), give it a margin on the top so there is room between it and the content box/navigation. Now, you will see .end - this will save you in SO many situations when using CSS. In the HTML you will see where I use it and I will explain it to you.
Alright you are totally done with the CSS part of the site! Now I'll show you some of the HTML, and that's it!
Alright, since we have got all our CSS out of the way, I can just zip through the HTML easy, and you will understand everything that is going on :).
That is where we define our header, remember we called it header in our CSS file? Then I make a quick link, nothing fancy. That is why I styled it all nice in my CSS file, because it will be the main website title.
This is our horizontal navigation, so we start off our navigation, by using a div, now since we already defined a background for our navigation all we have to do is add simple text links! And since they are already spaced out properly, they are extremely easy to add, just add a link tag and your done.
Now, do you remember when we made this sub-navigation. We used an un-ordered list <ul>, and then we just made list items for each of our links, this makes for neater coding, and it is a lot easier to style and transform via the CSS file.
-
<div id="content">
-
<div class="header">
-
News Post 1
-
</div>
-
<div class="text">
-
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam imperdiet, lacus nec hendrerit suscipit, est felis ultrices diam, ac facilisis nunc turpis a turpis. Duis dictum varius nulla. Integer pulvinar libero in leo. Etiam nonummy tortor a odio mattis porta. Nulla feugiat. Sed vitae arcu ut augue tincidunt sollicitudin. Nullam et ante. Sed leo nunc, pulvinar id, tempor hendrerit, vehicula vel, velit. Sed a libero quis elit placerat bibendum. Morbi facilisis nunc a est. Cras porttitor lobortis erat. Integer pellentesque semper ipsum. Vestibulum posuere.
-
</div>
-
</div>
Now, we start our content box, in our content box we make a header, so that we can put our title in it. Then after that we add a text box right under it, I fill it with some simple lorem ipsum, this is just filler text so you can get a feel for what it looks like with text in it.
Now for our footer,
This is where we use our end class we defined at the end of our CSS file, now remember when we used the float: left for our sub-navigation and content area? Well that made those 2 things next to each other. Now what if we wanted our footer underneath those 2 things, if you just did it normally without add this end div, you would notice that your text is in the right place, but the background image would be behind everything, and way up high.
This is because the sub-navigation and content area are still floating left, and technically nothing is filling the gap underneath the main navigation, it is a bit hard to explain, so go ahead, try it now. Remove the end class div from your HTML and try it out, see what happens :).
Alright, that concludes my tutorial on how to slice and code a layout using CSS.
If you have any questions on this tutorial, leave a comment. You can download ALL the source files for this tutorial (images, css file and HTML) just below here. This template has been tested and works in both IE (internet explorer) and FireFox.
You can view a demo of the outcome here -
http://tutorialcode.com/slicing-tutorial/slicing-coding-tutorial.html
As you can see, it looks almost exactly like our static image in the beginning of the tutorial, and when you highlight everything, only the text is highlighted!
I hope you stuck with me through it all!
Thanks,
Sean
TutorialCode.com
Download All Source Files - HTML, CSS and Images :
Slice a Template and Code it Using CSS - Source

(24 votes, average: 4.21 out of 5)













April 2nd, 2007 at 11:10 am
Pretty nice sean, ill start practicin
April 2nd, 2007 at 11:14 am
Another thing, whit the banner if i dont like just a Gradient map header i can put a normal one whiout the bg-repeat:repeat-; thing rite?
April 3rd, 2007 at 1:00 pm
Yup, if you don't want to use a gradient all you have to do is change the URL to the banner that you want, and take out repeat-x;
Or you could just add it in through the HTML, both work well!
April 9th, 2007 at 3:11 pm
Why not define .head and .text as #head and #text? After all, you will only use these elements once.
April 9th, 2007 at 5:32 pm
Well the whole point of the news box thing is that you can have multiple instances of each one. 1 for each news post in other words.
I like to use # to define boxes, and create boxes, and the '.' to define text formatting, because usually you will use text formatting multiple times.
April 15th, 2007 at 9:12 pm
For some reason the footer and content box area are combined. I checked the margins and I tried copying the footer and content codes directly from here... I need some help please.
Good tutorial, BTW.
April 16th, 2007 at 9:13 pm
@Adam - Hey! I'm glad you liked the tutorial, sorry to hear your having problems. Do you have any direct examples of the problem so I can get a better idea of what's going on and a visual aid as well?
Chances are you might be missing one small part of the code
April 18th, 2007 at 4:47 am
I think you were right about missing a piece of the code. I somehow fixed it... somehow...
Anyway, great tutorial and thanks for the tutorial. It helped me out a LOT!
April 23rd, 2007 at 5:46 pm
@Adam - I'm glad to hear you fixed your problem, always feels good to figure things out yourself eh? But I will always be here to help you with questions that you have with any of the tutorials on my website or even with any PHP questions that you have.
April 24th, 2007 at 4:49 pm
Just wanted to say that this tutorial is great and helped me a bunch. Thanks alot! I just had one quick question.
In the last code box in this page, where you insert the footer div (in HTML), why is there an "" tag right before the tag? What div is that closing? As far as I can tell you already closed all of the div tags (the .end div and the #footer div)
Thanks!
April 24th, 2007 at 4:51 pm
just to clarify my previous comment, i asked "why is there an "/div" tag there?
April 24th, 2007 at 8:00 pm
@Chris - I'm glad this tutorial helped you. To answer your question about the ending div tag. That is the finishing div tag for the container, everything is contained within the container!
If you have checked, and checked again and you are SURE it's not then I may have added in an extra ending div tag. Put I am pretty sure I didn't.
April 25th, 2007 at 6:18 am
Hi, This is very nice css & html tutorial. I agree just very nice web layout.
April 25th, 2007 at 12:09 pm
Oh ya, duh
Thanks again for the tutorial 
April 27th, 2007 at 1:33 pm
My header and hnav wont join, they leave like a big gap inbetween them. Any ideas?
April 27th, 2007 at 2:05 pm
Hey, i don't seem to able to make the navi(hnav) and the header join. they have a huge space between them. Any ideas?
May 1st, 2007 at 3:09 am
this tutorial is great, given that the university i went to isnt that detail either. I learn so much from this. Thanks.
if you have anymore of such tutorials please do let me know.
May 1st, 2007 at 5:15 pm
Thanks for the great tutorials..I like the way you bring the techno words like "container" down to kitchen level for guys like me..Simply makes sense..Nothing is hard when you really understand it..Great job to all.
May 22nd, 2007 at 3:00 pm
I'm pretty new in html/css and this is a perfect tutorial for me. However, I have two questions:
How can I put my logo in the header area?
How can I implement master page using this template?
many thanks!
May 25th, 2007 at 2:24 pm
wow this tutorial is really good and so easy to understand.
June 5th, 2007 at 8:37 am
Thanx, your tutorial helped me a lot in getting my first real step in CSS
June 7th, 2007 at 2:49 am
Okay let me just get this out there.. I seriously have no coding skills WHAT SO EVER, I can do advanced 3d renders and animations and graphics - yet I could not code anything at all for the life of me. To put it blunt I was code-challenged (retarded for a lack of a better word).
I Glanced at you're tutorial.. opened up Dreamweaver and I already have half of my portfolio site done within an hour.. Awesome work on this tutorial, I feel like a genius right now. lolz
Thank you so much..
June 27th, 2007 at 5:27 am
Hello, nice tutorial.
But i noticed that on the end result, the #snav box is not in the same height as the #content box.
If you put more and more content the difference will be more noticeable.
Can you suggest a fix for this?
July 3rd, 2007 at 1:07 am
Thanks for your great tutorial, it's very helpful to redesign my html table design to css design.
Keep these great works.
July 21st, 2007 at 5:21 am
Hi admin
If we have more complex design then how it is possible to manage through css
July 21st, 2007 at 5:22 am
and one thing more that if I want to add more images how can I put up through css i.e., logo, product images
July 21st, 2007 at 5:30 am
IF I have 100% width layout then how can i manage because who had taken fixed px width i.e, #header { width: 660px;
November 7th, 2007 at 4:42 am
This is just what I've been looking for in order to get a page of my own up and running.
There's a s***load of sites explaining how to slice&dice but none of them care to explain what happens next, how to actually turn ones nifty photoshop-ka-ra-te into a smashing website
Awesome
Thanks!
December 11th, 2007 at 6:49 am
Very good tutorial , after spending hours of searching I have landed to the right page. With your step by step tutorial I m confident I will be able to code in html after designing in photoshop
December 25th, 2007 at 1:38 pm
Nice tutorial. I didn't mess with CSS a lot but I think I'll start a bit now...
January 1st, 2008 at 12:23 am
OMG!! THNX... This was what i was looking for:D I searched for google and WOW!! this fine site come up..:D
Great work keep them cooming..
January 1st, 2008 at 5:22 pm
[...] Tutorial Code [...]
January 14th, 2008 at 11:31 pm
hey, nice tutorial. i was looking this tutorial before, and finally i got it... thnks..
January 29th, 2008 at 3:11 pm
mmmhmm..im having a hard time understanding this..Is it ok if you could email me because im in class right now and i have a quick question for you [:
speedy_heights24@yahoo.com
March 15th, 2008 at 2:21 pm
umm...ok...
so this is my 1st time coding anything...
i made my new template and all...
so how do i "describe my HTML document"?
March 21st, 2008 at 7:34 am
awesome tutorial, thanks mate
April 19th, 2008 at 11:11 pm
Damnit thanks a MILLION man. I learned a ton from your tutorial. I found css coding difficult until this. Now i understand it. I get minor problems but I can then quickly identify them. Thanks =)
April 20th, 2008 at 5:10 pm
This is a great tutorial and I am using the CSS part as a basic template (with a few extras)for designing a stylesheet.
Thanks.
May 7th, 2008 at 7:37 am
Man you rock!
One question; if I'm creating a whole page of many slices stuck to each other and I want to use DIVs instead of tables, is it do-able? ok, I know it's doable, I kinda figured it out but instead of sticking the slices horizontally they go vertical for some reason. Can you give me an example of putting, let's say, 3 images together horizontally then jump to the next line and stick 3 more others, so that we get a full image broken down into 6 slices, you get what I mean :p? Oh, and I also want to add text on those slices, as if the images are backgrounds. A lil help pleaaase :D!!
Cheers,
Nora
May 16th, 2008 at 6:39 am
That is such a great tutorial. Do you think you could do some more? I've been looking for a simple tutorial showing me both the design/slicing and coding side of it. Many thanks
James
June 14th, 2008 at 3:00 am
Thanks alot !
July 30th, 2008 at 1:31 pm
THY with this tutorials i learns div and css THY very much!