tags:

views:

334

answers:

2

Hi, I've a XHTML structure like this:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<title>Titel</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div id="Seite" class="bgr">
<div id="Titel" class="bgr">
<h1>some titel</h1>
</div>

<div id="Mitte" class="bgr">
<div id="Navigation" class="bgr">
<ul>
<li><a href="link.html" class="aktiv"><img src="text.gif" alt="[Link]" /></a></li>
</ul>
</div>

<div id="Inhalt" class="uebersicht bgr">
Content
</div>
</div>

<div id="Fusszeile" class="bgr">
</div>

</div>

</body>
</html>

"Titel" and "Fusszeile" are both block elements (display:block;). The additional container div "Mitte" is mainly there for margin/padding and background image reasons. The CSS for "Navigation" and "Inhalt" looks like this:

div#Navigation {
float: left;
}

div#Inhalt {
margin: 0 0 1em 185px;
padding: 0 1em;
}

The result is that the floating navigation list sticks out of the "Mitte" div. How can I fix this?

+2  A: 

You need a clear fix. See this page for one solution that doesn't require extra markup.

Their example code:

<style type="text/css">

  .clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
    }

.clearfix {display: inline-block;}  /* for IE/Mac */

</style><!-- main stylesheet ends, CC with new stylesheet below... -->

<!--[if IE]>
<style type="text/css">
  .clearfix {
    zoom: 1;     /* triggers hasLayout */
    display: block;     /* resets display for IE/Win */
    }  /* Only IE can see inside the conditional comment
    and read this CSS rule. Don't ever use a normal HTML
    comment inside the CC or it will close prematurely. */
</style>
<![endif]-->

Just copy the CSS to your project and add a .clearfix class to the #Navigation element and you'll be all set.

Reinis I.
Works great, thank you.
DaClown
A: 

Floating elements doesn't affect the size of the parent. Add a clearing div at the bottom of Mitte:

<div id="Mitte" class="bgr">
<div id="Navigation" class="bgr">
<ul>
<li><a href="link.html" class="aktiv"><img src="text.gif" alt="[Link]" /></a></li>
</ul>
</div>

<div id="Inhalt" class="uebersicht bgr">
Content
</div>
<div class="Clear"></div>
</div>

CSS:

.Clear { clear: both; height: 0; overflow: hidden; }

(The overflow setting is so that IE doesn't make the element larger than specified.)

Guffa
actually I tried that with a span element but it didn't work. Maybe I did something wrong, I'll test it again. Thanks you.
DaClown
-1: adding empty divs for the sake of clearing is poor form.
Jason
@Jason: What alternative do you suggest? You don't consider using CSS hacks as poor form?
Guffa
@guffa - no because they don't alter the markup. what happens when you redesign the site and you don't necessarily want a clear in that spot? then you have to change the markup in every place you don't want that clear, or make the "clear" class do something else. adding superfluous markup is the bloat of the internet.
Jason