Using CSS to layout a page can be frustrating at best.
It's a little like nailing jelly to a tree. This tutorial is a set
of pages that demonstrate different techniques and while displaying
the code at the same time.
You'll be able to use these pages as a beginning point with your
design work.
The CSS code is displayed on each page so you can print them out and use
them as a reference. These pages also make a great starting point
to help you learn by "noodling around."
Understanding CSS is a valuable skill to have in today's Web world.
This tutorial consists of several page examples.
Use each one as a starting point for your own work, adding more CSS to change the margins, fonts, and colors. You can also combine any of these techniques together. For example, the multipleBoxLayout can be included inside a single column of the 2ColLayout by changing the image size and amount of text. Or, add a pullQuote to one of your content blocks.
To start, download the XHTML files and graphics. Right-mouse click on
each link and select "save target as" to download to your computer.
You should organize your files using this screen shot as a guide:

Notice
that all the graphics are inside a folder named: graphic.
Here are the XHTML files:
2ColLayout.html
3ColLayout.html
cssLayoutTutorial.html
(this tutorial page)
multipleBoxLayout.html
overlapBox.html
pullQuoteDemo.html
Here are the graphic files:
blueGreenLight.jpg
glassCeilingGrid.jpg
hero.jpg
spy.jpg
thumbprint.jpg
Here are some gradients that you might want to use to add texture to the page. Right-mouse click on the ones you want and select "save target as" to save to your computer. Put these in a folder named: gradient inside of the graphic folder.
![]()
Noticed how I used this class rule to make the gradients
easier to see:
/* put an outline around each gradient so it is more visible */
.gradient {
border: 1px solid black;
}
Each img tag contains a class="gradient" attribute that utilizes this
CSS rule:
<img ... class="gradient"
/>
Here's some tips 'n tricks that will help make your CSS development a little easier:
Start with the two column layout.
The key to this page is the float:left attribute used in the sidebar rule. Normally each element will "flow" onto the page, each one starting on the left margin. By using the float:left attribute you are telling the browser to allow other elements to flow to the right.
The other important attribute is the clear:left found in the footer id rule. This tells the browser to clear any left floats and to start this block on the left margin.
Noodling around with the code:
The three-column layout is very similar to the two-column.
Position is Everything.
The trick here is to use the float for left and
right to position the two outer columns. Then use add the middle column
setting the left and right margin so there is no overlap with the other
two columns.
The order of the text in the <body> of the page is important. If you put the blocks of type in this order:
then the right column will position itself vertically starting at the bottom of the content column text. As soon as you move the text blocks around to this order:
Than all the float: attributes will be able work as they should.
Another rule when using float: The browser needs to know how wide a column is so always include the width: attribute when using float.
When you want the footer block to move to the left you have to use
clear: both;
in order to clear both the left and right float.
Noodling around with the code:
Click on the image for a larger view
Professional Tip: Establish a set of standard id class names and use them on all your pages. That will allow you to quickly adapt the CSS code from one site to another. Make sure these are unique names that won't conflict with HTML or CSS keywords. Some of the more common element names might include:
Using the class=" " attribute.
So far we've been using the id=" " attribute which designates a single entity on the page. You can also use the class=" " attribute which can be used multiple times on the same page. For example, let's say you want to display several photographs along with summary text. You could set up a class="summary"and use it for each photo/text set.
The Multiple Box Layout page shows how this is done. Notice how the Two Column Layout was used, adding the summary blocks in as content for the right column.
Controlling How Excessive Text is Handled (auto scroll bars)
The CSS code establishes the height of the summary block to be just
a little larger than the highest image. But, what if the text summary
takes up more space?
By using
overflow: auto;
the browser will automatically add scroll bars so the user can access
the overflowing text.
You can see the vertical and horizontal scroll bars by changing the size of the window.
Specific Styling using Sub-blocks
This example also demonstrates how you can style specific sub-blocks of content. Look at the code:
.summary {
border: 3px solid
blue;
background-color: #FFFFFF;
padding: 5px;
height: 160px; /*
height of highest image */
overflow: auto; /* if the page
width narrows, add scroll bars */
}
.summary .graphic {
border: 3px solid purple;
float: left;
width: 100px;
}
.summary p{
border: 3px solid gold;
margin: 0px;
}
First, the .summary class sets up the main
block.
Don't forget the period "." at the front of the class name.
Then, inside of each summary block there will be .graphic classes
and paragraph objects. The .graphic classes are designated by including
the class=" " attribute inside of each <img /> element. Here's an example
using the first image. The class attribute is highlighted using blue:
<img src="graphic/hero.jpg" alt="Silouhette against
the light" class="graphic" />
Back in the CSS code you can see where we tell the browser to apply
the style ONLY to the graphic class that are inside the summary class:
.summary .graphic {
That allows us to use the graphic class other places on the page without
having an all or nothing style.
(Notice that both of these items start with a period "." because they are
both classes.
We do the same thing with all the paragraphs that are inside of the
summary block:
.summary p {
You don't need a period in front of the p designator because p is a
standard XHTML element and not a custom class like .summary or .graphic.
The Overlapping Box example shows how to use the z-index: attribute to layer boxes that might overlap each other. It also demonstrates the Parchment Effect or changing the transparency of a box.
The Z-Index
CSS has been set up so several boxes can overlap each other and the designer can designate the order that they appear to the user. The higher the number the closer the box will be to the user.
In the example code you will see that the #content id has a z-index
of 5:
z-index: 5; /* higher numbers closer
to user */
and the #graphic id has a z-index of 0.
When the page is displayed you will see the text layer (id #content) on the top and always visible. You could use numbers like 0 and 1 but it is nice to have some flexibility allowing other layers to be inserted, so I recommend numbering in sets of 5.
This is a very useful technique when two blocks on the same page may overlap if the window is made smaller. As the developer you will have to determine which one should be visible. The box with the most critical information should always be visible.
The Parchment Effect (Setting Transparency)
This example also includes several lines of code which change the transparency
of the id #content object. Here's the code:
/* Set the opacity of the layer
*/
filter: alpha(opacity=90); /* IE 5.5 or higher
*/
-moz-opacity: .90; /*
Mozilla 1.5 or higher */
opacity: .90; /*
Mozilla 1.7 & FireFox
*/
Each line covers the different browsers as noted in the comments. Try to keep the transparency levels fairly high so the text is always easy to read no matter what the graphic is underneath.
This effect seems to trigger the JavaScript security features of IE 6.0 so the user may have to "unblock" the content in order to see the effect when using IE 6.0.
Noodling around with the code:
The final example uses a little JavaScript as well as the DOM (Document Object Model) which are more advanced techniques, but if you can copy and paste you will be able to easily add this effective technique to your pages.
A block quote is text from the page that is enlarged and styled to catch the reader's attention.
Simply copy and paste the <style> and <script> element into your pages,
making certain they are inside the <head> section of the page. To
create a block quote simply put a <span> element around
the text you want to highlight:
<span> class="pullquote">
The text that should be included in the pull quote
</span>
The elegance of this technique is that it uses existing copy. You don't have to write (or maintain) a separate block of copy for the pull quote. This means that if JavaScript is turned off, or the user is viewing the page in an old browser that doesn't understand the DOM, no information is lost.
Don't worry if you don't understand JavaScript or the DOM. Just make sure you copied the code correctly.
On the other hand, feel free to change the CSS code for the pullquote class to match the font and color scheme of whatever web site you are building. This is the beauty of CSS!
You now have several valuable tools to include in your designer's tool box.
Keep them handy and include them as a regular features of every template you build. Once you have a well designed template you will be able to replicate for all the pages of the web site you are building.
Here's what you should now be able to do:
*** *** ***
This is the end of the CSS Layout Tutorial
Written by Peter K. Johnson.
Read my blog at
http://webexplorations.com/blog

Questions? Send me an email: peter.johnson@southcentral.edu
Read my blog at:
http:webexplorations.com/blog
Strong Start, Successful Finish