tags:

views:

55

answers:

4

Hi!

How can i float my list-items like so: http://cl.ly/77c44596c0cdff8d77d5

Thanks in advance

+2  A: 
ul {
  list-style: none;
  overflow: hidden;
}

ul li {
  width: 200px;
  height: 50px;
  margin-right: 15px;
  float: left;
}

Although note that with this you will get the lis in this order:

1  2  3
4  5  6

Edit:

Well that's slightly more difficult. If you can live with the lack of browser support you can use the CSS3 column property.

http://www.jsfiddle.net/ns5pd/1/

Yi Jiang
Thanks for your answer, but it's not how i want it. I already knew how to do it that way.
Mikkel
Uh... then why ask? Or do you want the `li` s in the specific order you wanted?
Yi Jiang
Yes, that's why i made the preview like that.
Mikkel
A: 

Pretty easily by floating them within their <ul>. The width set on the ul is to set them up in the same grid pattern as your screenshot.

ul {
    width: 330px;
    margin: 0;
    padding: 0;
    list-style: none;
}

ul li {
    float: left;
    margin: 5px;
    width: 100px;
    height: 100px;
    background: red;
}

In action here.

Pat
Thanks for your answer, but it's not how i want it. I already knew how to do it that way.
Mikkel
A: 

Here's an example that uses negative margins:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>Title</title>
<style type="text/css">
    ul {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    ul li {
        margin: 5px;
        height: 100px;
        width: 100px;
        background: red;
        float: left;
    }

    ul li.negMarg {
      margin-top: 120px;
      margin-left: -106px;
      background: blue;
    }

</style>
</head>

<body>
<ul>
  <li>1</li>
  <li class="negMarg">2</li>

  <li>3</li>
  <li class="negMarg">4</li>

  <li>5</li>
  <li class="negMarg">6</li> 
</ul>
</body>

</html> 
Psy
That isn't valid HTML. The only element that `ul` and `ol` can contain is `li`
Yi Jiang
I just added a new code sample.
Psy
Well that's an option, but isn't there a more elegant method? It's kind of annoying to add a class to every second list-item. :nth-child(even) is not an option, cause it's not cross-browser compatible.
Mikkel
A: 

If you don't mind javascript you could use the jQuery :nth-child Selector. The link contains a nice example of a situation similar to yours.

Psy