开发者

Floating div issue

I have an issue with floating divs. I have a container st to fixed width, and I have child elements inside that which are all div elements. What I want is that, I need two elements to be shown in a row. The code that I wrote is as follows.

CSS

#container
    {
        width: 400px;
    }
    .item1
    {
        width: 180px;
        height: 200px;
        border: 1px solid red;
        float: left;
        margin: 10px 0 0 10px;
    }
    .item2
    {
        width: 180px;
        height: 250px;
        border: 1px solid red;
        float: left;
        margin: 10px 0 0 10px;
    }

HTML

<div id="container">
        <div class="item1">1</div>
        <div class="item1">2</div>
        <div class="item1">3</div>
        <div class="item1">4</div>
        <div class="item1">5</div开发者_JAVA技巧>
        <div class="item1">6</div>
        <div class="item1">7</div>
        <div class="item1">8</div>
        <div class="item1">9</div>
    </div>

This can be viewed at Demo1

But what I want is like this result. The only thing is that the height of the individual items can be different.

Hope I have made everything clear.

Thanks in advance

Additional clarification

The content elements will be generated dynamically in server and will be passed to the client.

Also the order should be like 1,2,3,4,...

The only thing is that in a row there should be two items and the first one should be aligned to the left side of the container.


You can't accomplish that with CSS only, but there is a jQuery plugin to do the trick. It's called jQuery Masonry, give it a try


You need a second wrapper:

<div id="container">
        <div class="wrapper"><div class="item1">1</div></div>
        <div class="wrapper"><div class="item1">2</div></div>
        ...
</div>

Float the wrapper and give it a fixed size. The items inside can have their own height.

I prefer using lists for this type of thing. Better HTML semantics.

<div class="container">
   <ul>
       <li><div class="item1">1</div></li>
       <li><div class="item2">2</div></li>
   </ul>
</div>

style:

.container ul {
    width:400px;
}

.container li {
    float:left;
    height:200px;
    width:180px;

}


If you want each pair of items to be in a row, and you have control over the dynamic generation of the content, see my edits to your fiddle here

To summarize:

Markup -

<div id="container">
    <div class="itemrow">
        <div class="item1">1</div>
        <div class="item1">2</div>
    </div>
    <div class="itemrow">
        <div class="item2">3</div>
        <div class="item1">4</div>
    </div>
    <div class="itemrow">
        <div class="item2">5</div>
        <div class="item1">6</div>
    </div>
    <div class="itemrow">
        <div class="item1">7</div>
        <div class="item2">8</div>
    </div>
    <div class="itemrow">
        <div class="item1">9</div>
    </div>
</div>

CSS -

    #container
    {
        width: 400px;
    }
    .itemrow
    {
        float: left;
        clear: both;
    }
    .item1
    {
        width: 180px;
        height: 200px;
        border: 1px solid red;
        float: left;
        margin: 10px 0 0 10px;
    }
    .item2
    {
        width: 190px;
        height: 250px;
        border: 1px solid red;
        float: left;
        margin: 10px 0 0 10px;
    }

Edit: Just read your above comment about having to edit the server side logic for rendering. Obviously this will only work if you can control that.


you're specifying item2 to be 10 pixels wider than item1 so I'm not clear on what you're trying to do....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜