1

Here's a before and after picture. When I add a float:left to the li, the layout breaks and the div that's supposed to be below this topmenu div, floats up.

alt text alt text

Here's the CSS:

#topmenu
{
    background-color:#335D7C;    
}

#topmenu ul
{
    list-style:none;
    margin:0;
    padding:0;
}

#topmenu ul li
{    
    background-image: url('../Content/Images/topmenutick.png');
    background-repeat:no-repeat;
    color:White;
    float:left;
    padding-left:15px;
    padding-right:15px;
}

#topmenu ul li a
{    
    text-decoration:none;
}

#topmenu ul li a:hover
{    
    text-decoration:none;
}
2
  • 2
    Try adding clear: both; to whatever element is supposed to be below the floated list. Commented Dec 12, 2010 at 18:24
  • Could you please provide the HTML as well? It looks like you're missing a clear: left; after the #topmenu. Commented Dec 12, 2010 at 18:24

3 Answers 3

3

That's because the elements don't take a full-width bounding box anymore. They all float to the left, and the following content runs right up to the end of the floating box. Add clear:left; to the CSS of the following element.

Alternatively, you can use a generated element to clear the float. Google for clearfix.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the solution. I added 'clear:both' to the div in orange and it works perfectly. Thanks!
1

You should control the urge of using floats.

#topmenu ul li
{    
    ...
}

float:left; display: inline;

Comments

0

Use a cleaner div after the floated element.
CSS:

.cl {
    clear: both;
}

HTML:

<div class="cl"></div>

4 Comments

Why add elements just to give them a css rule that can be applied to any pre-existing element?
@David Thomas: Maybe just my bad habit.
fair enough, I guess. But that way lies the madness of tag-soup, and divitis...
@David Is it good to add just the cleaner class to the next element even if it has a class already, for example <div class="next_divs_class cl"> or is it better to avoid the .cl class entirely and add the clear: both to the CSS for every class which needs it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.