【问题标题】:Floating divs with variable height可变高度的浮动div
【发布时间】:2014-03-21 17:23:10
【问题描述】:

我有一个高度可变的 div 列表,需要彼此相邻浮动。问题是评论 4 应该放在评论 2 之上。但当然不能这样做,因为评论 3 正在取代它。

在不过多更改标记的情况下,这在 css 中是否可行? cmets 是动态添加的,因此并不总是 5 cmets。我做了一个 jsfiddle 来显示问题 http://jsfiddle.net/Kv2Qf/ - 我目前拥有的是这样的:

<div id="CommentsContainer">
    <div class="Comment">
        <div class="CommentContent" style="height: 250px;">
            Comment 1
        </div>
    </div>

    <div class="Comment">
        <div class="CommentContent" style="height: 100px;">
            Comment 2
        </div>
    </div>

    <div class="Comment">
        <div class="CommentContent" style="height: 200px;">
            Comment 3
        </div>
    </div>

    <div class="Comment">
        <div class="CommentContent" style="height: 250px;">
            Comment 4
        </div>
    </div>
</div>

搭配造型:

#CommentsContainer
{
    width: 783px;
    height: 500px;
    background-color: #f2f2f2;
}

.Comment
{
    width: 229px;
    float: left;
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    margin-bottom: 10px;
    margin-right: 10px;
    border: 1px solid #aaaaaa;
    background-color: #fffec7;
}

结果:

预期结果:

【问题讨论】:

标签: css html


【解决方案1】:

您可以尝试为第四个框设置负边距:

<div class="Comment" style="margin-top:-90px;">
    <div class="CommentContent" style="height: 250px;">
        Comment 4
    </div>
</div>

适用于 IE 和 Firefox,从未在其他浏览器中测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2013-10-06
    • 2013-06-27
    • 2023-03-08
    • 2011-09-16
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多