views:

763

answers:

2

I have a menu of links on the left, and content in the middle. If the content is longer than the links, it will wrap below them instead of staying neatly to the right.

Adding float:left to my content div fixes this, except in IE, where it causes the menu (and the footer) to disappear entirely.

I need to either fix the problem with float:left, or find another way of keeping all my content to the right of the menu.

Here's a pared-down version of the html and css code that shows the problem in action. Replace the #content { float:left; } with #content {} to see the original wrapping problem.

content {margin:200px;} ALMOST solves this problem -- but the lines that previously would have wrapped are now about 3 pixels further left than those above them, again only in IE.

html:

<HTML><HEAD>
<TITLE>Index</TITLE>
<LINK rel="stylesheet" type="text/css" href="style.css">
</HEAD>
<BODY>
    <DIV id="main">
     <DIV id="nav">
      <A href="link1.html">Link 1</A>
      <A href="link2.html">Link 2</A>
     </DIV>
     <DIV id="content">
      Content<br>
      goes<br>
      here<br>
      and<br>
      sometimes<br>
      wraps<br>
      under<br>
      links
     </DIV>
     <DIV id="footer">
      Footer<BR>
     </DIV>
    </DIV>
</BODY>
</HTML>

css:

* {
    padding: 0;
    margin: 0;
    text-decoration:none;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    color: #fff;
}

img { border:0px; } 
body { background-color:#454547; padding: 4px; }
#main { position: relative; top: 20px; }
#nav {
    position: relative;
    left: 0px;
    width: 200px;
    float: left;
    font-weight: bold;
    padding: .25em;
    line-height: 1.25em;
    font-size: 1em;
}
#nav a { display: block; height: 35px; padding: 3px; color:#FFFFFF; }
#nav a:hover { background-color:#CCCCCC; color:#333; }
#footer { width: 100%; clear: both; margin: 0px 0px 10px 0px; padding: 10px; font-size:10px; }
#content { float:left; }
+1  A: 

ok, if you're not happy with my former answer, I'll go through it step by step...

there are very many things you can do wrong with HTML/CSS but there are also a few things you can do right and if you do them right, you will have to worry much less about the things you do wrong. so it's better to learn the few things you can do right and that's why I told you to read the other questions/answers.

I could tell you to take out the line "position:relative" in your #main div and you would find out that the problem is gone... but I wouldn't because you would not improve your CSS in general.

first of all, use DOCTYPES. whenever possible, use strict like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

if you think you can't apply strict, use transitional but be aware of the fact that you will already open the spectrum of possible problems again.

second: don't apply margins to div tags that contain your content. instaed create a div tag around the content tag and apply the margins there! you may use paddings on the container but no margins.

third: use a stack of resets in the beginning of your style sheet, there are quite many available, a good set (comes with the yui package by yahoo) which I use for many sites with strict xhtml as follows:

@charset "utf-8";
/*
 Copyright (c) 2008, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.net/yui/license.txt
 version: 2.6.0
 Adapted by Markus Hausammann
 */
/* reset rules */ * {
    font-size: 100.01%;
    overflow: hidden
}

html, body
{
    color: #313131;
    background-color: #ffffff;
    font-family: Geneva, Arial, Verdana, Helvetica, sans-serif;
    line-height: 1.2em;
}

body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td {
    margin: 0;
    padding: 0;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

fieldset, img {
    border: 0;
}

address, caption, cite, code, dfn, em, strong, th, var {
    font-style: normal;
    font-weight: normal;
}

li {
    list-style: none;
}

caption, th {
    text-align: left;
}

h1, h2, h3, h4, h5, h6 {
    font-size: 100%;
    font-weight: normal;
}

q:before, q:after {
    content: '';
}

abbr, acronym {
    border: 0;
    font-variant: normal;
}

/* to preserve line-height and selector appearance */
sup {
    vertical-align: text-top;
}

sub {
    vertical-align: text-bottom;
}

input, textarea, select {
    font-family: inherit;
    font-size: inherit;
    font-weight: inherit;
}

/*because legend doesn't inherit in IE */
legend {
    color: #000;
}

del, ins {
    text-decoration: none;
}

/* clearing methods */
/* clearfix method for clearing floats */ .clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

/* essential for Safari browser !! */ .clearfix {
    display: block;
}

/* removes ugly mozilla dotted link borders */
a:active
{
  outline: none;
}

fourth: don't float more than one div into the same direction in order to avoid some IE problems. if you need more than two divs create wrapper divs. so for example for a three column layout you would float your first column left, then a wrapper div with float:right containing the second column floated left (within the wrapper) and the third column floated right within the wrapper... etc. easy but very controllable!


former answer:

give a width (maxwidth resp.) to all the divs, float the menu left and the content right. all problems solved. works for percentaged widths too.

OR

give your main div the attribute inline and your menu div a fixed width.

OR

use grids (e.g. http://developer.yahoo.com/yui/grids/ or http://www.blueprintcss.org/)

OR

read any of the many other posts about this topic on stackoverflow (see on the right).

tharkun
When it comes to the myriad combinations of things you can do in CSS and HTML, it's very hard to tell which problems have the same cause and solution, and which just have similar symptoms and keywords. If you know of another question that's had this same problem and is solved, by al means, link it.
Jorenko
content {float:right; width:500px;} does NOT solve the problem -- it just moves the content further to the right
Jorenko
"the attribute inline" -- what?
Jorenko
I don't need to know which solution is fashionably more "correct" in the CSS community this week, and I don't have hours to spend becoming more familiar with CSS right now. I just need to fix a bug in a page when my CSS guy is unavailable.
Jorenko
SO is here to promote learning, not get your job done for you, though...
bobince
Actually, SO is for getting answers to questions. I asked, "How can I get X to work like Y", not "Teach me why X does isn't doing Y."
Jorenko
what a grumpy guy :)
tharkun
+2  A: 

find another way of keeping all my content to the right of the menu.

#content { margin-left: 200px; }

Consider adding a Standard Mode doctype to reduce the quirkiness of IE.

bobince