In the modern web era XHTML and CSS based webdesigns are getting more and more popular. The advantages are obvious:

  • Cleaner HTML sourcecode
  • faster webpages
  • higher accessibility

The problem is, that there is a whole bunch of directives that you have to learn first, and once you have learned them, you realize that CSS does not always behave the way you expect it to or browsers interpret it different - you have to write workarounds.

But often people encounter these problems because they just don’t understand what the CSS code they are writing is actually doing.

image1.jpgOne scenario, that is not necessarily self-explanatory for CSS beginners is how to put two div boxes next to each other - a simple two column layout. They want to create a structure like shown in this image.

One of the problems is, that they don’t think in CSS containers yet. What you usually want to do, is having a wrapper, or mother container, around the structure elements (I call them like that. This would be the navigation and content container).
So the structure you are planning should look like this:

image2.jpg

image3.jpgThe advantage of this wrapping container is, that the navigation and content area always have the same height. This way, if we added a footer underneath it, it would be pushed further down automatically, the more the content expands.

We can also center the design very easily etc., but that would be beyond the scope of this introduction to floats. For now just believe me, that it makes more sense to have a wrapper around the structure elements.

Finally we start to write HTML

So what people now do is to begin and write the markup for that website, which might look like this:

<!-- ... header and other markup -->
<div id="wrapper">
<div id="navigation">
<!-- links here -->
</div>

<div id="content">
<!-- content here -->
</div>
</div>
<!-- ... footer and other markup -->

CSS comes into the game

The CSS they create for this, looks like this:

#navigation
{
float:left;
}

image4.jpgWhat does this float mean? It basically tells the browser, on which side of the mother element (in this case the #wrapper div) the navigation should be positioned. This is exactly what we want. The content is nicely moved towards the center of the design and you can see the content area. But when we have more content and the height of the content container exceeds the navigations height, it moves back to the left side, which looks like this.

We do not want this to happen, so we need to assign the navigation container a fixed with, and give the content some space on its left side (we “push” it to the right by adding the margin-left):

#navigation
{
float:left;
width:200px;
}

#content
{
margin-left:210px;
}

The problem of not talking to the mother

image5.jpgYet another problem that most CSS beginners don’t know about is the fatal behavior of floated elements in relation to the mother elements height. We would expect, when the navigation grows in height that the mother element stretches with it, but it doesn’t.
As long as the content container is longer than the navigation container, this effect does not occur, because the content stretches the mother container. But once the navigation is longer than the content, it runs out of the container (see picture).

This is definately not, what we expected and at this point many beginners decide that CSS is stupid and stick with their HTML table designs. But wait, didn’t I say that there is a whole bunch of possibilities out there to overcome weird CSS behavior? Yes!

CSS-Magic to the rescue!

We have two possibilities to make the #wrapper container stretch with the navigation. We could either:

Make it a floating element too

#wrapper
{
float:left;
}

or We could change our markup a little bit

<!-- ... header and other markup -->
<div id="wrapper">
<div id="navigation">
<!-- links here -->
</div>

<div id="content">
<!-- content here -->
</div>
<br class="clear" />
</div>
<!-- ... footer and other markup -->

You may have noticed, that we added the <br /> after both, the navigation and content. Just adding the break does not change a whole lot in our design. But by adding some CSS  (therefore the class “clear”) we can achieve a big effect:

br.clear
{
clear:both;
}

image6.jpgWe can make the #wrapper element stretch, too. Why does this happen?

The clear attribute places all elements underneath the floating elements on both sides of it.
Since #navigation is floated to the left side, and the break with clear:both moves when there are floating elements either to the left or the right, it moves down and stretches the mother element #wrapper. The image on the right side illustrates this.

Which version should you use?

This depends on the scenario. Both versions have advantages over the other. In the first version, you have to add a float to the #wrapper element, which could destroy its centered position, if it was centered. It would just appear on the left side.

For the second scenario you have to add additional markup, which would contradict the basic idea of xHTML and CSS designs, just having the content structured and do the design with CSS. However in some cases it might be the most elegant way to achieve the desired effect.

Social bookmarks These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Digg
  • Yigg
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Google
  • Slashdot
  • Reddit
  • Technorati
  • MisterWong
  • e-mail


1 Trackbacks/Pingbacks

9 Comment(s)

Yare says 7th April @ 22:30

Quite good explanation. Useful too. Personally, I don’t look at CSS so religiously. For me XHTML can contain even table elements, but if all the styling is CSS based it’s still CSS design. I mean XHTML should be used for construction and CSS for styling. HTML styling elements like for example should be avoided and that’s it. Why should we use only elements in a design to be called ‘CSS design’?

Yare says 7th April @ 22:35

ehm… a correction:

“HTML styling elements like for example <font> should be avoided and that’s it. Why should we use only <div>elements in a design to be called ‘CSS design’?”

Shadow says 8th April @ 14:39

Hello Yare, thanks for your comment!

It depends on how well you want to follow the separation. The problem with <font> is, that we move the styling of the text into the HTML layer, which is supposed to create markup, but not the design.

I use <span> with several classes :)

Webby says 9th April @ 0:55

Well… not quite sure where Yare got the whole bit with “div only” but I only skimmed through the article.

I think he was talking about using and exclusively, with the loss of such tags as , for example, which, if any web designer ever did, would show just how ignorant they actually were.

tags, for instance, are, without a doubt, more powerful than CSS in a huge number of cases.

and tags are critical, as simply using the bold and italic css styles won’t affect screen readers, whereas the and tags will (different strength of the voice and different inflection).

tags are also more powerful than CSS, considering search engines won’t recognize that your Fancy Page Title block is any more important than your Lorem Ipsum block. As well, accessibility (screen readers) will read the tags differently and/or with a notice that it is a heading.

These are just examples of what I (and I think all three of us) meant.

Webby says 9th April @ 0:57

Oops… I hadn’t anticipated those tags being applied to the comment.

Second paragraph: “talking about “div” and “span” exclusively”, loss as “p”.

Third paragraph: “‘p’ tags, for instance”

Fourth: “’strong’ and ‘em’ are critical,”

Fifth: “‘h#’ tags are also”, “won’t recognize that your fancy ’span class=’title’”, ‘than your ’span class=’content’”

Oops on my part.

Yasir says 9th April @ 14:58

good tutorial, i hav one question if you explain i would really appreciate.
in your second scenario where you use clear both.
if i use two more div inside content div. example below

and here i clear both and again copy paste above two divs.
i hav the same problem here mother elements height .

fbaldoni says 11th April @ 13:36

The problem I have found with floated designs generaly show up when you resize the browser. Then is when you see things wrap to the next line when you don’t expect them to. I have tried to use as little table layout as possible but sometimes it’s the only thing that works. If you are doing things other than brochure-ware, you may not be able to get away with a 100% css design.

fbaldoni says 11th April @ 13:39

one more thing I should criticize, the navigation area has a fixed size. This is not possible sometimes. The navigation width may need to be able to grow to a max-width. so the width may be set to auto. I would challenge you to make this work with your design.

Gerson says 17th April @ 17:57

The most clear explanation I’ve found over there, keep the good work. Greetings from Ecuador.

Leave comment