views:

836

answers:

9

Hi

For long now, I've been using a CSS class called clear, which only has the rule clear: both. I use it in the following way (shown in Django-syntax, but it is unimportant):

{% for item in collection %}
  <ul class="horiz"><!-- horizontal list -->
    <li>{{ item }}</li>
    <li>{{ item }}</li>
  </ul>
  <div class="clear"></div>
{% endfor %}

As you can see, I'm doing a bunch of horizontal lists, to make it look just like a table. Imagine that the CSS rule .horiz li implies float: left. Note that I'm using <div class="clear"></div> after each row in this "table", a lot of HTML for something so simple.

Is this really the way to go? Isn't there anything simpler that I just haven't thought about?

+2  A: 

There's way to do it in CSS

http://www.positioniseverything.net/easyclearing.html

    #pages ul li {
        display: block;
        float: left;
        .......
    }


    #pages ul:after {
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
ymv
+5  A: 

Is there a reason you can't use...

<ul class="horiz clear">

... for every list except the first?

Amber
Yes, it's ugly, and I'm not sure the code gets any easier to understand using that.
Deniz Dogan
How exactly is it "ugly"? CSS is designed to have more than one class on elements if necessary.
Amber
It's not the generated HTML that's ugly, it's the conditional checking that happens using Django's template system.
Deniz Dogan
Well, depending on the page layout, you may be able to just use "horiz clear" for all of the lists including the first - as long as there's no floating content that would normally be overlapping the lists. That shouldn't require any conditional checking.
Amber
Ah, that's true! Cool, that makes perfect sense. Thanks!
Deniz Dogan
No problem, glad to help.
Amber
Or, you can apply the clearfix rules to the "horiz" class if all the horiz lists need to be cleared. http://perishablepress.com/press/2008/02/05/lessons-learned-concerning-the-clearfix-css-hack/
kmiyashiro
A: 

The most obvious solution is to move the clear: both to the next element in your layout. If this ul is being followed by a div, give that div a class="clear", etc.

+2  A: 

If you want each of the lists to show whatever is in them, without having to add a filler DIV tag, you can adjust the overflow property of the list with this:

.horiz
{
   overflow:hidden;
}

Now, even if the list-items themselves are floated, the contents of the list should show without collapsing the area as they maybe doing now because they are shifted out of flow.

random
A: 

Put the clear on the ul.

Daniel Roseman
+1  A: 

Why not give your UL and your LI fixed widths, where the width of the UL is at least twice the width of the LI, but less than 3 times the width of an LI.

Then you can put all of your LI's in one UL and put a float: left on the LI.

This will cause each list item to appear next to each other, but they will wrap onto the following line when they run out of room (due to the width of the UL).

Chris Roberts
Cool idea, I'm actually using a method similar to that on another part of my page, which is supposed to look like a calendar, where each floated element is a "day".
Deniz Dogan
A: 

Hi Skurpur,

Another one advice. You can use clear:both in "break(<br/>)" tag. No need to code in div. See the below

Coding in CSS:

.clear {
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}

Using is HTML:

<br class="clear" />

Try!!!

Logesh Paul
A: 

I'm agree with Logesh Paul, Its become more simple to use br instead of div.

but i think you should put the clear element outside the looping.

Didats Triadi
A: 

You can have your ul with overflow: hidden;. This establishes a new block formatting context for each ul which is exactly what you want.

I used to clear: both; like you, but once I discovered this weird little quirk, I've begun using this almost exclusively. It seems semantically superior and works in 99% of cases.

Steven Xu